Tuesday, September 25, 2012

[required] Conditional Validation with Data Annotations in ASP.NET MVC4

When the user want some fields are Required some time’s . But some times  user don’t  Required some fields for example
In registration time  we want username and password field but  change password time only we want password field .In this situation we would create two models .To avoid this problem using removing the fields from the model state, it won’t be invalid when they are missing.  This method is simple and avoids adding additional dependencies.
    public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
         }
    public ActionResult ChangePassword()
    {
        ModelState.Remove("Password");
        return View();
    }


  abiruban



3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;

    namespace TemplateProject.Entity.DTO
    {
    [DataContract]
    public class RecoverPassword : IValidatableObject
    {

    [Required(AllowEmptyStrings = false, ErrorMessage = "Please enter type ")]
    [DataMember(EmitDefaultValue = false)]
    public string Type { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public string PhoneNumber { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public Data Data { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public string ResponseCode { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public string Message { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public string LoginSessionKey { get; set; }
    [Required(AllowEmptyStrings = false, ErrorMessage = "Please enter Value.")]
    [DataMember(EmitDefaultValue = false)]
    public string Value { get; set; }
    [DataMember(EmitDefaultValue = false)]
    public string Email { get; set; }

    public IEnumerable Validate(ValidationContext validationContext)
    {
    if (!string.IsNullOrEmpty(Value) && (string.Compare(Type, "email", true) == 0))
    {
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    if (!reg.IsMatch(Value))
    {
    yield return new ValidationResult("Invalid email.");
    }

    }

    else if (!string.IsNullOrEmpty(Value) && (string.Compare(Type, "phonenumber", true) == 0))
    {
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^[+]?([\\d]{3}(-| )?[\\d]{3}(-| )?[\\d]{4}|[\\d]{5,12}|}|[(][\\d]{3}[)](-| )?[\\d]{3}(-| )?[\\d]{4})$");
    if (!reg.IsMatch(Value))
    {
    yield return new ValidationResult("Invalid phonenumber.");
    }

    }

    }

    }
    }

    You can see below link also:-
    http://stackoverflow.com/questions/2417113/asp-net-mvc-conditional-validation

    ReplyDelete