How can I conditionally disable client-side validation of some fields ?
Here is the example of what I need to do:
It does not work because: client side validation will not pass when user clicks resent button on the second (confirmation screen). It would say that e-mail is required even if there is no field for it. How can I disable javascript validating email before the confirm page gets send to user ?
Example ASP.NET MVC page
<%if (Model.Confirm == false){ %>
<% using (Html.BeginForm()) { %>
Your email: <%: Html.EditorFor(m => m.Email) %>
Your name: <%: Html.EditorFor(m => m.Name) %>
<input type="submit" value="Send Email" />
<% } %>
<%} else{ %>
The e-mail was send! You can write your name and click button to resend it.
<% using (Html.BeginForm()) { %>
Your name: <%: Html.EditorFor(m => m.Name) %>
<input type="submit" value="Resend me email again" />
<% } %>
<% } %>
In the model both Email
and Name
are required field to enforce client side validation.
Example controller actions
public ActionResult Index(){
return new MyModel {Confirm = false;}
}
[HttpPost]
public ActionResult Index(MyModel model){
if(DataHelper.isKnown(model.Name)){
//send e-mail to the address corresponding to name in database
}
if(!ModelState.IsValid) {
return View(model);
}
//Send e-mail to address in email
//store pair name->email to database
model.Confirm = true;
return View(model);
}
Why don't you use 2 different views? You don't have to redirect. I think you are violating the SRP here. That view is serving more than one purpose.
public ActionResult SubmitEmail(Model model)
{
if(!emailWasSent)
return View("FirstView");
else
return View("ConfirmationView");
}
That way the first view can have client-side validation and the second view can not opt in for it and do as it wishes.