访问关联模型中的验证规则 (CakePHP)
您好,我一直在尝试从主控制器访问关联模型的验证规则。它似乎不起作用,我也找不到任何关于此的参考。这是我想要做的:
用户控制器片段:
$this->User->TalentProfile->set( $this->data );
if ($this->User->TalentProfile->validates()) {
//it always validates and doesn't seem to see model's validation rules
}
TalentProfile 模型片段:
var $validate = array (
'first_name' => array(
'maxLength' => array(
'rule' => array('maxLength', 20),
'message' => 'Can not be longer than 20 characters.',
'last' => true
),
'first_name_not_empty' => array(
'rule' => 'notEmpty',
'message' => 'This field is required',
'last' => true
),
),
);
我还尝试重新包装数组,我的想法是模型名称可能会与验证混淆:
$this->User->TalentProfile->set( array('TalentProfile',$this->data) ) );
我也尝试过这个希望,但结果仍然相同:
$this->loadModel('TalentProfile');
$this->TalentProfile->set( $this->data) );
有是我所缺少的东西。请帮忙!谢谢
编辑:
我试图保存表单数据,这在 SQL 中给了我空白。我认为这可能是一个比我想象的更简单的问题。
Hi I have been trying to access the validation rules of an associated model from my main controller. It doesn't seem to be working and I haven't been able to find any reference about this. Here is what I am trying to do:
User Controller snip:
$this->User->TalentProfile->set( $this->data );
if ($this->User->TalentProfile->validates()) {
//it always validates and doesn't seem to see model's validation rules
}
TalentProfile Model snip:
var $validate = array (
'first_name' => array(
'maxLength' => array(
'rule' => array('maxLength', 20),
'message' => 'Can not be longer than 20 characters.',
'last' => true
),
'first_name_not_empty' => array(
'rule' => 'notEmpty',
'message' => 'This field is required',
'last' => true
),
),
);
I have also tried rewrapping the array my thought being that the model name could be screwing with the validation:
$this->User->TalentProfile->set( array('TalentProfile',$this->data) ) );
I have also tried this hoping but still same results:
$this->loadModel('TalentProfile');
$this->TalentProfile->set( $this->data) );
There is something I am missing. Please help! Thanks
EDIT:
I tried to save the form data which is giving me blanks in the SQL. I think this might be a simpler problem than I thought.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在
TalentProfile
模型中设置 $validate 数组,您需要以下形式的数据:关于其工作原理的一些解释:
在调用
$this->User-> TalentProfile->validates()
和$this->TalentProfile->validates()
您正在使用TalentProfile
模型中的验证方法。这意味着 CakePHP 将根据该模型中的验证规则来验证您的输入数据,因此它期望在数组中设置TalentProfile
键(但如果没有设置,则会安静地消失)。在您的
$validates
数组中,您已为名为first_name
的键设置了验证规则。 CakePHP 将此键用作从表单输入的字段名称。现在,您将输入作为
fname
而不是first_name
。您的输入也与User
模型关联,而不是与TalentProfile
关联。如果您进一步解释一下您要对表格执行的操作,我可以提供更多帮助。
By setting the $validate array in the
TalentProfile
model, you require data to be of the form:A little explanation on how this works:
In the call
$this->User->TalentProfile->validates()
and$this->TalentProfile->validates()
you are using the validation methods in theTalentProfile
model. This means that CakePHP is going to validate your input data against the validation rules in that model, so it expects theTalentProfile
key to be set in the array (but dies down quietly if it's not).In your
$validates
array, you have set up validation rules for a key calledfirst_name
. CakePHP takes this key as a field name that is being input from a form.Right now, you are taking inputs as
fname
instead offirst_name
. Your inputs are also associated with theUser
model, and notTalentProfile
.If you explain further what you're trying to do with your form, I can be of more help.