Cake 3.x验证 - 创建可重用的验证器

时间:2016-03-27 10:55:02

标签: php validation cakephp

我正在开发cakephp 3.x,我正在尝试验证用户更改密码表单。 所以在这里我尝试创建一个单独的类进行验证,请参阅下面的代码

UserValidator  在构造函数中,我定义了一些验证规则。

<?php
namespace App\Model\Validation;

use Cake\Validation\Validator;

class UsersValidator extends Validator
{
    public function __construct()
    {
        parent::__construct();
        // Add validation rules here.
        $this
            ->requirePresence('old_password')
            ->notEmpty('old_password');

        $this
            ->requirePresence('password')
            ->notEmpty('password');

        $this
            ->requirePresence('confirm_password')
            ->notEmpty('confirm_password');
    }

    public function validateChangePasswordForm(){

    }

}

的usermodel 我试图使用它...

<?php
namespace Accounts\Model\Table;
use Accounts\Model\Entity\User;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validation;
use Cake\Validation\Validator;
use App\Model\Validation\UsersValidator;
/**
 * Users Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Roles
 */
class UsersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->table('users');
        $this->displayField('first_name');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');
        $this->belongsTo('Roles', [
            'foreignKey' => 'role_id',
            'joinType' => 'LEFT',
            'className' => 'Accounts.Roles',
        ]);
        $this->hasOne('Profiles', [
            'foreignKey' => 'user_id',
            'className' => 'Accounts.Profiles',
            'dependent' => true
        ]);
    }
    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationChangepass(UsersValidator $validator)
    {
        return $validator;
    }
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        return $validator;
    }
    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['email']));
        $rules->add($rules->isUnique(['username']));
        $rules->add($rules->existsIn(['role_id'], 'Roles'));
        return $rules;
    }
    public function beforeSave($event, $entity, $options){
        if($entity->first_name)
            $entity->slug = $this->createSlug($entity->first_name);
    }
}

用户更改密码表单

<div class="users form large-9 medium-8 columns content">

    <?php echo $this->Form->create($user, ['context' => ['validator' => 'Changepass']]); ?>
    <fieldset>
        <legend><?= __('ChangePassword') ?></legend>
        <?php echo $this->Form->input('old_password', ['autocomplete'=>'off' ]) ?>
        <?php echo $this->Form->input('password', ['label'=>'New Password', 'autocomplete'=>false]) ?>
        <?php echo $this->Form->input('confirm_password', ['label'=>'Re-Enter Password']) ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

错误

警告(4096):传递给Accounts \ Model \ Table的参数1:UsersTable :: validationChangepass()必须是App \ Model \ Validation \ UsersValidator的实例,给出了Cake \ Validation \ Validator的实例,在C中调用:第106行\ wamp \ www \ comsats-lounge \ vendor \ cakephp \ cakephp \ src \ Validation \ ValidatorAwareTrait.php并定义[ROOT \ plugins \ Accounts \ src \ Model \ Table \ UsersTable.php,第55行]

我试图克服这个警告。请给我一个解决方案。

我关注的链接

http://book.cakephp.org/3.0/en/core-libraries/validation.html#creating-reusable-validators

1 个答案:

答案 0 :(得分:0)

UsersTable更改签名

public function validationChangepass(UsersValidator $validator) 

public function validationChangepass(Validator $validator)