Last active
December 22, 2015 11:49
-
-
Save phillipsharring/3b401b4c251156ed442d to your computer and use it in GitHub Desktop.
Zend Framework 2 Form Attaching a custom validator to an element that allows nulls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Validator; | |
use Zend\Validator\AbstractValidator; | |
class MyCustomValidator extends AbstractValidator | |
{ | |
const CONDITIONAL_EMPTY = 'conditionally_empty'; | |
const SOMETHING_BAD = 'something_bad'; | |
protected $messageTemplates = array( | |
self::CONDITIONAL_EMPTY => 'The value is required if you select something else', | |
self::SOMETHING_BAD => 'Something bad happened', | |
); | |
// bring in the context in the 2nd argument | |
// but default to null to match the parent signature | |
public function isValid($value, $context = null) | |
{ | |
$this->setValue($value); | |
// here's the allow empty bit, | |
// say if the user didn't select 'something_else', this field isn't required. | |
if ('' == $context['something_else']) { | |
return true; | |
} | |
// do your extra validation here, since 'something_else' had a value... | |
$error = false; | |
if ($error) { | |
$this->error(self::SOMETHING_BAD); | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Filter; | |
use Zend\InputFilter\InputFilter; | |
use Zend\Validator\NotEmpty; | |
use Application\Validator\MyCustomValidator; | |
class MyCustomFilter extends InputFilter | |
{ | |
public function __construct() | |
{ | |
// this is the field that may be empty | |
$this->add(array( | |
'name' => 'the_field', | |
// don't use these here; leave them out | |
// 'required' => true, | |
// 'allow_empty' => true, | |
'filters' => array( | |
// filters go here | |
), | |
// no 'validators' array | |
)); | |
$myValidator = new MyCustomValidator(); | |
$myValidator->setMessages( | |
array( | |
// your messages | |
'conditionally_empty' => 'the_field cannot be empty you select something_else' | |
) | |
); | |
$theField = $this->get('the_field'); | |
$theField->getValidatorChain() | |
// reattach the NotEmpty filter with NotEmpty::NULL | |
->attach(new NotEmpty(NotEmpty::NULL)) | |
// attach your custom validator | |
// second argument is 'break chain on error' | |
->attach($myValidator, true); | |
/* | |
// other fields in this filter | |
$this->add(array( | |
'name' => 'another_field', | |
)); | |
// etc. | |
// */ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment