cakephp 一个表单,多个模型,不显示一个模型的验证消息
Posted
技术标签:
【中文标题】cakephp 一个表单,多个模型,不显示一个模型的验证消息【英文标题】:cakephp one form, multiple models, not displaying one model's validation messages 【发布时间】:2011-06-08 02:04:21 【问题描述】:我有一个注册表单,我正在用户表和身份表中创建一条记录(用户有许多身份)
表格是这样的
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Register'); ?></legend>
<?php
echo $this->Form->input('Identity.name');
echo $this->Form->input('Identity.surname');
echo $this->Form->input('User.username');
echo $this->Form->input('User.pass');
echo $this->Form->input('User.pass_confirm', array('type' => 'password'));
echo $this->Form->input('Identity.email');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
我收到 User.*
字段的所有验证错误消息,但 Identity.*
字段显示时没有消息。
screenshot
验证规则:
身份:
var $validate = array(
'name' => array(
'notempty' => array(
'rule' => 'notempty',
'required' => true,
'message' => 'Your name is required.'
)
),
'surname' => array(
'notempty' => array(
'rule' => 'notempty',
'required' => true,
'message' => 'Your surname is required.'
)
),
'email' => array(
'validateEmail' => array(
'rule' => 'validateEmail',
'required' => true,
'message' => 'The email seems invalid.'
),
'notempty' => array(
'rule' => 'notempty',
'message' => 'You have to enter an email address.'
)
),
);
用户:
var $validate = array(
'pass' => array(
'required' => array(
'rule' => array('custom','/^.*[0-9].*$/i'),
'message'=>'Password must contain numbers'),
'length' => array(
'rule' => array(
'minLength',8),
'message' => 'Password must be at least 8 characters long')
),
'pass_confirm' => array(
'required' => array(
'rule' => 'notempty',
'message' => 'You have to confirm the password'
),
'length' => array(
'rule' => 'validatePassword',
'message'=>'Your passwords don\'t match!' )
),
'username' => array(
'unique' => array(
'rule' => 'validateUniqueUsername',
'message' => 'Username is already taken, please choose a different one.'
),
'notempty' => array(
'rule' => 'notempty',
'message' => 'You have to choose a username.'
)
),
);
【问题讨论】:
【参考方案1】:hasMany
模型的字段应该是数组(与父模型结合时),见.0
在字段名称之间添加
echo $this->Form->input('Identity.0.name');
echo $this->Form->input('Identity.0.surname');
...
echo $this->Form->input('Identity.0.email');
【讨论】:
你在控制器里做$this->User->saveAll($data);
吗?
谢谢,现在它工作正常;] 我不知道为什么它做了几秒钟的事情,但最后没关系;]
你好@ish希望你做得很好我有一个问题你能看到我发布的这个问题***.com/questions/51120021/…【参考方案2】:
我在this page 上找到的一个答案是关于同样的问题。解决方案是将 validate 属性添加到 saveAll
if($this->Member->saveAll($this->data, array('validate'=>'only')))
//your custom save function
【讨论】:
以上是关于cakephp 一个表单,多个模型,不显示一个模型的验证消息的主要内容,如果未能解决你的问题,请参考以下文章