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
]
];
Related
i want to change the type of car_engine from text to option but i do not even understand what this code means and in which language i shall write my modification.
i have tried no thing because i had no idea where should i start.
public static function front_fields( $fields ) {
$fields[] = [
'forms' => ['car', ],
'type' => 'fieldset',
'id' => 'car',
'legend' => esc_html__( 'Car', 'listing-manager' ),
'collapsible' => true,
'fields' => [
[
'id' => LISTING_MANAGER_LISTING_PREFIX . 'car_engine',
'type' => 'text',
'label' => esc_html__( 'Engine', 'listing-manager' ),
'required' => false,
],
Try to use this way (untested):
public static function front_fields( $fields ) {
$fields[] = [
'forms' => ['car', ],
'type' => 'fieldset',
'id' => 'car',
'legend' => esc_html__( 'Car', 'listing-manager' ),
'collapsible' => true,
'fields' => [
[
'id' => LISTING_MANAGER_LISTING_PREFIX . 'car_engine',
'type' => 'select',
'label' => esc_html__( 'Engine', 'listing-manager' ),
'required' => false,
'options' => [ //options may be values
'alabama' => 'Alabama',
'alaska' => 'Alaska'
],
'placeholder' => '',
],
]
);
I am creating ZF2 email address validator via factory that has 2 parts, one checks if emaill is already in DB, two: validate the email.
Prblem is that my NoObjectExists validator works just fine, but the acatual Email address validator does not (validator recognises "dsfsfhsadjkfnaskl" as valid email). Here is my code, maybe you guys can spot what is wrong with it?
$factory = new \Zend\InputFilter\Factory();
$input = $factory->createInput(array(
'name' => 'email',
'required' => false,
'filters' => array(
0 => array(
'name' => 'Zend\Filter\StringTrim',
'options' => array(),
),
),
'validators' => array(
0 => array(
'name' => '\DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $this,
'fields' => array('email'),
),
1 => array(
'name' => '\Zend\Validator\EmailAddress',
'options' => array(
'allow' => \Zend\Validator\Hostname::ALLOW_DNS,
'domain' => true,
),
),
),
),
));
return $input;
You have an error in your array nesting. You aren't closing off the containing the NoObjectExists validator so the Email validator is nested inside.
Try the following:
$factory = new \Zend\InputFilter\Factory();
$input = $factory->createInput(array(
'name' => 'email',
'required' => false,
'filters' => array(
0 => array(
'name' => 'Zend\Filter\StringTrim',
'options' => array(),
),
),
'validators' => array(
0 => array(
'name' => '\DoctrineModule\Validator\NoObjectExists',
'options' => array(
'object_repository' => $this,
'fields' => array('email'),
),
),
1 => array(
'name' => '\Zend\Validator\EmailAddress',
'options' => array(
'allow' => \Zend\Validator\Hostname::ALLOW_DNS,
'domain' => true,
),
),
),
));
return $input;
I have a custom validator using $context that has been working for over a year.
The $context contains all posted parameters except for "bar" which is an empty array although it is posted as bar[0][description]. The array used to contain one or more arrays with key-value-pairs.
This seems to be the case since updating from version 2.3.2 to 2.5.1
I can't even check the 2.5 documentation since it's not available, Google didn't turn up anything close to the issue, ... was the behaviour changed or is this a bug?
Edit:
In .../Lorem/Form/FooForm.php I include a fieldset:
public function __construct() {
// ...
$this->add(array(
'type' => 'Collection',
'name' => 'bar',
'options' => array(
'label'=> '',
'count' => 1,
'should_create_template' => TRUE,
'allow_add' => TRUE,
'use_as_base_fieldset' => TRUE,
'target_element' => array(
'type' => 'Lorem\Fieldset\BarFieldset'
)
)
));
}
In .../Lorem/Fieldset/BarFieldset.php I have:
public function __construct() {
parent::__construct('bar');
$this->setHydrator(new ReflectionHydrator())
->setObject(new Bar());
$this->add(array(
'name' => 'description',
'type' => 'Textarea',
'attributes' => array(
'rows' => 3,
'cols' => 40,
),
));
$this->add(array(
'name' => 'amount',
'attributes' => array(
'type' => 'Text',
'class' => 'input-bar-amount',
'maxlength' => 9,
),
));
// ...
}
public function getInputFilterSpecification()
{
return array(
'description' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => TRUE,
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter a description.',
),
),
),
),
),
'amount' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => TRUE,
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter amount.',
),
),
),
array(
'name' => 'Float',
'break_chain_on_failure' => TRUE,
'options' => array(
'locale' => 'de',
'messages' => array(
'floatInvalid' => 'Invalid input. String, Integer oder Float expected.',
'notFloat' => 'Input ist not a floating point number.',
),
),
),
array(
'name' => 'Between',
'break_chain_on_failure' => TRUE,
'options' => array(
'min' => -1000000,
'max' => 1000000,
'messages' => array(
'notBetween' => "Only values between '%min%' and '%max%' EUR are allowed.",
'notBetweenStrict' => "Only values between '%min%' and '%max%' EUR are allowed.",
),
),
),
),
),
// ...
);
}
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() ?
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',
),
),