我正在开发一个包含用户数据的表单,特别是电话号码字段。通常不需要电话号码,因此模型中唯一的验证规则是 usphone
规则。但是,如果用户提交此表单,则需要电话号码。我以为我可以简单地添加一个 validate
规则,设置模型并调用 validates
方法,但要么我做错了,要么它是没有按我预期的方式工作。
在我的控制器中:
# Update a few validation rules that are specific to this context
$this->Proposal->Requestor->validate['phone_number']['notempty'] = array(
'rule' => 'notEmpty',
'message' => 'Please enter a phone number so can can contact you with any questions about the work.',
'allowEmpty' => false,
'required' => true,
);
$validationErrors = array();
$this->Proposal->Requestor->set( $this->data['Requestor'] ); # $this->data['Requestor']['phone_number'] only (no other requestor data)
if( !$this->Proposal->Requestor->validates( array( 'fieldList' => array( 'phone_number' ) ) ) ) {
$validationErrors['Requestor'] = $this->Proposal->Requestor->validationErrors;
}
即使我将电话号码字段留空,也不会报告任何错误。在这种情况下,我向用户请求的唯一信息是他们的电话号码,因此请求者数据的其余部分为空,但我尝试合并其余的用户数据并我得到同样的结果。如果我删除 fieldList
选项,我会在不同的字段上收到错误,但空电话号码上仍然没有任何内容。
知道我在这里缺少什么吗?我已经在这个问题上折腾了好几个小时了,但还没有找到正确的答案。
谢谢。
I'm working on a form that contains user data, specifically a phone number field. The phone number typically isn't required so the only validation rule in the model is the usphone
rule. However, if the user is submitting this form, the phone number becomes necessary. I thought I'd be able to simply add a validate
rule on the fly, set the model and call the validates
method, but either I'm doing it wrong or it's not working the way I expected.
In my controller:
# Update a few validation rules that are specific to this context
$this->Proposal->Requestor->validate['phone_number']['notempty'] = array(
'rule' => 'notEmpty',
'message' => 'Please enter a phone number so can can contact you with any questions about the work.',
'allowEmpty' => false,
'required' => true,
);
$validationErrors = array();
$this->Proposal->Requestor->set( $this->data['Requestor'] ); # $this->data['Requestor']['phone_number'] only (no other requestor data)
if( !$this->Proposal->Requestor->validates( array( 'fieldList' => array( 'phone_number' ) ) ) ) {
$validationErrors['Requestor'] = $this->Proposal->Requestor->validationErrors;
}
No errors are reported, even if I leave the phone number field empty. In this case, the only information I'm requesting from the user is their phone number, so the rest of the Requestor
data is empty, but I've tried merging in the rest of the user data and I get the same result. If I remove the fieldList
option, I get an error on a different field, but still nothing on the empty phone number.
Any idea what I'm missing here? I've been monkeying around with this for hours now and I just haven't found the right answer.
Thanks.
发布评论
评论(2)
解决方案最终是双重的:
phone_number
字段上已有规则,强制该值成为美国电话号码。该规则还将allowEmpty
设置为true
,将required
设置为false
。我想捕获一个空值,以便可以显示特别精确的消息。allowEmpty
和required
值,并添加一个新规则,并将其last
值设置为正确。
在我的控制器操作中添加的最终更改如下所示:
我不记得是否已验证鉴于
last
值,对现有usphone
规则的更改是绝对必要的新规则,但这种组合运行良好。The solution ended up being twofold:
phone_number
field that forced the value to be a US phone number. That rule also setallowEmpty
totrue
andrequired
tofalse
. I wanted to catch an empty value so I could display a particularly precise message.allowEmpty
andrequired
values and also add a new rule with itslast
value set totrue
.The final change, added in my controller action looks like this:
I can't remember whether I verified that the change to the existing
usphone
rule was strictly necessary given thelast
value of the new rule, but this combination is working fine.您可以尝试使用多重验证行为 - http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
You could try using the Multivalidatable Behaviour - http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model