【问题标题】:CakePHP validation is not working for same model different view and functionCakePHP 验证不适用于相同型号的不同视图和功能
【发布时间】:2015-12-24 05:37:37
【问题描述】:

我是 CakePHP 的新手,我正在尝试构建一个密码更改表单,但验证不适用于更改密码。知道为什么吗?

app\Controller\UsersController.php

App::uses('AppController', 'Controller');

class UsersController extends AppController {
    public $paginate = array(
        'limit' => 4,
        'order' => 'User.id DESC'
    );

    public function admin_userAdd() {
        if ($this->request->is('post')) {
            pr($this->request->data);
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved');
                return $this->redirect(array('action' => 'userList'));
            }
            $this->Session->setFlash('The user could not be saved. Please, try again.', 'error');
        }
    }

    public function admin_userList(){
        $users = $this->User->find('all');
        $this->set('users', $this->paginate());
    }

    public function admin_userEdit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                //pr($this->request->data); die();
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'userList'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }

    }

    public function admin_passChange($id = null) {
        $this->User->id = $id;
        if (!empty($this->request->data)) {
            $password = $this->request->data['User']['pass1'];
            if ($this->User->saveField('password',$password)) {
                $this->Session->setFlash('Password has been changed.');
                //return $this->redirect(array('action' => 'userEdit'));
            } else {
                $this->Session->setFlash('Password could not be changed.');
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
    }
}       

app\Model\User.php

App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel {

    public $validate = array(
        'username' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required',
                'allowEmpty' => false
            ),
            'between' => array(
                'rule' => array('between', 4, 15),
                'required' => true,
                'message' => 'Usernames must be between 5 to 15 characters'
            ),
            'unique' => array(
                'on'         => 'create',
                'rule'    => array('isUniqueUsername'),
                'message' => 'This username is already in use'
            ),
            'alphaNumericDashUnderscore' => array(
                'rule'    => array('alphaNumericDashUnderscore'),
                'message' => 'Username can only be letters, numbers and underscores'
            ),
        ),
        'email' => array(
            'required' => array(
                'on' => 'create',
                'rule' => array('email', true),
                'message' => 'Please provide a valid email address.'
            ),
            'unique' => array(
                'on'      => 'create',
                'rule'    => array('isUniqueEmail'),
                'message' => 'This email is already in use',
            ),
            'between' => array(
                'rule' => array('between', 6, 60),
                'message' => 'Usernames must be between 6 to 60 characters'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'editor', 'accountent')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        ),
        'password' => array(
            'length' => array(
                'rule'      => array('between', 5, 40),
                'message'   => 'Your password must be between 5 and 40 characters.',
            ),
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter a password.'
            )
        ),
        'pass1' => array(
            'length' => array(
                'rule'      => array('between', 8, 40),
                'message'   => 'Your password must be between 5 and 40 characters.',
            ),
            'required' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter a password.'
            )
        )
    );
    public function beforeSave($options = array()) {
        // hash our password
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }

        // if we get a new password, hash it
        if (isset($this->data[$this->alias]['pass1'])) {
            $this->data[$this->alias]['pass1'] = AuthComponent::password($this->data[$this->alias]['pass1']);
        }
        return true;
        // fallback to our parent
        return parent::beforeSave($options);
    }

}

app\View\Users\admin_pass_change.ctp

<?php echo $this->Session->flash(); ?>
<div class="row" style="float:right">
    Hi <?php echo $this->data['User']['username']; ?>
</div>
<div class="row">
    <div class="col-lg-6">
        <?php echo $this->Form->create('User'); ?>
        <?php

        echo $this->Form->input('pass1', array('label' =>'New Password', 'class' => 'form-control'));
        echo $this->Form->input('pass2', array('label' =>'Confirm Password', 'class' => 'form-control'));

        ?>

        <?php echo $this->Form->end(__('Submit', array('class' => 'btn btn-default'))); ?>
    </div>

【问题讨论】:

    标签: validation cakephp cakephp-2.x


    【解决方案1】:

    username 在验证数组中包含'required'=&gt;true,但未出现在您的密码更改表单中。

    'username' => array(
        'nonEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'A username is required',
            'allowEmpty' => false
        ),
        'between' => array( 
            'rule' => array('between', 4, 15), 
            'required' => true, // <----------- REQUIRED
            'message' => 'Usernames must be between 5 to 15 characters'
        ),
    

    尝试将其设置为'required'=&gt;'create'

        'between' => array( 
            'rule' => array('between', 4, 15), 
            'required' => 'create',
            'message' => 'Usernames must be between 5 to 15 characters'
        ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      相关资源
      最近更新 更多