php - How to get validation when saving multiple records with newEntities() CakePhp 3.x? -


for reason not getting validation errors when saving multiple records. can grab errors using print_r($user->errors()); not automatically injected form when adding single user. according docs "validating entities before saving done automatically when using newentity(), newentities()." not sure if there specific way set form make return validation multiple records or if have special validation in model inputs have indexes or what?

view:

<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> 

controller:

public function addmultiple() {     $users = $this->users->newentities($this->request->data());     if ($this->request->is('post')) {         foreach($users $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 not added. please, try again.'));             }         }     } } 

table:

public function initialize(array $config) { parent::initialize($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 required')         ->add('full_name', 'notblank', [             'rule' => 'notblank',             'message' => __('a full name required'),         ]);      $validator         ->notempty('username', 'a username required')         ->add('username', [             'notblank' => [                 'rule' => 'notblank',                 'message' => __('a username required'),                 ]         ]);      $validator         ->notempty('email', 'an email required')         ->add('email', [             'notblank' => [                 'rule' => 'notblank',                 'message' => __('a full name required'),                 ],             'unique' => [                 'rule' => 'validateunique',                 'provider' => 'table',                 'message' => __('that email has been used.'),                 ]         ]);      $validator         ->notempty('old_password', 'you must enter old password required')         ->add('old_password', 'notblank', [             'rule' => 'notblank',             'message' => __('your old password required'),         ]);      $validator         ->notempty('password', 'a password required')         ->add('password', 'notblank', [             'rule' => 'notblank',             'message' => __('a full name required'),         ]);      $validator         ->notempty('password_confirmation', 'password confirmation required')         ->add('password_confirmation',             'comarewith', [                 'rule' => ['comparewith', 'password'],                 'message' => 'passwords not match.'         ]);      $validator         ->notempty('role', 'a role required')         ->add('role', 'inlist', [             'rule' => ['inlist', ['1', '2', '3', '4']],             'message' => 'please enter valid role'         ]);     return $validator; } 

you can use 'addnestedmany()' : http://book.cakephp.org/3.0/en/core-libraries/validation.html#nesting-validators


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -