Laravel 5.2 different validation rules based on another field value inside request class - laravel

Here is what I'm trying to accomplish, I have a form that has a dropdown field of types, lets say type1, type2, type3, and I have a textfield, I want to make the rule for the textfield depend on the type, for example if the type is type1 then the validation rule for the value of the textfield should be 'email' if it is type2 then the validation rule is 'number', etc.
I have read the Validation documentation on the Laravel site, I know this section is what I'm trying to accomplish, but I like to keep validation logic separate from the controller, and it doesn't say how to call the sometimes method from within the request class.

Related

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

Throwing a message when Updating/Validating Part contents/data

Crosspost: https://orchard.codeplex.com/discussions/455498
When updating a Part, in the DriverResult Editor, how can we manually throw a warning message when implementing custom validation?
I want to add a custom validation for some date fields, where values are dependent on some other field in the Content Item itself (dates in the part need to be between a specific date in the content item's field).
Which leads me to another question, can we set validation in model properties between one another? Like if there are two properties StartDate and EndDate, is it doable in the model to declare that end date must be after start date?
You can add model validation errors using the "updater" parameter being passed in:
updater.AddModelError("StartDate", T("Please enter a date in the future"));

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

ASP.NET MVC 3 Pattern for dynamic validation attributes including client side

My validation requirements for a the fields in a form are contained in an external table so that they can be updated without altering and rebuilding the code.
I have approximatley 100 fields with a mixture of validation requirements - range, required, regular expression and dependency on other fields. One example is date range validation. A date of birth field requires a date range which is between -10 years and -50 years of the current date.
I have read around the subject but have not identified a pattern for the complete solution.
I am using Visual Studio 2010 with MVC 3 and Entity Framework.
Any help with this would be gratefully received. Thanks in advance.
In a simple level I think you can still use the built-in Data-Annotations validation attributes to does the validation and for that you should map the validation rules stored in the table to the attributes.
I think all you have to do is create a custom model validation provider by inheriting the class ModelValidatorProvider. This class contains a single method called GetValidators that returns the collection validators for that model.
You have to implement the GetValidators method and in there you have to make a database call to get the validation rules for the model from the database (or from cache?) and convert them into ModelValidators. You could still use the built-in DataAnnotationsModelValidator to do the validations.
I would suggest you to look into the source code of DataAnnotationsModelValidatorProvider that will give you all the information. In that class what they are doing is basically iterating all the validation attributes applied to the model properties and converting them into ModelValidators through adapters and factories. In your case instead of attributes they are stored as records in tables and I don't think much work will be there.

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