本文整理汇总了PHP中Cake\ORM\Table::validator方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::validator方法的具体用法?PHP Table::validator怎么用?PHP Table::validator使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Table
的用法示例。
在下文中一共展示了Table::validator方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addValidationRules
public function addValidationRules()
{
$add_validation_rules = $this->config('add_validation_rules');
if ($add_validation_rules) {
$this->_table->validator()->add('parent_id', 'child_of_itself', ['rule' => function ($value, $context) {
if (!$context['newRecord'] && $context['data']['parent_id'] == $context['data']['id']) {
return false;
}
return true;
}, 'message' => __d('alaxos', 'a node can not be child of itself')]);
$this->_table->validator()->add('parent_id', 'child_of_child', ['rule' => function ($value, $context) {
if (!$context['newRecord']) {
$child_nodes = $context['providers']['table']->find('children', ['for' => $context['data']['id']]);
$children_ids = $child_nodes->extract('id')->toArray();
if (in_array($context['data']['parent_id'], $children_ids)) {
return false;
}
}
return true;
}, 'message' => __d('alaxos', 'a node can not be child of one of its children')]);
}
}
示例2: _validate
/**
* Returns the validation errors for a data set based on the passed options
*
* @param array $data The data to validate.
* @param array $options The options passed to this marshaller.
* @param bool $isNew Whether it is a new entity or one to be updated.
* @return array The list of validation errors.
* @throws \RuntimeException If no validator can be created.
*/
protected function _validate($data, $options, $isNew)
{
if (!$options['validate']) {
return [];
}
if ($options['validate'] === true) {
$options['validate'] = $this->_table->validator('default');
}
if (is_string($options['validate'])) {
$options['validate'] = $this->_table->validator($options['validate']);
}
if (!is_object($options['validate'])) {
throw new RuntimeException(sprintf('validate must be a boolean, a string or an object. Got %s.', gettype($options['validate'])));
}
return $options['validate']->errors($data, $isNew);
}
示例3: _processValidation
/**
* Validates the $entity if the 'validate' key is not set to false in $options
* If not empty it will construct a default validation object or get one with
* the name passed in the key
*
* @param \Cake\ORM\Entity $entity The entity to validate
* @param \ArrayObject $options The option for processing validation
* @return bool true if the entity is valid, false otherwise
*/
protected function _processValidation($entity, $options)
{
$type = is_string($options['validate']) ? $options['validate'] : 'default';
$validator = $this->_table->validator($type);
$pass = compact('entity', 'options', 'validator');
$event = $this->_table->dispatchEvent('Model.beforeValidate', $pass);
if ($event->isStopped()) {
return (bool) $event->result;
}
if (!count($validator)) {
return true;
}
$success = !$entity->validate($validator);
$event = $this->_table->dispatchEvent('Model.afterValidate', $pass);
if ($event->isStopped()) {
$success = (bool) $event->result;
}
return $success;
}
示例4: testValidatorSetter
/**
* Tests that it is possible to set a custom validator under a name
*
* @return void
*/
public function testValidatorSetter()
{
$table = new Table();
$validator = new \Cake\Validation\Validator();
$table->validator('other', $validator);
$this->assertSame($validator, $table->validator('other'));
$this->assertSame($table, $validator->provider('table'));
}