繁体   English   中英

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

[英]Cake 3.x Validation - Creating Reusable Validators

我正在使用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 \\ UsersTable :: validationChangepass()的参数1必须是App \\ Model \\ Validation \\ UsersValidator的实例,指定的Cake \\ Validation \\ Validator的实例,在C:\\ wamp \\中调用第106行的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/zh-TW/core-libraries/validation.html#creating-reusable-validators

在您的UsersTable更改签名

public function validationChangepass(UsersValidator $validator) 

public function validationChangepass(Validator $validator)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM