CakePHP 3验证范围不起作用 - 我没有收到任何错误消息

时间:2018-01-16 18:09:11

标签: validation cakephp cakephp-3.0

我正在尝试验证我的实体,但它无法正常工作。只有isUnique才有效。但是NoEmpty,minLength和range被忽略了。我也希望显示这些消息。我究竟做错了什么?或者theruleschecker是否会覆盖验证?

CitiesTable.php

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;    

class CitiesTable extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('cities');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->hasMany('Fields', [
            'foreignKey' => 'city_id'
        ]);
    }    

    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        $validator
            ->requirePresence('name', 'create')
            ->notEmpty('name')
            ->add('name', [
                'length' => [
                    'rule' => ['minLength', 15],
                    'message' => 'Titles need to be at least 10 characters long',
                ]
            ])
            ->lengthBetween('username', [15, 20])
            ->minLength('name',15,'TestMessage');

        $validator
            ->integer('fields_per_pieces_x')
            ->range('fields_per_pieces_x',[1,5],'TeestMessage')
            ->requirePresence('fields_per_pieces_x', 'create')
            ->notEmpty('fields_per_pieces_x');

        $validator
            ->integer('fields_per_pieces_y')
            ->requirePresence('fields_per_pieces_y', 'create')
            ->notEmpty('fields_per_pieces_y');

        $validator
            ->integer('field_pieces')
            ->requirePresence('field_pieces', 'create')
            ->notEmpty('field_pieces');

        return $validator;
    }

    public function buildRules(RulesChecker $rules){
        $rules->add($rules->isUnique(['name']));
        return $rules;
    }
}

和此:

$map['city'] = $this->generateCity('Kantosddd', -2, -2, -4);

pr($map);
die();

显示以下内容:

Array
(
    [city] => App\Model\Entity\City Object
        (
            [name] => Kantosddd
            [fields_per_pieces_x] => -2
            [fields_per_pieces_y] => -2
            [field_pieces] => -4
            [[new]] => 1
            [[accessible]] => Array
                (
                    [*] => 1
                )

            [[dirty]] => Array
                (
                    [name] => 1
                    [fields_per_pieces_x] => 1
                    [fields_per_pieces_y] => 1
                    [field_pieces] => 1
                )

            [[original]] => Array
                (
                )

            [[virtual]] => Array
                (
                    [get] => Array
                        (
                            [0] => fields_per_pieces_all
                            [1] => fields_in_x
                            [2] => fields_in_y
                            [3] => count_of_all_fields
                        )

                )

            [[errors]] => Array
                (
                    [name] => Array
                        (
                            [_isUnique] => This value is already in use
                        )

                )

            [[invalid]] => Array
                (
                    [name] => Kantosddd
                )

            [[repository]] => Cities
        )

)

generateCity函数

function generateCity(string $name, int $fieldsPerPiecesX, int $fieldsPerPiecesY, int $fieldPieces ): City
    {

        $citiesTable = TableRegistry::get('Cities');

        $city = $citiesTable->newEntity();

        $city->set('name', $name);
        $city->set('fields_per_pieces_x', $fieldsPerPiecesX);
        $city->set('fields_per_pieces_y', $fieldsPerPiecesY);
        $city->set('field_pieces', $fieldPieces);

        if(!$citiesTable->save($city)){
            foreach($city->errors() as $fields => $field){
                foreach($field as $rule => $message){
                    $this->Flash->error($fields.": ".$message);
                }
            }
        }
        return $city;
    }

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

使用set功能时,不会进行验证,只会进行验证。试试这个:

$city = $citiesTable->newEntity([
    'name' => $name,
    'fields_per_pieces_x' => $fieldsPerPiecesX,
    'fields_per_pieces_y' => $fieldsPerPiecesY,
    'field_pieces' => $fieldPieces
]);

或者,相当于

$city = $citiesTable->newEntity();
$city = $citiesTable->patchEntity($city, [
    'name' => $name,
    'fields_per_pieces_x' => $fieldsPerPiecesX,
    'fields_per_pieces_y' => $fieldsPerPiecesY,
    'field_pieces' => $fieldPieces
]);