Is there a NotEqualTo validator in Zend Framework 2? If not; how can we implement a NotEqualTo validator in Zend Framework 2? Or is there any other workarounds?
Example
I have a text field having the following attributes
'onfocus'=>'if(value==\'Name\'){ value=\'\'}'
'onblur'=>'if(value==\'\'){ value=\'Name\'}'
When the user submits the form without entering their name; I have to validate whether the field is having a value of 'Name' and if it has; return an error.
Note:
I can't use the placeholder attribute as my client require the placeholder text to disappear on clicking the text field(currently it disappears only when we begin typing).
I have made a simple workaround using the Callback validator as follows.
'validators' => array(
array(
'name' => 'Zend\Validator\Callback',
'options' => array(
'callback'=>function($value){
return $value!='Name';
}),
),
),
),
Details of the Callback Validator can be seen at the following link
http://framework.zend.com/manual/2.0/en/modules/zend.validator.set.html#callback
Related
I use standalone Symfony components in my app (and without Twig).
My HTML form contains two fields ('new_password' & 'confirm_new_password').
The following validation works fine:
$validator = Validation::createValidator();
$violations = $validator->validate($new_password, [
new Length(['min' => 4]),
new Regex([
'pattern' => '/\d/',
'match' => true,
'message' => 'Password must contain at least one number'
])
]);
if (0 !== count($violations)) {
...
}
I would like to add validation of password confirmation fields as well
The 'Form' component by Symfony allows to create, process and reuse forms, but this is far beyond of what I want to do. I found that 'RepeatedType' field by Symfony can do this but seems to be using the 'Form' component.
How can I simply add password confirmation to my validation script?
there is a constraint call identicalTo that looks quite the same as explained by RiggsFolly. So you call this constraint and give it both fields' values.
I wrote an input filter for a Form by inheriting from InputFilter (class CustomInputFilter extends Zend\InputFilter\InputFilter).
By default, each form element is required. This means that I have to set required to False explicitly for all optional elements. How can I change the InputFilter such that all form elements are optional by default?
I use the Zend 2 framework.
I am not sure about it is possible the way you want using InputFilter. I think this implies filtering and validating data. If any input field does not need to be required, then ZF have had the option ('required' => false). See another approach
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'allow_empty' => true,
));
So why is this? This is something that input field must exist but you can keep it empty. If you then put invalid data in it can then validate data too.
I am building a web application using ZF2 and Doctrine. I have a view containing a base form to which the user can add multiple instances of a fieldset, the filedsets are added via HTML template and js cloning. We are making use of the Doctrine hydrator and cascade=persist to write to the dB. It is all working but I am concerned when the fieldsets are added it results in multiple items with the same ID which breaks w3 standards. Has anyone a solution or work around for this? Or would it be considered acceptable in this instance?
An example of one fieldset element:
$this->add(array(
'name' => 'glassAssemblyID',
'attributes' => array(
'type'=> 'hidden',
'id' => 'glassAssemblyID',
),
));
Many thanks
James
You should set the ID in JavaScript after cloning the element.
This is an easy one. Just just change your code to:
$this->add(array(
//'name' => 'glassAssemblyID',
'attributes' => array(
'type'=> 'hidden',
//'id' => 'glassAssemblyID',
),
));
No point in putting out an element id which is obviously not being used.
If you really feel you do need ids for some reason then put out something like EntityType-id for your ids.
Zend\InputFilter\InputFilter creates inputs with the createInput() method of Zend\InputFilter\Factory. But I was digging through the code and could not find where the actual input filter key value pairs are defined. For example:
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
), ...
'Int' is a filter. Where is that defined in the zf2 library so I can see what other possible filters there are. I know the docs have info on this, but I'd like to know where it is defined in the actual library.
You'll find it in the class Zend\Filter\FilterPluginManager.
The path of the file is your_project/vendor/zendframework/zendframework/library/Zend/Filter
In this directory you'll also find all the filter's classes (like the Int one).
I used the Symfony 2 documentation to create a simple registration.
But now i have two little problems. The password fields, which I created with the Form Builder, be time in plain text.
The second problem is that the action of the form is not used the correct route. When I press the submit button, I get the standard page.
Has anyone of you an idea, which may be related?
If you still parts of the code needed, I like to add these.
Greetings
did you set it to a password type? eg
$builder->add('password', 'password)
Or can do repeated field
$builder->add('password', 'repeated', array(
'type' => 'password',
));
When you build the form do you set action?
$form = $this->createForm(new Type(), $type, array(
'action' => $this->generateUrl('your_route'),
));