php - cakephp 一种形式,多个模型,不显示一个模型的验证消息

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

最佳答案

hasMany 模型的字段应该是数组(当与父模型结合时),参见 .0 添加在字段名称之间

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

关于php - cakephp 一种形式,多个模型,不显示一个模型的验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4673383/

相关文章:

php - 任何有效的 php 调试器,PDT?

php - mysql select where子查询慢

if条件下的javascript表单validation_error

php - 是否可以在 CakePHP 的同一 View 中对两个不同的模型进行分页和排序?

cakephp - CakePHP 中的基本文件操作?

php - 在 cakephp 的 xml 帮助程序中找不到 xml 类

php - google analytics api php,如何访问以某个url开头的所有登陆页面的总 session 数

php - 错误报告级别的数值

html - 使用 jQuery 手动触发表单验证

validation - 关于软件设计: Where I must check parameters?