Ref. this Microsoft's official video:
http://www.asp.net/web-api/videos/getting-started/custom-validation
I downloaded the code and run it. It's fine.
Then, I remove all the client validation attributes(data-val-*) from the html file. It didn't work fine. I could not see the validation messages on the web page.
My question is how to regular the server side validation messages and how to display them as client-side validation.
Why would you delete the validation attributes? That's exactly what gets you the validation messages. To change the validation tests, you need to set the appropriate validation attributes on the model properties, e.g.,
[Required]
public string Genre { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
As described in this post on ASP.NET MVC 4 Model Validation.
Related
I have a model of name Student
public class Student
{
[Required]
public string Name { get; set; }
public string Number { get; set; }
public string Email { get; set; }
}
My question is that, does form is submitted and then validations are checked or there is another mechanism that post hidden form values to the server side validations ?
Generally in web development, validation occurs both client side and server side.
You want to scrub the forms for any unwanted data or characters before they are submitted, and you want to check them / scrub them again server side to ensure nothing is being passed that is unwanted.
I'm in the process of upgrading an ASP.NET WebForms application to ASP.NET MVC. One of the features on the existing application is it has a dynamic form generator. Each form has steps (using the wizard control that are dynamically added in the Wizard_Init event) and validation (using the validation controls).
I already have some static MVC forms where I use data annotation attributes on my model properties and then use the in-built unobtrusive validation to validate the form.
Now say I have the following model:
public class Form {
public string Name { get; set; }
public IList<Step> Steps { get; set; }
}
public class Step {
public string Name { get; set; }
public IList<Field> Fields { get; set; }
}
public class Field {
public string Name { get; set; }
public bool IsRequired { get; set; }
}
Obviously the fields are dynamic so I can't use the data annotation attributes. Also I only wish to validate each step. One option I thought was to render all the steps within the form and show/hide the appropriate step using javascript as they click next/previous. But again I can't see how I can add validation to this. Another option is to load in each step using Ajax but again I'm struggling with this concept.
I'd really appreciate it if someone could help. Ideally I'd like advice on the best way to achieve this rather than linking to an old third party library unless it has been built since ASP.NET MVC 3. Thanks
I think the best thing to do in your case is to create your own validation script. Its not hard, nor complex and you can address only the things necessary. So in my opinion just use js to hide, show and validate by your own script. (Just only validate the not hidden fields. )
If the fields are dynamic, doesnt mean you cannot have validation.
you can make validation dynamic, by breaking models down into smaller pieces and add validation to each smaller model.
I'm working in a content development scenario where entities are likely to be created in an incomplete state and remain incomplete for some time. The workflow involved is extremely ad-hoc, preventing me from using a more structured DDD approach.
I have a set of validation rules which must be satisfied at all times, and a further set of validation rules which must be satisfied before an entity is "complete".
I'm already using the built-in ASP.NET MVC validation to validate the former. What approaches could I use to capture the latter?
For example:-
public class Foo
{
public int Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
[Required] // A Bar must be owned by a Foo at all times
public int FooId { get; set; }
public virtual Foo Foo { get; set; }
[Required] // A Bar must have a working title at all times
public string WorkingTitle { get; set; }
public bool IsComplete { get; set; }
// Cannot use RequiredAttribute on Description as the
// entity is very likely to be created without one,
// however a Description is required before the entity
// can be marked as "IsComplete"
public string Description { get; set; }
}
There are different approaches you could use:
Have your model implement the IValidatableObject interface and perform conditional validation without using data annotations.
Write a custom validation attribute that will perform the conditional validation logic of the Description property based on the value of the IsComplete property.
Use Mvc Foolproof which already has the RequiredIf validation attribute defined for you so that you don't need to write it yourself.
Use FluentValidation.NET which allows you to express your validation rules in a fluent manner. Writing conditional validation rules using this library is not only elegant but very easy. It integrates nicely with ASP.NET MVC and also allows you to easily unit test your validation logic in isolation.
Personally I would go with the FV.NET but you could use any of the other approaches if it better suits your needs.
Can I restrict user to enter only numbers using DataAnnotations?
Below is the property which I have in my Model
[Display(Name = "Fiscal Year")]
[Required(ErrorMessage = "Fiscal Year is required")]
public int FiscalYear { get; set; }
Below is the definition which I am using in .chtml (razor view):
#Html.TextBoxFor(model => model.project.FiscalYear)
I want to allow users only to enter numbers. Any suggestions?
Thanks,
Balaji
Get the Nuget package called DataAnnotationsExtensions.. then use it like below:
[Integer(ErrorMessage="This is needs to be integer")]
public int CustomerId { get; set; }
It will put the proper the validation in place assuming that you have jQuery validation plugin and unobtrusive validation enabled.
You can use RegularExpression attribute
I have a class:
class Item
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
I have a view where I want objects for the class above created using inputs (so I have a textbox for Name and a date select type thing for Date). However, I want thev user to be able to click a link and through jquery/javascript another textbox and date select will be added to the form, and this can happen unlimited times.
How can I bind this to a model so that I can return it to my action method? Ideally the model would be something like:
class MyModel
{
public string AProperty { get; set; }
public List<Item> Items { get; set; }
}
Apologies for the poor wording, struggling to describe what I want but I think this should get the point across.
You want to use a client-side template and then return JSON to your controller. If you are using MVC 3, JSON model binding is built-in, but in MVC 2 you need to set up your own binder. There is an example here.
I recommend using KnockoutJS for your client side. It's very simple for working with dynamic collections and very well documented. You can see an example similar to what you're trying to do here as well as in the previous link.