Reputation: 336
I am a newbie in zendframework. I am using zendframework version 2.
I have a ClientForm class which is inheriting from Zend\Form\Form.
class Client extends Form
{
public function __construct($name = null, $options = array())
{
if (null == $name) $name = 'ClientFrom';
parent::__construct($name, $options);
$this->add(array(
'name' => 'clientName',
'type' => 'Text'
));
$this->add(array(
'name' => 'address1',
'type' => 'Text'
));
}
}
I need to implement validation and filtering for the above form.
Rules for Validation
'clientName' => required, min = 3, max = 25
Rules for Filter
'clientName' => [a-zA-Z0-9_ ]
Questions
Thank you.
Upvotes: 1
Views: 158
Reputation: 12111
Try this:
$this->add(array(
'name' => 'clientName',
'type' => 'text',
'required' => true,
'validators' => array(
new Validator\RegexValidator('/^#[a-zA-Z0-9_ ]$/'),
new Validator\StringLength(array('min'=>3 ,'max' => 25))
)
));
Upvotes: 2
Reputation: 16445
There is the Zend\InputFilter
-Component that you'd use for this kind of task. There's a lot of examples out there, some programatical ones like here or some configurational ones like here or here.
The later two being examples used for specific Doctrine-Validators, but you can use them for any normal Zend\InputFilter
, too.
Upvotes: 1
Reputation: 3953
I suppose you have no code yourself yet ... well.
'clientName' => required, min = 3, max = 25
This one isnt too difficult, heres some easy code ... ill write it extremely simple and readable, you can shorten the if-statements if you want:
if(!empty($clientName)) {
if(!($clientName > 25)) {
if(!($clientName < 3)) {
echo "valid name!";
}
}
} else {
return false;
}
'clientName' => [a-zA-Z0-9_ ]
Either you use RegEx or string-functions where you only allow these specific characters.
Upvotes: 0