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

发布于 2024-10-11 16:42:48 字数 3088 浏览 2 评论 0原文

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

该表单看起来像这样

<?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.* 字段显示时不带消息。

截图

验证规则:

身份:

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

I have a registration form and I am creating a record in both User and Identity tables (a user hasMany identities)

the form looks like this

<?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));?>

I get all the validation error messages for User.* fields but Identity.* fields are shown without messages.

screenshot

validation rules:

Identity:

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

User:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-10-18 16:42:48

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

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

hasMany model's fields should be as array (when combined with parent model), see .0 added between field names

echo $this->Form->input('Identity.0.name');
echo $this->Form->input('Identity.0.surname');
...
echo $this->Form->input('Identity.0.email');
蛮可爱 2024-10-18 16:42:48

我在此页面上找到了很多关于同样的问题。解决办法是给saveAll添加validate属性

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

One answer I found was on this page much about the same problem. The solution was to add the validate attribute to saveAll

if($this->Member->saveAll($this->data, array('validate'=>'only'))){
   //your custom save function
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文