Last active
December 20, 2015 12:19
-
-
Save intellix/6129922 to your computer and use it in GitHub Desktop.
Simple ZF2 validation using inputProviderInterface
This file contains 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\Form; | |
use Zend\Form\Form; | |
use Zend\Validator; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
class YourForm extends Form implements InputFilterProviderInterface | |
{ | |
public function __construct() | |
{ | |
parent::__construct('your-form-name'); | |
$this->add(array( | |
'name' => 'persons_name', | |
'type' => 'text'; | |
)); | |
$this->add(array( | |
'name' => 'dynamically_added', | |
'type' => 'text' | |
)); | |
$this->getInputFilter()->add(array( | |
'name' => 'dynamically_added', | |
'required' => true, | |
'validators' => array( | |
new Validator\LessThan( | |
array( | |
'max' => 1, | |
'inclusive' => true | |
) | |
) | |
) | |
)); | |
$this->add(array( | |
'name' => 'amount', | |
'type' => 'text'; | |
)); | |
} | |
public function getInputFilterSpecification() | |
{ | |
return array( | |
'persons_name' => array( | |
'required' => true, | |
'allow_empty' => true, | |
), | |
'amount' => array( | |
'required' => true, | |
'allow_empty' => true, | |
'validators' => array( | |
new Validator\LessThan( | |
array( | |
'max' => 10 | |
) | |
), | |
new Validator\Callback( | |
array( | |
'callback' => function ($value) { | |
return $value / 2; | |
} | |
) | |
) | |
) | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment