[2.0.0] Phalcon\Validation\Validator\PresenceOf fails on 0
Since Version 2.0.0 the Phalcon\Validation\Validator\PresenceOf Validator returns failure if the Value is "0".
$validator->add('activities', new PresenceOfValidator());
I have an Input field where the Customer writes the Number of Activities for an Entity. It is possible to have 0 activities for the Entity.
With Version 1.3.4 everything works fine. But since Version 2.0.0 the validator fails if i enter 0 activities.
Reference:
I agree that the PresenceOf validator should be reverted to the old way of validating values.
In 2.0.x it uses PHP empty(), which in this context incorrectly treats 0 and "0" as empty. https://github.com/phalcon/cphalcon/blob/master/phalcon/validation/validator/presenceof.zep#L48
Pre 2.0.x it used PHALCON_IS_EMPTY(value), which meant "not null && not empty string"
#define PHALCON_IS_EMPTY(var) (Z_TYPE_P(var) == IS_NULL || (Z_TYPE_P(var) == IS_STRING && !Z_STRLEN_P(var)))
The empty construct in zephir does not work the same way as the php function empty(). It does not treat 0 as empty.
$form = new \Phalcon\Forms\Form();
$options = array(
1 => 'Yes',
0 => 'No',
);
$element = new \Phalcon\Forms\Element\Select('active', $options);
$element->addValidator(new \Phalcon\Validation\Validator\PresenceOf());
$form->add($element);
$postData = array(
'active' => '0',
);
if ($form->isValid($postData)) {
print "Valid!";
} else {
foreach ($form->getMessages() as $message) {
print $message->getMessage();
}
}
Expected (as 1.3.4):
Valid!
Actual (2.0.1):
Field active is required