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')]); } }
/** * 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); }
/** * 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; }
/** * 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')); }