[英]Server-side RequiredFieldValidators not working
I have a form representing a survey that is dynamically generated based on some database configuration. 我有一个代表调查的表格,该表格是根据某些数据库配置动态生成的。 I have a custom server control for rendering the survey ( SurveyRenderer
) which contains custom server controls for rendering questions ( QuestionRenderers
). 我有一个用于呈现调查的自定义服务器控件( SurveyRenderer
),其中包含用于呈现问题的自定义服务器控件( QuestionRenderers
)。 I dynamically add RequiredFieldValidators
for questions if they are flagged as being required. 如果问题被标记为RequiredFieldValidators
,我会动态添加RequiredFieldValidators
来提问。 I add these validators to the SurveyRenderer
's control collection. 我将这些验证器添加到SurveyRenderer
的控件集合中。
The gist of the code... 代码要点...
// In SurveyRenderer.CreateChildControls()...
foreach (QuestionRenderer questionRenderer in questionRenderers)
{
if (questionRenderer.Question.IsRequired)
{
Controls.Add(CreateRequiredValidator(questionRenderer));
}
}
The client-side validation works fine -- if someone has omitted a required question, the validators catch it and the form doesn't validate. 客户端验证工作正常-如果有人省略了必填问题,验证程序将捕获该问题,并且表单将不进行验证。 However if I turn off JavaScript and submit an invalid form, the validators do not seem to work. 但是,如果我关闭JavaScript并提交了无效的表单,则验证程序似乎无法正常工作。
On the server-side I am calling Page.Validate()
and checking Page.IsValid
in the submit button click event handler. 在服务器端,我正在调用Page.Validate()
并在“提交”按钮单击事件处理程序中检查Page.IsValid
。 Despite submitting a form where required questions have been left blank - something that would be caught client-side - on the server-side Page.IsValid
remains True
. 尽管提交了表单,但在服务器端Page.IsValid
保留了必填问题(在客户端可能会遇到Page.IsValid
留为True
。
// In SurveyPage.aspx...
public void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
// Always get here, even though the form is not valid and would
// have been caught client-side...
}
}
Should I be adding the validators to the Page's Control collection, rather than the SurveyRenderer? 我应该将验证器而不是SurveyRenderer添加到Page的Control集合中吗? How come it works on the client-side but not server-side? 它如何在客户端而非服务器端工作?
UPDATE : My QuestionRenderer is annotated with: 更新 :我的QuestionRenderer带有以下注释:
[ValidationProperty("IsValid")]
And the IsValid get method is like so: IsValid的get方法如下所示:
// QuestionRenderer.IsValid
public bool IsValid
{
get
{
EnsureChildControls();
if (Question.IsRequired && QuestionIsNotAnswered())
{
return false;
}
return true;
}
}
If I set a breakpoint and step through, I can see that QuestionRenderer.IsValid
is being fired OK. 如果设置断点并逐步执行,则可以看到QuestionRenderer.IsValid
被解雇了。 It is returning false
when it should do. 它应该做的时候返回false
。 If I go fine-grained and call in btn_submitClick: 如果我细粒度并调用btn_submitClick:
// In SurveyPage.aspx...
public void btnSubmit_Click(object sender, EventArgs e)
{
foreach (IValidator validator in Page.Validators)
{
validator.Validate(); // this calls through to QuestionRenderer.IsValid, which returns false...
bool valIsValid = validator.IsValid; // yet this is set to True
}
}
So validator.IsValid is true, even though the call to QuestionRenderer.IsValid returns false. 因此,validator.IsValid为true,即使对QuestionRenderer.IsValid的调用返回false。 So maybe I haven't wired something up correctly? 所以也许我没有正确地接线? Is using [ValidationProperty("IsValid")]
not enough? 使用[ValidationProperty("IsValid")]
还不够吗?
actually, validation uses Page.Validators
where all the validators are stored (the actual routine is quity tricky) - so it does not matter, where you add them. 实际上,验证使用Page.Validators
存储所有验证器的位置(实际例程有些棘手)-因此,将它们添加到何处都没有关系。
source of BaseValidator
BaseValidator
来源
protected internal override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.Validators.Add(this);
}
i would leave them in th view, as you could use object sender
-parameter (which represents the validator) to get the associated control ... 我将它们保留在视图中,因为您可以使用object sender
-parameter(代表验证器)来获取关联的控件...
i believe, your CreateChildControls
- which does the attaching of the validators - is called to late, so it misses the validation phase ... could you maybe try to call EnsureChildControls
in OnLoad
-event, to see if it changes something? 我相信,你CreateChildControls
-这也验证程序的安装-被调用来晚了,所以它忽略了验证阶段......你能也许尝试调用EnsureChildControls
中OnLoad
-event,看它是否改变了什么? another chance might be, that your validators are not visible or disabled... 另一个机会可能是您的验证器不可见或被禁用...
EDIT 编辑
according to your edits, i would encourage you to use a CustomValidator
- a RequiredFieldValidator
will return true
on each case (property is true
or false
), because it is not empty :) 根据您的编辑,我鼓励您使用CustomValidator
- RequiredFieldValidator
将在每种情况下返回true
(属性为true
或false
,因为它不为空:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.