Validation: problem with nullValue()

I have a class that has a property, end_price, that I validate like this:

v::attribute('end_price', v::oneOf(v::nullValue(),v::numeric()->positive()))

if I set this property to and empty string and then do ->assert($this) it passes. I have tried this with 9.2 and 9.3.

what am I missing?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (7 by maintainers)

Most upvoted comments

@asperon I think your problem is that you are passing an object to assert() instead of the result, which is akin to doing assert(new StdClass) which always equates to true.

Try the following instead:

$obj = new stdClass;
$obj->foo = ''; //test empty string

$result = v::attribute('foo', v::oneOf(v::nullValue(),v::numeric()->positive()))->validate($obj); //false

assert($result); //assertion will fail...

Also you don’t need to chain notEmpty() to nullValue() see description:

NullValue

Validates if the input is null. This rule does not allow empty strings to avoid ambiguity.

Hope that helps…