I need something like «cancelOnSuccess» :)
My decision:
class EmptyValidator extends Validator implements ValidatorInterface
{
/**
* @var \Phalcon\Validation\ValidatorInterface[]
*/
protected $validators;
/**
* @param \Phalcon\Validation\ValidatorInterface[] $validators
*/
public function __construct($validators = null)
{
$this->validators = $validators;
}
/**
* @param Validation $validator
* @param string $attribute
*
* @return boolean
*/
public function validate($validator, $attribute)
{
$value = $validator->getValue($attribute);
$result = true;
if (!empty($value)) {
if (!empty($this->validators) && sizeof($this->validators)) {
/**
* @var \Phalcon\Validation\ValidatorInterface $_validator
*/
foreach ($this->validators as $_validator) {
$validationResult = $_validator->validate($validator, $attribute);
if (!$validationResult) {
$result = false;
}
}
}
}
return $result;
}
}
And using:
$email->addValidators([new EmptyValidator([
new EmailValidator([
'message' => 'Email is invalid',
]),
])]);