I have a list of Pair of radio buttons (Yes/No):
Q1.(Y)(N)
Q2.(Y)(N)
Q3.(Y)(N)
Q4.(Y)(N)
and I have one property in my model
public string MedicalExplanation { get; set; }
My goal is to make Explanation required if any of the radio button has been set to true.
My first try was to use [Required] but it does not handle conditions.
Then I decided to use third party tool like MVC Foolproof Validation
I used it like this:
[RequiredIf("Q1", true, ErrorMessage = "You must explain any \"Yes\" answers!")]
Now the problem is I don't know how to make it required if any of the other Q2, Q3, Q4 is checked.
Please advice
In your ViewModel, create a bool property like this:
public bool IsMedicalExplanationRequired
{
get
{
return Q1 || Q2 || Q3 || Q4;
}
}
Then, use your RequiredIf attribute like this:
[RequiredIf("IsMedicalExplanationRequired", true, ErrorMessage = "You must explain any \"Yes\" answers!")]
UPDATE:
If your Q1 - Q4 properties are of type bool?, just change the IsMedicalExplanationRequired property like below:
public bool IsMedicalExplanationRequired
{
get
{
return Q1.GetValueOrDefault() || Q2.GetValueOrDefault() || Q3.GetValueOrDefault() || Q4.GetValueOrDefault();
}
}
This is how I did it:
First I created a custom validation attribute which gets a string array of fields to check passed in:
public class ValidateAtLeastOneChecked : ValidationAttribute {
public string[] CheckBoxFields {get; set;}
public ValidateAtLeastOneChecked(string[] checkBoxFields) {
CheckBoxFields = checkBoxFields;
}
protected override ValidationResult IsValid(Object value, ValidationContext context) {
Object instance = context.ObjectInstance;
Type type = instance.GetType();
foreach(string s in CheckBoxFields) {
Object propertyValue = type.GetProperty(s).GetValue(instance, null);
if (bool.Parse(propertyValue.ToString())) {
return ValidationResult.Success;
}
}
return new ValidationResult(base.ErrorMessageString);
}
}
Then I use it like this (I am using resource files to localize my error messages):
[ValidateAtLeastOneChecked(new string[] { "Checkbox1", "Checkbox2", "Checkbox3", "Checkbox4" }, ErrorMessageResourceType=typeof(ErrorMessageResources),ErrorMessageResourceName="SelectAtLeastOneTopic")]
public bool Checkbox1{ get; set; }
public bool Checkbox2{ get; set; }
public bool Checkbox3{ get; set; }
public bool Checkbox4{ get; set; }
It is only actually setting the error on the first checkbox. If you are using the built in css highlighting to highlight fields in error you will need to modify this slightly to make it look right, but I felt this was a clean solution which was reusable and allowed me to take advantage of the support for resource files in validation attributes.
Related
This is my first post.
I need string array validation such like below.
[Required(ErrorMessage = "Content name is required")]
public string[] ContentName { get; set; }
I found a post which has the same situation.
This answer and following code helped me so much and I could solve my problem.
public class StringArrayRequiredAttribute : ValidationAttribute
{
protected override ValidationResult IsValid (object value, ValidationContext validationContext)
{
string[] array = value as string[];
if(array == null || array.Any(item => string.IsNullOrEmpty(item)))
{
return new ValidationResult(this.ErrorMessage);
}
else
{
return ValidationResult.Success;
}
}
}
And
[StringArrayRequired(ErrorMessage = "Content name is required")]
public string[] ContentName { get; set; }
But now I found an another problem. This validation works only server side. I wish I could have a client validation too. Because it would make my client much happier!!
So would you give me a nice way for this? Waiting for your answers!!
Thank you for your help.
I write a short code in my view.
$.validator.addMethod('stringarrayrequired', function (value, element, params) {
let array = value;
if (array == null) {
return false;
}
for (var i = 0; i < array.length; i++) {
if (!array[i]) {
return false;
}
}
return true;
}, '');
$.validator.unobtrusive.adapters.add("stringarrayrequired", function (options) {
options.rules["stringarrayrequired"] = "#" + options.element.name.replace('.', '_'); // mvc html helpers
options.messages["stringarrayrequired"] = options.message;
});
(Sorry, I'm not fluent in JS...)
And I add id="stringarrayrequired" to my . But it doesn't work.
I also checked html code. When I click the submit button, there should be a class="input-validation-error" or "valid" in input tag for "ContentName", but I couldn't find both of them.
I still need more info... Anyone help?
I found a way to solve my problem.
(I changed property name ContextName to Selection)
[Display(Name = "Selections")]
public Selection[] Selections { get; set; }
public class Selection
{
[Required(ErrorMessage = "SelectionItem is empty")]
public string SelectionItem { get; set; }
}
I use Selections for , SelectionItem for and .
As you know, [Required] attribute doesn't work for string[]. So I created a Selection class and changed string[] to Selection[], and applied [Required] attribute to string.
I know that this's not a clean way... I'll use foolproof or something.
Add the following javaScript code in your view:
$.validator.addMethod('stringarrayrequired', function (value, element, params) {
// here return true or false based on checking the input value
},'');
$.validator.unobtrusive.adapters.add("stringarrayrequired", function (options) {
options.rules["stringarrayrequired"] = "#" + options.element.name.replace('.', '_'); // mvc html helpers
options.messages["stringarrayrequired"] = options.message;
});
I would like to use a DataAnnotation Attribute that tells the user that he must select one checkbox of the two following checkbox groups. My model is:
//group T
public bool T0 {get;set;}
public bool T1 {get;set;}
public bool T2 {get;set;}
//group P
public bool P0 {get;set;}
public bool P1 {get;set;}
The user must select at least one of the T properties, and one of the P properties. IS there something that do that on some customized dataannotations or i need to create one from beggining?
Thanks
You may use Fluent Validation
[FluentValidation.Attributes.Validator(typeof(CustomValidator))]
public class YourModel
{
public bool T0 { get; set; }
public bool T1 { get; set; }
public bool T2 { get; set; }
}
public class CustomValidator : AbstractValidator<YourModel>
{
public CustomValidator()
{
RuleFor(x => x.T0).NotEqual(false)
.When(t => t.T1.Equals(false))
.When(t => t.T2.Equals(false))
.WithMessage("You need to select one");
}
}
Here 2 solutions you can choice anyone to use.
1.Use action rule:
a) Set “False” as the check box’s default value.
b) Add following action rule to that check box field.
If check_box_field = “False”
Set check_box_field (itself) = “true”
Then this field can’t be unchecked anymore.
2.Use validation rule. Add following validation rule to that check box field.
If check_box_field = “False”
Show ScreenTip and Message: “Need to be checked”
With this validation rule, if check box has not selected, validation error will be displayed and stop prevent form submission. Let me know if you have any question.
I figure out this solution that worked as I expected but i cant display the error messages on the view.
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class AtLeastOnePropertyAttribute : ValidationAttribute
{
public AtLeastOnePropertyAttribute(string otherProperties)
{
if (otherProperties == null)
{
throw new ArgumentNullException("otherProperties");
}
OtherProperties = otherProperties;
}
public string OtherProperties { get; private set; }
public string OtherPropertyDisplayName { get; internal set; }
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, OtherPropertyDisplayName ?? OtherProperties);
}
public override bool IsValid(object value)
{
var typeInfo = value.GetType();
var propertiesToGet = OtherProperties.Split(',');
var values = propertiesToGet.Select(propertyName => (bool) typeInfo.GetProperty(propertyName).GetValue(value)).ToList();
return values.Any(v => v);
}
public override object TypeId
{
get
{
return new object();
}
}
}
And in the DTO class:
[AtLeastOneProperty("T0 ,T1,T2", ErrorMessage = #"At least one field should be marked as true.")]
[AtLeastOneProperty("P0,P1", ErrorMessage = #"At least one field should be marked as true.")]
public class TestDTO
{
//Properties
}
Here is another way with FluentValidation.
RuleFor(x => x).Must(x =>
{
if (!x.checkbox1 &&
!x.checkbox2 &&
!x.checkbox3)
{
return false;
}
return true;
})
.WithMessage("Please select at least one checkbox.");
You could also run a foreach loop on a list of checkboxes and verify all boxes are not checked and throw the error.
Can you create custom data annotations for the model that can be read inside the T4 template for the View like property.Scaffold is read? I would like to add data annotation parameters like Scaffold based on which I would build the view.
Thank you
I wrote a blog post on the solution I came up with for MVC5. I'm posting it here for anyone who comes along:
https://johniekarr.wordpress.com/2015/05/16/mvc-5-t4-templates-and-view-model-property-attributes/
Edit: In your entities, decorate property with custom Attribute
namespace CustomViewTemplate.Models
{
[Table("Person")]
public class Person
{
[Key]
public int PersonId { get; set;}
[MaxLength(5)]
public string Salutation { get; set; }
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(50)]
public string Title { get; set; }
[DataType(DataType.EmailAddress)]
[MaxLength(254)]
public string EmailAddress { get; set; }
[DataType(DataType.MultilineText)]
public string Biography { get; set; }
}
}
With this Custom Attribute
namespace CustomViewTemplate
{
[AttributeUsage(AttributeTargets.Property)]
public class RichTextAttribute : Attribute
{
public RichTextAttribute() { }
}
}
Then create a T4Helper that we'll reference in our template
using System;
namespace CustomViewTemplate
{
public static class T4Helpers
{
public static bool IsRichText(string viewDataTypeName, string propertyName)
{
bool isRichText = false;
Attribute richText = null;
Type typeModel = Type.GetType(viewDataTypeName);
if (typeModel != null)
{
richText = (RichTextAttribute)Attribute.GetCustomAttribute(typeModel.GetProperty(propertyName), typeof(RichTextAttribute));
return richText != null;
}
return isRichText;
}
}
}
So, this is how you do it.
Follow this tutorial on how to create a custom attribute http://origin1tech.wordpress.com/2011/07/20/mvc-data-annotations-and-custom-attributes/
To read this attribute values in the T4 scaffolding templates, first add the template files as described here http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx
Then, for example, open List.tt from the AddView folder. This template creates the Index view.
Go to the end of the template file and find the definition for class ModelProperty. Add your property value to it ( public string MyAttributeValue { get; set; }
Now go a bit down in the List.tt and find bool Scaffold(PropertyInfo property) method. You will need to add your own attribute property reader. This method, for the above mentioned tutorial, would be:
string OptionalAttributesValueReader(PropertyInfo property){
foreach (object attribute in property.GetCustomAttributes(true)) {
var attr = attribute as OptionalAttributes ;
if (attr != null) {
return attr.style;
}
}
return String.Empty;
}
Then find the method List GetEligibleProperties(Type type) at the bottom of the file. Add your reader to it like this:
...
IsForeignKey = IsForeignKey(prop),
IsReadOnly = prop.GetSetMethod() == null,
Scaffold = Scaffold(prop),
MyAttributeValue = OptionalAttributesValueReader(prop)
When you want to use and read this attribute you can do it like the Scaffold property is used in the List.tt
List<ModelProperty> properties = GetModelProperties(mvcHost.ViewDataType);
foreach (ModelProperty property in properties) {
if (property.MyAttributeValue != String.Empty) {
//read the value
<#= property.MyAttributeValue #>
}
}
Since these classes are defined in my project, I had to add my project dll and namespace to the top of the List.tt:
<## assembly name="C:\myProjectPath\bin\myMVCproject.dll" #>
<## import namespace="myMVCproject.CustomAttributes" #>
If your model changes and you need to find these new changes in the scaffolding, you need to rebuild your project.
Hope anyone looking for the solution will find this useful. Ask if there is anything unclear.
This is how I did it in MVC 5. I did this a long time ago and I may be forgetting stuff, I'm just copy/pasting what I see in my modified templates.
I needed a way to set the order of properties in (for example) the create/edit views or in the list view table. So I created a custom attribute OrderAttribute with an integer property Order.
To access this attribute in the T4 templates I modified the file ModelMetadataFunctions.cs.include.t4. At the top I added one method that retrieves the Order value set in the attribute from a PropertyMetadata object, and another method to simply order a list of PropertyMetadata items by that order:
List<PropertyMetadata> GetOrderedProperties(List<PropertyMetadata> properties, Type modelType) {
return properties.OrderBy<PropertyMetadata, int>(p => GetPropertyOrder(modelType, p)).ToList();
}
int GetPropertyOrder(Type type, PropertyMetadata property) {
var info = type.GetProperty(property.PropertyName);
if (info != null)
{
var attr = info.GetCustomAttribute<OrderAttribute>();
if (attr != null)
{
return attr.Order;
}
}
return int.MaxValue;
}
Finally, in the List template for example, I have added a part where I call the GetOrderedProperties method:
var typeName = Assembly.CreateQualifiedName("AcCtc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", ViewDataTypeName);
var modelType = Type.GetType(typeName);
var properties = ModelMetadata.Properties.Where(p => p.Scaffold && !p.IsPrimaryKey && !p.IsForeignKey && !(p.IsAssociation && GetRelatedModelMetadata(p) == null)).ToList();
properties = GetOrderedProperties(properties, modelType);
foreach (var property in properties)
{
//...
}
Unfortunately I needed the name of the project to be able to create a Type object which I needed to get the attributes from. Not ideal, perhaps you can get it some other way but I couldn't manage it without this string including all the version stuff.
I would like to use the built-in validation features as far as possible. I would also like to use the same model for CRUD methods.
However, as a drop down list cannot be done using the standard pattern, I have to validate it manually. In the post back method, I would like to just validate the drop down list and add this result to ModelState so that I don't have to validate all the other parameters which are done with Data Annotation. Is it possible to achieve this?
I may be mistaken about the drop down list, but from what I read, the Html object name for a drop down list cannot be the same as the property in the Model in order for the selected value to be set correctly. Is it still possible to use Data Annotation with this workaround?
Thanks.
You can use the addModelError
ModelState.AddModelError(key,message)
when you use that, it will invalidate the ModelState so isValid will return false.
Update
after seeing the comment to #Pieter's answer
If you want to exclude an element from affecting the isValid() result, you can use the ModelState.Remove(field) method before calling isValid().
Another option is to inherit IValidatableObject in your model. Implement its Validate method and you can leave all other validation in place and write whatever code you want in this method. Note: you return an empty IEnumerable<ValidationResult> to indicate there were no errors.
public class Class1 : IValidatableObject
{
public int val1 { get; set; }
public int val2 { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var errors = new List<ValidationResult>();
if (val1 < 0)
{
errors.Add(new ValidationResult("val1 can't be negative", new List<string> { "val2" }));
}
if (val2 < 0)
{
errors.Add(new ValidationResult("val2 can't be negative", new List<string> { "val2" }));
}
return errors;
}
}
EDIT: After re-reading the question I don't think this applicable to this case, but I'm leaving the answer here in case it helps someone else.
You cannot manually set the ModelState.IsValid property but you can add messages to the ModelState that will ensure that the IsValid is false.
ModelState.AddModelError();
yes, you can achieve this (also you will use the same model for CRUD methods) :
Example MODEL
public class User
{
public virtual int Id{ get; set; }
public virtual Role Role { get; set; }
}
public class Role
{
[Required(ErrorMessage = "Id Required.")]
public virtual int Id { get; set; }
[Required(ErrorMessage = "Name Required.")]
public virtual string Name { get; set; }
}
Example VIEW with validation on the dropdownlist
#Html.DropDownListFor(m => m.Role.Id, (SelectList)ViewBag.gRoles, "-- Select --")
#Html.ValidationMessageFor(m => m.Role.Id)
CONTROLLER: clearing the required (but not needed here) fields
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Creedit(User x)
{
x.Role = db.RoseSet.Find(x.Role.Id);
if (x.Role != null)
{
ModelState["Role.Name"].Errors.Clear();
}
if (ModelState.IsValid)
{
// proceed
}
else
{
// return validation error
}
}
Might be more recent methods, since this is an old post, but this might help future readers.
One can set a field to valid with this two methods:
ModelState.ClearValidationState("Password");
ModelState.MarkFieldValid("Password");
Need to use both because the second one without the first one it gives an error stating that the state is already marked.
To set a field to invalid, just use ModelState.AddModelError() method as already referred.
I'm curious how I can dynamically set a model's validation attributes. For instance, I often have Views where certain fields should be required when a user is in a certain role, but not required when a user is in another role. I would like both the server-side and client-side validation to be set accordingly.
Wouldn't something like this work for you for the server side?
public class RequiredForRoleAttribute : ValidationAttribute
{
public string Role { get; set; }
public override bool IsValid(object value)
{
return !Roles.IsUserInRole(Role) || (value != null && !string.IsNullOrEmpty((string)value));
}
}
And an example usage will be;
[RequiredForRoleAttribute(Role = "Admins", ErrorMessage = "Phone number is required for members of the admin role.")]
public string PhoneNumber { get; set; }
Now for the client side of things,
Your going to have to register it for remote validation as described at the following link; http://forums.asp.net/t/1559594.aspx/1
Hope you get it,
Chris