cakephp一个表单,多个模型,不显示一个模型的验证消息

时间:2011-01-12 19:55:27

标签: php validation cakephp model cakephp-1.3

我有一个注册表单,我在用户和身份表中创建了一条记录(用户拥有多个身份)

表格看起来像这样

<?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.'
            )
        ),
    );

2 个答案:

答案 0 :(得分:12)

hasMany模型的字段应为数组(与父模型结合使用时),请参阅字段名称之间添加的.0

echo $this->Form->input('Identity.0.name');
echo $this->Form->input('Identity.0.surname');
...
echo $this->Form->input('Identity.0.email');

答案 1 :(得分:5)

我发现的一个答案是关于同一问题的this page。解决方案是将validate属性添加到saveAll

if($this->Member->saveAll($this->data, array('validate'=>'only'))){
   //your custom save function
}