CakePHP3.5默认验证方法没有提供

时间:2017-11-09 10:40:17

标签: cakephp-3.x

在CakePHP 3.5验证方法中,在注册表单时不生成字段级错误报告。根据cakePHP文档将数据保存到数据库自动验证将发生,我已经使用了简单的默认内置验证方法,但错误没有被抛出。请帮我这个作为新的cakePHP UsersTable.php

    namespace App\Model\Table;
    use Cake\ORM\Table;
    use Cake\Validation\Validator;           
    class UsersTable extends Table{        
    public function initialize(array $config)
    {
    parent::initialize($config);
    //for table
    $this->setTable('users');
    }
    //validation rule
    function validation(Validator $validator){ 
    $validator ->requirePresence('firstname','true')
               ->notBlank('firstname', 'Please fill the First name field');
    $validator->requirePresence('lastname','true')
              ->notBlank('lastname', 'Please fill the First name field');
    $validator ->requirePresence('username','true')
               ->notBlank('username', 'Please fill the User name field');
    $validator->requirePresence('age','true')
               ->notBlank('age', 'Please fill the Age field');
    $validator->requirePresence('gender','true')
             ->notBlank('gender', 'Please fill the gender field');
    $validator->requirePresence('address','true')
              ->notBlank('address', 'Enter your details in address field');
    $validator  ->requirePresence('email','true')
                 ->notBlank('email', 'Enter your mail id');
    $validator->requirePresence('phone','true')
              ->notBlank('phone', 'Provide your phone number');        
    return $validator;
    }
    }    
UserController.php
    namespace App\Controller;
    use App\Controller\AppController;
    public function registration()
    {
    $user = $this->Users->newEntity($this->request->data()); 
    if ($this->request->is('post')) {              
    $user = $this->Users->patchEntity($user, $this->request->data());
    if ($this->Users->save($user)) {
    $this->Flash->success(__('User detail has been saved.'));
    } else
    $this->Flash->error(__("Error thrown out check and fill the form once again"));

    }
registration.ctp
     echo $this->Form->create('Users',['type' => 'post']);
     echo $this->Form->control('firstname',['value' => '']);
     echo $this->Form->control('lastname',['value'=> '']);
     echo $this->Form->control('username',['value'=> '']);
     echo $this->Form->control('age', ['value'=> '']);
     echo $this->Form->control('gender',['value'=> '']);
     echo $this->Form->control('address',['value'=> '']);
     echo $this->Form->control('email',['value'=> '']);
     echo $this->Form->control('phone',['value'=> '']);
     echo $this->Form->control('password',['value'=> '']);
     echo $this->Form->button('Submit');
     echo $this->Form->end();

0 个答案:

没有答案