Respect\Validation - 如果字符串不为空则验证字符串,否则 return true

Respect\Validation - validate string if not empty, otherwise return true

我正在使用 Respect\Validation 来验证字段。我的一些字段不是强制性的,如 phone 数字,但如果它们不为空,则必须对其进行验证。 按照我当前的代码:

v::Phone()->validate($phoneNumber); // it should return true if $phoneNumber is empty

怎么做?

基于the docs,看起来应该是:

v::optional(v::Phone())->validate($phoneNumber);