3

出于某种原因,我在保存多条记录时没有收到任何验证错误。我可以使用 print_r($user->errors()); 但它们不会像添加单个用户时那样自动注入表单中。根据文档“在使用 newEntity()、newEntities() 时自动完成保存前的验证实体。” 我不确定是否有特定的方法来设置表单以使其返回多个记录的验证,或者您是否必须在模型中对具有索引的输入进行特殊验证或什么?

看法:

<div class="page-wrap">
    <div class="form">
        <h1>Join Now</h1>
        <?php 
            echo $this->Form->create(null, ['controller' => 'users', 'action' => 'addMultiple']);   
            echo $this->Form->input('1.full_name');
            echo $this->Form->input('1.username');  
            echo $this->Form->input('1.email');
            echo $this->Form->input('1.password');
            echo $this->Form->input('1.password_confirmation', array('type' => 'password'));

            if ($current_user['role'] === 1 && isset($logged_in)) {
                echo $this->Form->input('1.role', ['type' => 'select', 'options' => ['1' => 'Admin', '2' => 'Editor', '3' => 'Author', '4' => 'Reader'], 'default' => '4']);
            }   
            echo $this->Form->input('2.full_name');
            echo $this->Form->input('2.username');  
            echo $this->Form->input('2.email');
            echo $this->Form->input('2.password');
            echo $this->Form->input('2.password_confirmation', array('type' => 'password'));

            if ($current_user['role'] === 1 && isset($logged_in)) {
                echo $this->Form->input('2.role', ['type' => 'select', 'options' => ['1' => 'Admin', '2' => 'Editor', '3' => 'Author', '4' => 'Reader'], 'default' => '4']);
            }

            echo $this->Form->button(__('Sign Up'));
            echo $this->Form->end();
        ?>
    </div>
</div>

控制器:

public function addMultiple()
{
    $users = $this->Users->newEntities($this->request->data());
    if ($this->request->is('post')) {
        foreach($users as $user) {
            if( empty($this->request->session()->read('Auth.User')) || $this->request->session()->read('Auth.User.role') !== 1 ) {
                $user->role = 4;
            }
            if ($this->Users->save($user)) {
                $this->Flash->success(__('You have been added.'));
            } else {
                $this->Flash->error(__('You could not be added. Please, try again.'));
            }
        }
    }
}

桌子:

公共函数初始化(数组$config){父::初始化($config);

    $this->table('users');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');

    $this->hasMany('MembershipOrders', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('MembershipOrders', [
        'foreignKey' => 'affiliate_token',
        'joinType' => 'INNER'
    ]);
}
public function validationDefault(Validator $validator)
{
    $validator
        ->notEmpty('full_name', 'A full name is required')
        ->add('full_name', 'notBlank', [
            'rule' => 'notBlank',
            'message' => __('A full name is required'),
        ]);

    $validator
        ->notEmpty('username', 'A username is required')
        ->add('username', [
            'notBlank' => [
                'rule' => 'notBlank',
                'message' => __('A username is required'),
                ]
        ]);

    $validator
        ->notEmpty('email', 'An email is required')
        ->add('email', [
            'notBlank' => [
                'rule' => 'notBlank',
                'message' => __('A full name is required'),
                ],
            'unique' => [
                'rule' => 'validateUnique',
                'provider' => 'table',
                'message' => __('That email has already been used.'),
                ]
        ]);

    $validator
        ->notEmpty('old_password', 'You must enter your old password is required')
        ->add('old_password', 'notBlank', [
            'rule' => 'notBlank',
            'message' => __('Your old password is required'),
        ]);

    $validator
        ->notEmpty('password', 'A password is required')
        ->add('password', 'notBlank', [
            'rule' => 'notBlank',
            'message' => __('A full name is required'),
        ]);

    $validator
        ->notEmpty('password_confirmation', 'Password confirmation is required')
        ->add('password_confirmation',
            'comareWith', [
                'rule' => ['compareWith', 'password'],
                'message' => 'Passwords do not match.'
        ]);

    $validator
        ->notEmpty('role', 'A role is required')
        ->add('role', 'inList', [
            'rule' => ['inList', ['1', '2', '3', '4']],
            'message' => 'Please enter a valid role'
        ]);
    return $validator;
}
4

2 回答 2

2

您可以使用“addNestedMany()”:http ://book.cakephp.org/3.0/en/core-libraries/validation.html#nesting-validators

于 2015-10-29T23:44:02.383 回答
-1

You have to pass the entity object to the Form->create(... function, instead of passing nullas the following:

echo $this->Form->create($user, .....
于 2015-12-10T09:19:11.173 回答