Skip to content

Instantly share code, notes, and snippets.

@intellix
Last active December 20, 2015 12:19
Show Gist options
  • Save intellix/6129922 to your computer and use it in GitHub Desktop.
Save intellix/6129922 to your computer and use it in GitHub Desktop.
Simple ZF2 validation using inputProviderInterface
<?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