Validation 如何构建cakephp 3中存在或等于数字的规则?

Validation 如何构建cakephp 3中存在或等于数字的规则?,validation,cakephp,cakephp-3.0,Validation,Cakephp,Cakephp 3.0,我有表注释和列parent\u id 这是CommentsTable.php的内容: namespace App\Model\Table; use App\Model\Entity\Comment; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; /** * Comments Model */ class CommentsTable exte

我有表
注释
和列
parent\u id

这是CommentsTable.php的内容:

namespace App\Model\Table;

use App\Model\Entity\Comment;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Comments Model
 */
class CommentsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        $this->table('comments');
        $this->displayField('id');
        $this->primaryKey('id');
        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Posts', [
            'foreignKey' => 'post_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('ParentComments', [
            'className' => 'Comments',
            'foreignKey' => 'parent_id'
        ]);
        $this->hasMany('ChildComments', [
            'className' => 'Comments',
            'foreignKey' => 'parent_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create')
            ->requirePresence('body', 'create')
            ->notEmpty('body')
            ->requirePresence('path', 'create')
            ->notEmpty('path')
            ->add('status', 'valid', ['rule' => 'numeric'])
            ->requirePresence('status', 'create')
            ->notEmpty('status')
            ->add('created_at', 'valid', ['rule' => 'datetime'])
            ->requirePresence('created_at', 'create')
            ->notEmpty('created_at')
            ->add('updated_at', 'valid', ['rule' => 'datetime'])
            ->requirePresence('updated_at', 'create')
            ->notEmpty('updated_at');

        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->existsIn(['user_id'], 'Users'));
        $rules->add($rules->existsIn(['post_id'], 'Posts'));
        $rules->add($rules->existsIn(['parent_id'], 'ParentComments'));
        return $rules;
    }
}
我想为字段
父\u id
生成规则:存在于ParentComments中或等于0

你能帮我吗


非常感谢。

规则只是可调用函数或可调用类。
existsIn()
函数只是
existsIn
类的别名。我们可以利用以下优势:

...

use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
   public function buildRules(RulesChecker $rules)
    {
        ...
        $rules->add(
            function ($entity, $options) {
                $rule = new ExistsIn(['parent_id'], 'ParentComments');
                return $entity->parent_id === 1 || $rule($entity, $options);
            },
            ['errorField' => 'parent_id', 'message' => 'Wrong Parent']
        );
        return $rules;
    }
}

规则只是可调用的函数或可调用的类。
existsIn()
函数只是
existsIn
类的别名。我们可以利用以下优势:

...

use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
   public function buildRules(RulesChecker $rules)
    {
        ...
        $rules->add(
            function ($entity, $options) {
                $rule = new ExistsIn(['parent_id'], 'ParentComments');
                return $entity->parent_id === 1 || $rule($entity, $options);
            },
            ['errorField' => 'parent_id', 'message' => 'Wrong Parent']
        );
        return $rules;
    }
}

规则只是可调用的函数或可调用的类。
existsIn()
函数只是
existsIn
类的别名。我们可以利用以下优势:

...

use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
   public function buildRules(RulesChecker $rules)
    {
        ...
        $rules->add(
            function ($entity, $options) {
                $rule = new ExistsIn(['parent_id'], 'ParentComments');
                return $entity->parent_id === 1 || $rule($entity, $options);
            },
            ['errorField' => 'parent_id', 'message' => 'Wrong Parent']
        );
        return $rules;
    }
}

规则只是可调用的函数或可调用的类。
existsIn()
函数只是
existsIn
类的别名。我们可以利用以下优势:

...

use Cake\ORM\Rule\ExistsIn;
class CommentsTable extends Table
{
...
   public function buildRules(RulesChecker $rules)
    {
        ...
        $rules->add(
            function ($entity, $options) {
                $rule = new ExistsIn(['parent_id'], 'ParentComments');
                return $entity->parent_id === 1 || $rule($entity, $options);
            },
            ['errorField' => 'parent_id', 'message' => 'Wrong Parent']
        );
        return $rules;
    }
}