Vuelidate with data nesting and validation groups - validation

Im using Vue.js in my project. I have a form made with Vue Form Wizard and with Vuelidate to validate the fields. At certain point of the form, the user will be able to add multiples addresses, so I created a array named 'addresses' into the data of my component.
addresses: [
{
street: ''
//there will be more itens here in the future
}
]
Now, in order to validate the fields, I created a validation group where Im gonna add this field, but the point is, I dont know how to 'link' the field (that is inside my array) into the validation group.
In Vuelidate Documentation, there is a section Data Nesting, but i didn't find how to use it with validation groups.
FIDDLE

It's an old question but if I understand it correctly then I guess you can just create a custom validator on the addresses array, and in the validator validate single address objects.
validations: {
addresses: {
customValidator: customValidatorFunction
}
},
Then the customValidatorFunction is gonna get the whole array of addresses, and you can use it to validate all/any object you want in your custom way.

Related

What to check when validator is not called in symfony 3?

I cannot find general checklist - what to check when it is not called. Can you write it?
For example code snippets where validator is not being called:
$fieldOptions['constraints'] = [
new NotBlank($constraintOptions)
];
$builder->add(
$builder
->create($formField->getId(), EntityType::class, $fieldOptions)
->addModelTransformer(
new EntityCollectionToArrayTransformer($this->registry, $fieldOptions['class'])
)
);
One of things to check - validation groups. Try commenting out any validation groups, so it would work as default. When form adds a collection of forms, those subforms validator constrains also have to have same group. https://symfony.com/doc/3.4/validation/groups.html
When validating just the User object, there is no difference between the Default group and the User group. But, there is a difference if User has embedded objects. For example, imagine User has an address property that contains some Address object and that you've added the Valid constraint to this property so that it's validated when you validate the User object.
If you validate User using the Default group, then any constraints on
the Address class that are in the Default group will be used. But, if
you validate User using the User validation group, then only
constraints on the Address class with the User group will be
validated.
In other words, the Default group and the class name group (e.g. User)
are identical, except when the class is embedded in another object
that's actually the one being validated.

How to share validation rules of several models and a large ajax form that holds them

I have a large ajax managed form with several steps. Obviously, I need to validate data after each submit step. The final validation results in the creation of several business objects.
Some business objects will receive all of their fields, others will not. For example, the customer who will not receive the billing address (it will be requested at the time the billing takes place). Indeed, the form being very long, I do not want to overload it by adding elements not immediately useful.
Some form partial concern only few informations
hold by my model so i can't instanciate model just for validation of little % of his attributes.
In principe, with October, the validation is done at the level of the models by adding the trait validator which will make it possible to use the generic Laravel validation functionalities (perhaps i'm wrong here)
But I'm not sure I can use on the models validation because some will be incomplete at the end of the form filling.
It is necessary to mutualize the validation of all these data without making a gas factory with duplicate validation code but how, where?
For the moment I am on the idea of ​​making a trait added to my object component which handles all my ajax handlers but I am not excited because it does not correspond to my idea to mutualize the validation
Perhaps using behavior instead of traits are better
https://octobercms.com/docs/services/behaviors
to be continued ...
If I had to do it, I'll use the Validation service after each submit step.
https://octobercms.com/docs/services/validation
You can store the info in a session and use them at the final stage.
$myinput = post('myinput');
$validator = Validator::make(
[
'myinput' => $myinput
],
[
'myinput' => 'required|min:8'
]
);
if ($validator->fails()) {
// The given data did not pass validation
}
// store in session
Session::push('myform.myinput', $myinput);
Session::push('myform.step', 2);
PS: on est deux francophones qui se causent en anglais :)

How to run validation on a slice of a redux-form FieldArray state

I am running something very similar to the example here:
http://redux-form.com/6.0.5/examples/fieldArrays/
Say I want to add a list of members and a list of hobbies under each member, like in the example above.
What is different from the example above is, I want to also be able to validate each member, before I am able to add additional members or hobbies for that member.
Similarly, I also want to be able to validate each hobby, before I can add another hobby.
In other words, I want to be able to treat each entry in the FieldArray as a form and each add button as a submit function for that form.
I would have a validateMember and validateHobby validation functions that I would run against the respective FieldArray entries.
I am not aware of any feature of redux-form that allows running synchronous validation on a subset of the form's state. Alternatively, I also cannot treat everything as a form as then I would lose all the nesting (i.e. which hobby belongs to each address).
Any ideas and suggestions are greatly appreciated!

How I can validate only some validation groups based on some fields in the form itself in Symfony2

I have a big form organized in some validations groups. For every group in the form there is a corresponding checkbox which tell the server to save group data.
When the user post the form, I need to validate only validation groups whose correspond the checked checkboxes because some of their "sub" fields are required, but only if you activate the group. Otherwise the validator must ignore the required fields.
Actually I do that in my controller. I skip the Symfony's normal validation cycle and manually I validate every field checking for the group activation checkbox.
How I can move this validation logic inside the Form class or in a specific Constraint class used by the entity?
EDIT:
As said below is possibile in symfony 2.1, for now i solved:
$request = $this->get('request');
// myEntity knows the business logic to chose validation groups
$myEntity->collectValidationGroups($request);
$form = $this->createForm(new MyEntityType(), $myEntity);
If you are using Symfony 2.1 then you can set validation group based on submitted data. Check this section.
There is another possibilty than the one offered by 2.1.
You can set the validation_groups attribute on the form using $builder->getData():
// inside buildForm method of a form type:
$builder->setAttribute('validation_groups', $builder->getData()->getValidationGroups());

Validate single form field only in Symfony2

I'm looking for a way to validate just a single field (object property) against the constraints specified in the annotations of a particular entity.
The goal is to send an AJAX request after the "onBlur" event of a form field, asking the server to validate this single field only, and - depending on the response - add a small "OK" image next to this field or an error message.
I don't want to validate the whole entity.
I wonder what's the best approach for this problem? Thanks for any tips.
The Validator class has the validateProperty method. You can use it like this:
$violations = $this->get('validator')->validateProperty($entity, 'propertyName');
if (count($violations)) {
// the property value is not valid
}
Or, if the value is not set in the entity, you can use the validatePropertyValue method:
$violations = $this->get('validator')->validatePropertyValue($entity, 'propertyName', $propertyValue);
if (count($violations)) {
// the property value is not valid
}
Have a look at validation groups. I think this is what you need. You could add a group "ajax" or and just adding the one constraint to it. Then tell the validator to use that group. THe symfony2 docs have an example included.

Resources