1

I am new to MVC framework. I have 2 radio buttons and i want at least one should be checked. I want to do it through custom validation, need help?

The field is "usWorkPermit":

My View code:

Do you have US Work Visa ?
@Html.RadioButtonFor(m => m.usWorkPermit,"True") Yes I have
@Html.RadioButtonFor(m => m.usWorkPermit, "False") No I don't

My Model code:

[UsWorkPermitValidation]
public Boolean usWorkPermit { get; set; }

My Controller code:

[HttpPost]
public ActionResult Index(Models.JobApplication jobApplication)
{
    if (ModelState.IsValid)
    {
    }
    return View();
}

I created the custom validator which is incomplete. I want to ask how i can force at least one radio button to be checked ??

public class UsWorkPermitValidation : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        // check at least one radio button is checked.
    }
}

How to get values of each radio buttons on my custom validator? If i know then then i can easily do the checking ??

yogihosting
  • 5,494
  • 8
  • 47
  • 80
  • did you try using `[Required]`? – Syed Ali Taqi Apr 14 '17 at 11:54
  • [Required] not working on my case. – yogihosting Apr 14 '17 at 11:57
  • 1
    as per the definition of "radio button", you don't want "at least one" radio button to be checked, you want "exactly one" radio button to be checked. In other words, in a group of radio buttons, it is nonsensical to have two radio buttons checked. That's what checkboxes are for. – Mike Nakis Apr 14 '17 at 12:01
  • @yogihosting: try [this](http://stackoverflow.com/questions/7247748/mvc3-validation-require-one-from-group) – Syed Ali Taqi Apr 14 '17 at 12:02
  • @MikeNakis i want the radio button to be checked(there will be only one which can be checked, also during the page loading for the first time none of them is checked). I want to make sure user has to check them and i want to do it through custom validation. – yogihosting Apr 14 '17 at 12:04
  • basically i want to know how to loop through the values of each radio button in my custom validation class - "UsWorkPermitValidation" ? any help? – yogihosting Apr 14 '17 at 12:11

2 Answers2

2

You can try this, It worked for me

@Html.RadioButtonFor(m => m.usWorkPermit, true , new {@required = "true"}) //Yes I have
@Html.RadioButtonFor(m => m.usWorkPermit, false) //No I don't

According to me, No need to use custom validator only for this

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24
1

Why wasting time when people have already wasted time to make this easy for us. Try using unabtrusive js instead.

class UsWorkPermitValidation
{
  [Required(ErrorMessage = "Your error message")]
  public Boolean usWorkPermit { get; set; }
}

In your view.cshtml

@Html.Label("Yes, I've")
@Html.RadioButtonFor(model => model.usWorkPermit, "Yes")
@Html.Label("No, I don't Have")
@Html.RadioButtonFor(model => model.usWorkPermit, "No")

And add the reference of the following jquery scripts files within bundle/current view.

<script src="~/Scripts/jquery-3.1.1.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

Here is the snap of its run.

enter image description here

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24