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.
Related
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.
In my Laravel 5.2 app, I have the following validation rules to check a field:
$rules = array(
'file' => 'mimes:jpg,png,pdf,doc,docx',
'file' => 'validate_file'
);
Now, the validate_file is a custom validation rule. I want to run it only only if the first mimes validation passes.
I know I could simply validate/redirect with errors - in two batches, first the mimes, then the validate_file, but it seems like an overkill.
Any way I can do it the smart way in Laravel 5.2?
Use "bail" attribute. It will stop validation after first failure.
$this->validate($request, [
'title' => 'bail|mimes:jpg,png,pdf,doc,docx|validate_file',
]);
https://laravel.com/docs/5.2/validation#validation-quickstart search for "bail"
I have a main form that includes a number of sub-forms. One of the sub-forms contains a pair of date fields for entering a date range. I have created the entity classes and the form classes, and have updated services.yml appropriately.
The form renders fine. The problem is that the date fields are not being validated when the form is submitted. I can leave them blank or put anything in them that I like and I never get a validation error. I've tested validation of a date field in the top-level form and it worked as expected.
For testing I created a simple form and sub-form. The main test form has two fields: a text field and a sub-form field. The sub-form has two fields, a date field and a check box field.
As for the real case, I've created the entity and form classes and updated services.yml. The form displays fine. The date field fails to generate any errors when the form is submitted with an invalid date.
I have tried specifying validation with annotations in the entity classes, a constraints attribute in the $builder->add() method call, and both at the same time ;-)
The current add() call for the date field looks like this:
...
->add( 'date',
'date',
[
'attr' => [ 'placeholder' => 'a date (mm/dd/yyyy)' ],
'error_bubbling' => true,
'format' => 'MM/dd/yyyy',
'html5' => false,
'input' => 'datetime',
'invalid_message' => 'Invalid date (use mm/dd/yyyy)',
'label' => false,
'widget' => 'single_text',
'constraints' =>
[
new NotBlank(),
new Type( '\DateTime' )
]
] )
...
Suggestions?
Environment:
- PHP V5.5.9
- Symfony V2.7.4
- Twig V1.21.2
When you add SubFormType to MainForm do the following to validate sub forms:
$builder->add('sub_form', new SubFormType, array(
'constraints' => array(
new Valid()
));
I hope this helps :)
In addition to adding a Valid() constraint to the sub-form field in the main form, it comes down to the error_bubbling attributes.
The fields in the sub-form need to be set error_bubbling true to move any errors up to the sub-form field in the main form.
The sub-form field in the main form needs to set error_bubbling false to associate any sub-form errors with the sub-form field.
Through the use of a debugger and judicious {{ dump() }} tags, I finally realized that the sub-form errors were being added to the main form's global collection of errors.
I'm using cake 2.3.8 version. I have a registration form where users can enter in a username and password in the form.
My model validation looks like:
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
),
'alphanumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Usernames must only contain letters and numbers.'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
) );
Now the weird thing is in my site when I enter in a username with space in it, the validation is displayed twice. But when I use the Family registration form and enter a username with space in it, the validation error only displays once. Does anyone know what could be the issue?
Generally this is because validation is triggered twice. What exactly causes it to be triggered twice is pretty hard to tell without seeing more code (especially involved controllers, components, behaviours and models).
Check whether you are maybe calling Model::validates() manually, additionally to validation that is triggered by the models save operation, or maybe you are even calling it twice manually.
It could also be triggered by a third party component or a behaviour or whatever... you'll need to do some debugging.
In my case it happened because in the controller:
I did a $this->Model->save() which returned false (first
validation)
Then, to show validation error I did
$this->Model->invalidFields() which validates again the fields
(second time) and returned the message
To fix it I changed $this->Model->invalidFields() to $this->Model->validationErrors to get the error message
I also got this issue once. But, in my case I found out that, though I had written validate code in the Model, I validated it again in the controller because of which it was showing double validation errors. If you have done the same, then remove
$this->Model->validates()
from controller.
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'),
));