Data annotation validation for multiple model properties - asp.net-mvc

I'm currently using MVC data annotations for validating my form and it looks quite easy.
Now, I have a particular situation:
IF(checkbox1value = checked), then validate if textbox is not empty. If textbox is empty, then show validation error message.
How do I do this? Both checkbox and textbox are available as my model properties (bool and string respectively).
Is it possible to do this via a custom validation?

You can use a custom validator or IValidateableOject
There's a good explanation of both options here.
From the link.
If you need to support client validation, then a custom validator is what you want.
If your validation is strictly server side and you want to validate a number of business rules at once then use IValidateableObject.

I had to switch to Foolproof validation, which offered this functionality through it's RequiredIfTrue validator. I also really liked Fluent Validation since it was really flexible, and would also easily offer this functionality. Foolproof client-side support is provided for each of it's validators, where the boundary between client and server validation with Fluent Validation is not as easily distinguishable.

Related

Is a Binder the only way to have automatic handling for a data-entry field being required in Vaadin 8 layout?

In Vaadin 8, the BinderBuilder::asRequired lets us define a Binder where a field is known to be required on a layout. If data is missing, the layout indicates to the user that the field needs to have data entered. This is great functionality, and smartly designed.
But using a Binder can be overkill for very small forms or dialog boxes. For one thing, we must define a data class to interact with the binder which can seem silly for a little form.
➙ Is there any other way to tap into Vaadin’s automatic handling of a required field without using a Binder?
In the Community Articles section of the manual, there is a page, Mark required fields as such. That page shows TextField as having setRequired and setRequiredError methods. But this seems incorrect. I can find no such methods on the latest TextField JavaDoc.
But using a Binder can be overkill for very small forms or dialog boxes. For one thing, we must define a data class to interact with the binder which can seem silly for a little form.
Yes. This is somewhat true. Thus I implemented FieldBinder tool. Which makes possible to use similar validator - converter chain as with Binder (it replicates the same API for applicable parts) with single field without Bean. Also it has the same facilities to handle validation status changes, uses same way to show required value, or validation error as Binder.
https://vaadin.com/directory/component/fieldbinder

Mvc3 a potentially dangerous request.form ... Solution without disabling Validation

I'd rather not disable validation on my form. I feel like I'm not taking advantage of MVC's benefits if I do that. Is there a way to regex the contents of a property submitted in a form before mvc gets its hands on it? I simply want to allow only alphanumerical values and some symbols but still leave protection on. Do I have to disable with [ValidateInput(false)]??
You can create custom validation attributes and apply them to your entire model class, or individual properties.
http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx

ASP.NET MVC - Dynamic RegEx Validator

In our application our customers are allowed to define a couple of reference fields, and the validation for these reference fields by way of a regular expression. The Validation Regex is saved into our database.
What is the best way to apply this regex as validation for an MVC text box field?
In addition to checking server-side when the value's submitted, you can use remote validation which would create a round-trip to test the current value against logic stored on the server (or in this case in a database).
Have a look at the RemoteAttribute and that MSDN article above (which includes an example).

How to make validation optional for a complex type in asp.net mvc?

I want to display an editor for a type User. User contains a field Address of type Address. I made an editor template for the type Address so that it is reusable.
I don't want the field Address to be required for creating a user. But some fields are required for Address, for example country, state etc.
I want to validate Address if I receive any data for it, if I don't receive anything, then I don't want to return any validation error to the UI for Address. I would return only validation errors for User then.
What would be the best way to do this?
Thanks,
I used some code from Simon J Ince of Microsoft. He has it here on his blog. It also has client side validation which is also nice. It has a RequiredIf attribute that only makes a field required if another field has a certain value. Just being able to see how he implemented it helped me figure out how to do some of this stuff by myself and I even retrofitted it to allow multiple values.
You might want to look into a Custom Model Binder for your User type. That way you can choose to override the validation of the Address item inside a User.
I have found that more complex custom validation is easier with FluentValidation. The documentation provided is very helpful, and you will be able to achieve your validation goal with this open source validator.

Combine DataAnnotations Validation with complex business rules

I understand annotating class properties with the basic required and minimum length and getting all the benefits of the asp.net mvc server side and client side validation.
However does anyone have a link that shows how you combine this 'base' validation with more complex business rules. How would I run business rule functions, such as for example, has the customer ordered anything in the last year (database hit required) and still use the same DataAnnotation and mvc validation plumbing?
Goal : Don't want two ways of generating and outputting validation methods.
From http://msdn.microsoft.com/en-us/library/dd901590%28VS.95%29.aspx:
To create customized validation checks, you can either create a class that derives from the ValidationAttribute class or create a method that performs the validation check and reference that method when applying the CustomValidationAttribute to the data member. When you create a class that derives from ValidationAttribute, override the IsValid method to provide the logic for your customized validation check.
There appears to be example code there.
Data Annotation run before your action is invoked. Then, regardless whether the validation succeded or not, the action is still called. If the DA detected invalid data, your ModelState will be invalid.
Once here, you can still do any validation you want, for your business rules, as you would normally do without the data annotation, if you want to. In your action, you can add errors to the ModelState even if the Data Annotation validation passed.
In this case, you add your errors with ModelState.addError, and those errors are added to any error provided by the DA. So in your View it doesn't matter where the error comes from.
Or, if your rules are general, you can write your own annotation tags. The Data Annotation thing is distributed with its source, so you have full control on it.
You could use VAB (Application Validation Block) from the Enterprise Library 5 of Microsoft that actually based on the DataAnnotations class but u do your complex bussiness logic very easily through configuration...
i'd suggest you check it out...
Have a look at following article, where you can use DataAnnotations Multiple Times On Same Field, Compare N number of properties and N number of values....
http://www.codeproject.com/KB/validation/MultipleDataAnnotations.aspx

Resources