Different validation on a field depending on user selection - validation

For example lets say I have the following validator:
Acme\BlogBundle\Entity\Person:
properties:
ID:
- NotBlank: ~
However the kind of validation for ID depends on the property IDType that is selected by a user. Which some IDS can be blank and some cannot. Not only that but other types of ID needs other types of validation. Is this possible to do? Or should I have one property for each ID?

The Callback validation constraint is very suitable in this case. It lets you define complex validation logic and add errors to any form field you see fit.

Related

How can redux-form validation based on multiple fields be implemented?

Redux Form validation examples only include individual field validations.
Having a list of a certain number of fields that are the same, how can I model a validation that requires at least one of the inputs to be a valid email address?
For example, for the given form made up of the following fields:
User 1,
User 2,
User 3,
Special User 4
How can I write a validation function that requires Special User 4 to always be set, and either one of the first 3 users?
Your synchronous validation function is given all the values of the form. Your errors must correspond to a field key, or you can use the generic _error key, which will result in this.props.error being populated.
Does that make sense?

Apply Sitecore validation rule to a field type?

I have created a validation rule in Core DB, and I have created a custom Sitecore field called 'Single Select Treelist'. I want to apply this validation rule to all 'Single Select Treelist' fields in all templates in my Sitecore instance.
I know we can individually apply the validation rule to each template field of type 'Single Select Treelist'. Is there a way to achieve this automatically for all 'Single Select Treelist' fields without any custom coding?
I see the list of field type validation rules available in Master:
How are these connected to the respective field types in Core?
Please advise.
If you edit the field type item there is a validation section that contains fields to set validation rules for the Quick Action Bar, the Validate Button, the Validator Bar and finally Workflow.
Once you set the validation rules, depending on which field you set, Sitecore will display the validation messages in different ways. See this blog post for examples: http://sitecoreworld.blogspot.com/2014/12/sitecore-validation-examples.html
The link between the Field Type Validation Rule and the actual Field Type seems to be based simply on the name of the Field Type.
Just add a new item with the template "Field Type Validation Rules" in the Validation Rules/Field Types folder that you show in your picture. Give it the exact same name as the field type you want it to validate. Select the rules you need inside the Validation Rules section of that new object.
For example, I just needed to add some validation to the Single-Line Text type, as you can see in the image. We struggled with it for a little while because we missed the dash.
Image - Sitecore Tree with new Field Type Validation

UniqueEntity validation for OneToMany related entities

I have the following validations defined for ProductType, ProductTypeAttributes (OneToMany):
Bike\ProductBundle\Entity\ProductType:
properties:
name:
- NotBlank: ~
productTypeAttributes:
- Valid: ~
Bike\ProductBundle\Entity\ProductTypeAttribute:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [attribute, productType]
errorPath: attribute
message: 'Attributes must be different for the same product type.'
There is a unique key for ProductTypeAttributes: attribute,productType.
I am using embedded forms (collection type) for ProductTypeAttributes with the possibility to add/remove items. The validation seems to work only for the already existent records in the db, happening only if I am adding a new related entity which will trigger the unique key violation for a record.
The problem is the validation doesn't work when adding two completely new related entities, with the same attribute/productType. In this case I get the "duplicate entry" exception.
So the validation checks only through db records using default findBy method, but not against newly added records themselves for duplicates.
Any way to overcome this?
Yes, but not through the default UniqueEntity validator. You'll have to manually create a second validator that works on the entire ProductType entity and validates that none of the new (unsaved) Attributes are the same.
Check out Symfony´s cookbook on how to make your own validator:
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

Cakephp one model different form validation

I have a model name "User", their I added a validation for login. But I need to validate registration page also. Fields for both forms are different.
Can someone please tell me how to manage different form validation with 1 model.
You can validate as many fields as you want inside your User model, it does not matter in which View or in which form you input them.
So just add the fields from your registration page to the User's $validate inside your User model.
If all forms share similar fieldnames but require different validation rules you can use:
http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
If the duplicate fields validate the same on all forms you can just add them all to the Model, it will only validate the ones present on the form.
Remember to NOT use 'required' => true, setting this key to true will make the field always required and it has to be present in the data array even if it's not on your form

Data Validation in MVC

Suppose i have a 'View' for filling up form for renting a DVD , as per MVC architecture , either of 'Controller' or 'Model', who is supposed to validate the form data?
Thanks
You validation should be in Model section of MVC.
As models have various fields, only models can know what combination of inputs make that model valid. It's not just about whether a field is blank, or the input of that field matches some pattern, but sometimes this is a combination of field inputs, or the model's relationship to other models that determine the valid state.
All 3 are usually involved in the validation process if you follow the typical flow.
The model defines validation attributes such as the required or stringlength attributes. The controller checks the validation state of the model via ModelState.IsValid and makes decisions accordingly. The view may additional provide client-side validation for those same attributes. Don't rely solely on js to validate the form.
My suggestion would be to validate in the view with some form of validation binding, and then again in the model before persisting to any data store.

Resources