Am litle confused with email validations. In my model i define rules for form fields. My problem is that I do not know where to put Zend\Validator\EmailAddress() rule. In standard InputFilter i have standard filters and validators like : require, min, max, encode ect. And does ZF2 supports validation for phone numbers ?
I read this but i dont know how to mix EmailAddress rules in my model array getInputFilter(). Do i need define email validations in controller or i can do that in model ?
http://framework.zend.com/manual/2.0/en/modules/zend.validator.set.html
My Model use Zend\InputFilter\ÍnputFilter and i set rule like this :
/**
* Company Rules
*
* #filters
* StripTags | StringTrim
* #validators
* StringLenght | UTF-8 encoding | min | max
*/
$filter->add(array(
'name' => 'company',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLenght',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
/**
* Fax Rules
*
* #filters
* StripTags | StringTrim | Int
* #validators
* StringLenght | UTF-8 encoding | min | max
*/
$filter->add(array(
'name' => 'fax',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLenght',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
}
}
You can mix different validators writing styles in the following way:
'validators' => array(
array(
'name' => 'StringLength',
'options' => ...
),
new \Zend\Validator\EmailAddress()
),
You can also specify EmailAddress validator using an array-like format:
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
array(
'name' => 'EmailAddress',
),
),
Regarding validation of phone numbers, ZF2 doesn't provide validators ready for use (probably because there are so many differences between international phone numbers format), but using Zend\Validator\Regex you could implement one with not too much work.
this is the simple and best suited way of email validation at-least for me
'validators' => array(
array(
'name' => 'EmailAddress',
'options' =>array(
'domain' => 'true',
'hostname' => 'true',
'mx' => 'true',
'deep' => 'true',
'message' => 'Invalid email address',
),
),
Related
i am using the getInputFilterSpecification() to validate my form elements i.e
public function getInputFilterSpecification()
{
return array(
'comment' => array(
'required' => true,
'filters' =>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
),
),
),
),
'name' => array(
'required' => true,
'filters' =>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
),
),
),
),
}
my question is: rather than repeat the same validators for each separate element in my forms, is its possible to lump all the elements into an array and to get the valiators to loop all over them .
i.e somthing like this:
$validateThisGroup = $inputFilter->setValidationGroup(array('comment', 'name'));
return array(
$validateThisGroup => array(
'required' => true,
'filters' =>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
),
),
),
),
UPDATE;
I have reworded my question. I am basically trying to find out if there is a validator group function for the input filter specification function.
Not sure if my advice deserves to be an fully fledged answer but anyways :)
Validators need to be created separately for each input, they are separate stateful instances created by InputFilter's plugin manager, so you need to pass separate definition of validators for each input.
If you just want to avoid copypasting, define validator definition before returning and plug it into return array.
$inputValidatorsSpec = [
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
],
],
];
$inputFiltersSpec = [
['name' => 'StripTags'],
['name' => 'StringTrim'],
];
return [
'comment' => [
'required' => true,
'filters' => $inputFiltersSpec,
'validators' => $inputValidatorsSpec
],
'name' => [
'required' => true,
'filters' => $inputFiltersSpec,
'validators' => $inputValidatorsSpec
]
];
I have a model that implements InputFilterAwareInterface. For one of the fields I wish to validate that user input is digits and use null filter to ensure db field is set to null. Is there a way to do this? The following doesn't work.
$inputFilter->add(array(
'name' => '_programme_id',
'required' => false,
'allow_empty' => true,
'validators' => array(
array(
'name' => 'Digits',
)
),
'filters' => array(
array(
'name' => 'Null',
'options' => array(
'type' => 'all'
),
)
),
)
);
Validation fails with message "Invalid type given. String, integer or float expected"
Seems like this is because filtering happens before validation. Is there a quick way to achieve this behaviour?
You can convert your value to integer with the Int filter before passing it to Null filter, as follows:
$inputFilter->add(array(
'name' => '_programme_id',
'required' => false,
'allow_empty' => true,
'validators' => array(
array(
'name' => 'Digits',
)
),
'filters' => array(
array(
'name' => 'Int',
)
array(
'name' => 'Null',
'options' => array(
'type' => 'all'
),
)
),
)
);
I have a select element as follows and I want to make it non required field. But I am unable to do so. It gives me error "Value is required and can't be empty"
$this->add(array(
'name' => 'civil_status',
'type' => 'Select',
'attributes' => array(
'id' => 'civil_status',
'class' => 'form-control',
),
'options' => array(
'empty_option' => 'Civil status',
'value_options' => array(
'married' => 'Married',
'single' => 'Single',
'other' => 'other',
),
)
));
I have set the validation as follows
$this->inputFilter->add($factory->createInput(array(
'name' => 'civil_status',
'required' => FALSE,
'allow_empty' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => '2',
'max' => '255',
),
),
),
)));
I tried to remove required from input filter as above and even more I have tried to remove required validation from the controller too as follows. But nothing seams to work. Please let me know if you guys know to fix this issue.
$form->getInputFilter()->get("civil_status")->setRequired(FALSE);
$form->getInputFilter()->get("civil_status")->setAllowEmpty(TRUE);
Select has build in InArray validator you dont need to use StringLength here or filters you only need to use 'required' => false which should work.
$this->inputFilter->add($factory->createInput(array(
'name' => 'civil_status',
'required' => false,
)));
I know it might be strange question but did you add your InputFilter to your Form before you hit $form->IsValid() ?
$inputFilter->add($factory->createInput(array(
'name' => 'amount',
'validators' => array(
array(
'name' => 'string_length',
'options' => array(
'max' => 60,
'messages' => array(
\Zend\Validator\StringLength::TOO_LONG => 'Title should be less than %max% characters',
),
How to add numeric validation for this code ?
You can use the Int or Float validators: I18n Validators
$inputFilter->add($factory->createInput(array(
'name' => 'amount',
'validators' => array(
array(
'name' => 'string_length',
'options' => array(
'max' => 60`
'messages' => array(
\Zend\Validator\Digits::NOT_DIGITS => 'It is not a Valid Numeric Character',
Note: Validating numbers
When you want to validate numbers or numeric values, be aware that this validator only validates digits. This means that any other sign like a thousand separator or a comma will not pass this validator. In this case you should use Zend_Validate_Int or Zend_Validate_Float.
I've got the following problem. I wrote (based on the tutorial) a form validation. The text fields work just fine but the integer field behave odd.
This is my validator:
$inputFilter->add($factory->createInput(array(
'name' => 'zip',
'required' => false,
'filters' => array(
array('name' => 'Int'),
),
)));
It lies within my Entity.php like the other filters. The odd thing is that this one accepts not even a string but ignores the required when I set it to true. I tried to replace Int with Digits which then causes the form to accept required but still accepts strings.
Any ideas? Thanks!
Try using the Between validator:
$inputFilter->add($factory->createInput(array(
'name' => 'zip',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'Between',
'options' => array(
'min' => 1,
'max' => 1000,
),
),
),
)));
this is a old topic but i should mention that Filters don't cause validation errors, they work in background and do their jobs silently .
for example Int filter will remove any non-integer from the input , so when you do $form->getData() the field with the Int filter will only have integer values and 0 if its empty.
array(
'name' => 'not_empty',
),
array(
'name' => 'Digits',
), array(
'name' => 'Between',
'options' => array(
'min' => 0,
'max' => 1,
),
),