Published on May 11, 2015 by Jamie Munro
You've added some data validation and you want to test it out in action. The data validation can be standard data annotations, custom data attributes, or data validation implemented via an IValidatableObject
Unit testing data validation can be accomplished by creating a ValidationContext
in conjunction with the Validator
helper class. To demonstrate how this is accomplished, I will use the ValidationModel
class I created in the Removing Data Validation from MVC Controllers
. As you may recall this class contained an IValidatableObject
that ensured if the boolean FixedQuantity
was true, the MaxQuantity
property must be null.
Read more >
Tags: validation | ASP.NET | mvc | Testing | unit testing | ivalidatableobject | validationcontext
Published on May 6, 2015 by Jamie Munro
You require custom data validation that cannot be accomplished via the built-in data annotation validation attributes - or - your data validation requires access to multiple properties in your model.
Many times the easy answer is to place the validation inside of your controller. However, I'm a strong believer of placing data validation outside of your controllers and within your data model. This is commonly accomplished by adding data annotation above the properties, such as the [Required] attribute. Another common way is to create your own validation attributes.
This example will use a third way and that is to implement the IValidatableObject interface. This interface defines a Validate function that must be implemented and because it is inside of your data model it has access to all properties within your model.
Read more >
Tags: validation | ASP.NET | mvc5 | ivalidatableobject | validationcontext
Published on Mar 28, 2009 by Jamie Munro
Have you found yourself wanting to remove validation on a specific field in a specific form? Yes? Excellent, you found the right place.
If you haven't had the need for this, a great example of where you would want to accomplish this would be in a users edit form. Normally in CakePHP we would encrypt the password, especially if you are using the AuthComponent, so we wouldn't want the password prefilled on our form. Instead, we'll make it blank and place a note underneath that says "Only enter a password if you wish to change it".
The above shouldn't be difficult, however, when you go to use the form, you will get some unexpected results. I keep getting a message saying that I must enter a password, because I had setup validation on that field for the registration process. Below is a simple solution to this problem.
Read more >
Tags: CakePHP | validation