Reputation: 2539
I have a Model
which have both phone number and email in MVC 4
. It's something like registration. So user can provide either email or cellphone number or both of them. However I can't make a alternative required field of them.
My Model is like
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
[UsernameValidator(ErrorMessage = "Only Letters and numbers")]
[System.Web.Mvc.Remote("IsUserExitst", "Account", HttpMethod = "POST", ErrorMessage = "This user name already exists. Try a new one.")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[MaxLength(50)]
[EmailOrPhoneValidator(ErrorMessage = "Invalid Phone number")]
[Display(Name = "Phone number")]
[System.Web.Mvc.Remote("IsMobOrEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Phone number or Email already exists. Try a new one.")]
public string MobileNo { get; set; }
[Required]
[MaxLength(50)]
[EmailAddress(ErrorMessage = "Invalid Email address")]
[Display(Name = "Email")]
[System.Web.Mvc.Remote("IsEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Email already exists. Try a new one.")]
public string Email { get; set; }
}
Here I want to use Email
and PhoneNo
alternatively. I want to write a class which works as Custom required field. I have gone through this Questions,
RequiredIf
is seems the exact suggestion of my question, but I so tired to try that solution. I am not capable to understand how to import the .dll in my project. and it's developed in Mvc 3Upvotes: 3
Views: 1118
Reputation: 1825
I would go with solution 2 and create a RequiredIf validation attribute. No need to import any DLLs since you can just create the class yourself. Here is an example of what the attribute could look like. It's from a RequiredIfTrue attribute I use in my own code and I exchanged the validation logic to look for empty values
public class RequiredIfEmpty : ValidationAttribute
{
private string _fieldName;
public RequiredIfEmpty(string fieldName)
{
_fieldName = fieldName;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var property = validationContext.ObjectType.GetProperty(_fieldName);
if (property == null)
{
throw new ArgumentException("No property with the name " + _fieldName + " found.");
}
string fieldValue = (string)property.GetValue(validationContext.ObjectInstance, null);
if (String.IsNullOrWhiteSpace(fieldValue))
{
return (value != null && !String.IsNullOrWhiteSpace((string)value) ? ValidationResult.Success : new ValidationResult(ErrorMessage));
}
else
{
return ValidationResult.Success;
}
}
}
In your ViewModel you can use the attribute like any other attribute but you have to specify the name of the alternative property
[RequiredIfEmpty("Email", ErrorMessage = "Phone number invalid")]
[MaxLength(50)]
[Display(Name = "Phone number")]
[System.Web.Mvc.Remote("IsMobOrEmailExists", "Account", HttpMethod = "POST", ErrorMessage = "This Phone number or Email already exists. Try a new one.")]
public string MobileNo { get; set; }
Upvotes: 2