问题描述
我如何有条件地禁用一些领域的客户端验证?
下面是什么,我需要做的例子:
- 用户写的名字和地址并提交。
- 两者都验证服务器和客户端。如果验证通过电子邮件被发送给定的地址和(姓名,地址)对存储。
- 当显示的响应同一页面(我不想重定向!)这个时候确认。用户可以重新输入他们的名字,并提交。在这种情况下,电子邮件将被重新发送到对应于给定名称的地址。
它不起作用,因为:当用户点击重新发送第二(确认屏幕)按钮客户端验证将无法通过。它会说,即使是它没有字段的电子邮件是必需的。我怎样才能禁用javascript验证电子邮件确认页面之前被发送到用户?
为例ASP.NET MVC页
<若%(Model.Confirm == FALSE){%GT;
&下;使用%(Html.BeginForm()){%GT;
你的电子邮件:<%:Html.EditorFor(M = GT; m.Email)%GT;
您的名字:<%:Html.EditorFor(M = GT; m.Name)%GT;
<输入类型=提交值=发送电子邮件/>
<%}%GT;
<%}其他{%GT;
电子邮件是发送!你可以写你的名字,然后点击按钮来重新发送。
&下;使用%(Html.BeginForm()){%GT;
您的名字:<%:Html.EditorFor(M = GT; m.Name)%GT;
<输入类型=提交值=重新发送电子邮件我再次/>
<%}%GT;
<%}%GT;
在模型中均为电子邮件
和名称
需要现场执行客户端验证。
例控制器动作
公众的ActionResult指数(){
返回新为MyModel {确认= FALSE;}
} [HttpPost]
公众的ActionResult指数(为MyModel模型){
如果(DataHelper.isKnown(model.Name)){
//发送电子邮件到相应的数据库来命名地址
}
如果(!ModelState.IsValid){
返回查看(模型);
}
//发送电子邮件的电子邮件解决
//商店对名字 - >电子邮件数据库
model.Confirm = TRUE;
返回查看(模型);
}
你为什么不使用2个不同的看法?你不必重定向。我认为你在这里违反了SRP。这种观点是服务多个目的。
公众的ActionResult SubmitEmail(型号模型)
{
如果(!emailWasSent)
返回视图(的firstView);
其他
返回视图(ConfirmationView);
}
这样,第一种观点可以有客户端验证,第二视图不能选择为它做,因为它的愿望。
How can I conditionally disable client-side validation of some fields ?
Here is the example of what I need to do:
- user writes name and address and submits.
- Both are validated both server and client side. If validation passes e-mail is send to given address and (name, address) pair is stored.
- as a response same page is displayed (I don't want redirect!) this time with confirmation. User can retype their name and submit. In such case e-mail will be resend to the address that corresponds to given name.
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.
这篇关于在ASP.NET MVC某些领域禁用客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!