Sami
Sami

Reputation: 3976

Validation of form with two buttons and one class

I have a view and have two button in that view, i have one model. One is "Update" button and other one is "Change" button.

I want that if Update button is clicked then only First Name should be validated, and if Change button is clicked then Last name should be validated only, but on button click it is validation both fields.

This is my view:

@model MyProject.Web.Models.User
@using (Html.BeginForm("Update", "ControllerName"))
         {
            @Html.ValidationSummary()
            @Html.LabelFor(u => u.FirstName)
            @Html.TextBoxFor(u => u.FirstName)

            <button type="submit" id="btnUpdate">Update</button>
}

@using (Html.BeginForm("Change", "ControllerName"))
         {
            @Html.ValidationSummary()
            @Html.LabelFor(u => u.LastName)
            @Html.TextBoxFor(u => u.LastName)

           <button type="submit" id="btnChange">Change</button>
        }

Class implementation is:

 public class User
 {
    [Required]
    [StringLength(32, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    [StringLength(32, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
    public string LastName { get; set; }
 }

Upvotes: 0

Views: 80

Answers (1)

Daniil Grankin
Daniil Grankin

Reputation: 3933

Remove Required attributes and add custom validation.

Or just create two separate ViewModels.

public class FirstNameViewModel 
 {
    [Required]
    [StringLength(32, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
    [Display(Name = "First Name")]
    public string Value { get; set; }
}

 public class LastNameViewModel 
 {
    [Required]
    [Display(Name = "Last Name")]
    [StringLength(32, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
    public string Value { get; set; }
 }

Common view model

public class ViewModel
    {
        public FirstNameViewModel FirstNameViewModel { get; set; }

        public LastNameViewModel LastNameViewModelNameViewModel { get; set; }
    }

View

@model ViewModelDynamicAttribute.ViewModels.ComplexViewModel

@using (Html.BeginForm("Update", "Home"))
{
    @Html.ValidationSummary()
    @Html.LabelFor(u => u.FirstNameViewModel.Value)
    @Html.TextBoxFor(u => u.FirstNameViewModel.Value)

    <button type="submit" id="btnUpdate">Update</button>
}

@using (Html.BeginForm("Change", "Home"))
{
    @Html.ValidationSummary()
    @Html.LabelFor(u => u.LastNameViewModelNameViewModel.Value)
    @Html.TextBoxFor(u => u.LastNameViewModelNameViewModel.Value)

    <button type="submit" id="btnChange">Change</button>
}

Actions

public ActionResult Update(ComplexViewModel viewModel)
        {
            return null;
        }

        public ActionResult Change(ComplexViewModel viewModel)
        {
            return null;
        }

and then map viewModel on user model.

Update Custom Validation Attribute

public enum ActionMethodEnum
    {
        Change,
        Update
    }

    public class ConditionalRequiredAttribute : RequiredAttribute
    {
        public ActionMethodEnum ActionMethodEnum { get; set; }

        public string ActionMethodFieldName { get; set; }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var property = validationContext.ObjectType.GetProperty(ActionMethodFieldName);
            if (property == null)
            {
                return new ValidationResult(string.Format("{0} is unknown property", ActionMethodFieldName));
            }
            if (property.PropertyType != typeof(ActionMethodEnum))
            {
                return new ValidationResult(string.Format("{0} property has invalid type", ActionMethodFieldName));
            }

            ActionMethodEnum actionMethodEnum;
            Enum.TryParse(property.GetValue(validationContext.ObjectInstance, null).ToString(), out actionMethodEnum);
            if (actionMethodEnum != ActionMethodEnum)
            {
                return ValidationResult.Success;
            }

            return base.IsValid(value, validationContext);
        }
    }

Your model

public class User
    {
        public ActionMethodEnum ActionMethod { get; set; }

        [ConditionalRequired(ActionMethodEnum = ActionMethodEnum.Update, ActionMethodFieldName = "ActionMethod")]
        [StringLength(32, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [ConditionalRequired(ActionMethodEnum = ActionMethodEnum.Change, ActionMethodFieldName = "ActionMethod")]
        [Display(Name = "Last Name")]
        [StringLength(32, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        public string LastName { get; set; }
    }

Changes in view

@using (Html.BeginForm("Update", "ControllerName"))
{
    @Html.ValidationSummary()
    @Html.LabelFor(u => u.FirstName)
    @Html.TextBoxFor(u => u.FirstName)
    {
        Model.ActionMethod = ActionMethodEnum.Update;
    }
    @Html.HiddenFor(u => u.ActionMethod)

    <button type="submit" id="btnUpdate">Update</button>
}

@using (Html.BeginForm("Change", "ControllerName"))
{
    @Html.ValidationSummary()
    @Html.LabelFor(u => u.LastName)
    @Html.TextBoxFor(u => u.LastName)
    {
        Model.ActionMethod = ActionMethodEnum.Change;
    }
    @Html.HiddenFor(u => u.ActionMethod)

    <button type="submit" id="btnChange">Change</button>
}

Upvotes: 1

Related Questions