public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
 {
     if ($entity->isNew() && empty($entity->user_id)) {
         $entity->errors('user_id', ['not_found', 'User not found']);
         $entity->errors('net_id', ['not_found', 'User not found']);
         return false;
     }
 }
Example #2
0
 /**
  * Skip error checking for the actual save operation since
  * the entity is checked for errors manually within the KeyValueBehavior.
  *
  * @param string|array|null $field The field to get errors for, or the array of errors to set.
  * @param string|array|null $errors The errors to be set for $field
  * @param bool $overwrite Whether or not to overwrite pre-existing errors for $field
  * @return array|\Cake\Datasource\EntityInterface
  */
 public function errors($field = null, $errors = null, $overwrite = false)
 {
     if (isset($this->_properties['scope'])) {
         $this->_errors = [];
     }
     return parent::errors($field, $errors, $overwrite);
 }
 /**
  * Constructor
  *
  * @param \Cake\ORM\Entity $entity Entity
  * @param int $code code to report to client
  */
 public function __construct(Entity $entity, $code = 422)
 {
     $this->_validationErrors = array_filter((array) $entity->errors());
     $flat = Hash::flatten($this->_validationErrors);
     $errorCount = $this->_validationErrorCount = count($flat);
     $this->message = __dn('crud', 'A validation error occurred', '{0} validation errors occurred', $errorCount, [$errorCount]);
     parent::__construct($this->message, $code);
 }
 /**
  * Before save callback.
  *
  * @param \Cake\Event\Event $event The beforeSave event that was fired
  * @param \Cake\ORM\Entity $entity The entity that is going to be saved
  * @return bool
  */
 public function beforeSave(Event $event, Entity $entity)
 {
     $addressColumn = $this->_config['addressColumn'];
     $latitudeColumn = $this->_config['latitudeColumn'];
     $longitudeColumn = $this->_config['longitudeColumn'];
     $parameters = (array) $this->_config['parameters'];
     $requireSuccess = $this->_config['requireSuccess'];
     if (is_array($addressColumn)) {
         $address = [];
         foreach ($addressColumn as $column) {
             if (!empty($entity->{$column})) {
                 $address[] = $entity->{$column};
             }
         }
         $address = implode(', ', $address);
     } else {
         $address = $entity->{$addressColumn};
     }
     $parameters['address'] = $address;
     $parameters['sensor'] = 'false';
     $url = 'http://maps.googleapis.com/maps/api/geocode/json';
     $http = new Client();
     $response = $http->get($url, $parameters);
     $response = json_decode($response->body());
     if ($response->status == 'OK') {
         $entity->{$latitudeColumn} = floatval($response->results[0]->geometry->location->lat);
         $entity->{$longitudeColumn} = floatval($response->results[0]->geometry->location->lng);
         return true;
     } else {
         if ($requireSuccess) {
             $entity->errors($addressColumn, 'Could not geocode address');
             return false;
         } else {
             return true;
         }
     }
 }
 public function testValidationErrorMultipleMessages()
 {
     $entity = new Entity();
     $entity->errors(['title' => ['error message'], 'body' => ['another field message']]);
     $Exception = new ValidationException($entity);
     $Controller = $this->getMock('Cake\\Controller\\Controller', ['render']);
     $Controller->request = new Request();
     $Controller->response = new Response();
     $Renderer = $this->getMock('Crud\\Error\\ExceptionRenderer', ['_getController'], [], '', false);
     $Renderer->expects($this->once())->method('_getController')->with()->will($this->returnValue($Controller));
     $Renderer->__construct($Exception);
     $Renderer->render();
     $expected = ['code' => 422, 'url' => $Controller->request->here(), 'message' => '2 validation errors occurred', 'errorCount' => 2, 'errors' => ['title' => ['error message'], 'body' => ['another field message']], 'exception' => ['class' => 'Crud\\Error\\Exception\\ValidationException', 'code' => 422, 'message' => '2 validation errors occurred']];
     $data = $Controller->viewVars['data'];
     unset($data['trace']);
     $this->assertEquals($expected, $data);
 }
 /**
  * Changes the password for an user.
  *
  * @param \Cake\ORM\Entity $entity User entity
  * @return boolean
  */
 public function changePassword(Entity $entity)
 {
     if ($entity->errors()) {
         return false;
     }
     $entity->password = $this->hashPassword($entity->password);
     if ($this->_table->save($entity)) {
         return true;
     }
     return false;
 }
Example #7
0
 /**
  * Tests __debugInfo
  *
  * @return void
  */
 public function testDebugInfo()
 {
     $entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
     $entity->somethingElse = 'value';
     $entity->accessible('name', true);
     $entity->virtualProperties(['baz']);
     $entity->dirty('foo', true);
     $entity->errors('foo', ['An error']);
     $entity->source('foos');
     $result = $entity->__debugInfo();
     $expected = ['foo' => 'bar', 'somethingElse' => 'value', '[new]' => true, '[accessible]' => ['*' => true, 'name' => true], '[dirty]' => ['somethingElse' => true, 'foo' => true], '[original]' => [], '[virtual]' => ['baz'], '[errors]' => ['foo' => ['An error']], '[repository]' => 'foos'];
     $this->assertSame($expected, $result);
 }
Example #8
0
 /**
  * Test the generation of fields for a multi record form.
  *
  * @return void
  */
 public function testMultiRecordForm()
 {
     $this->loadFixtures('Articles', 'Comments');
     $articles = TableRegistry::get('Articles');
     $articles->hasMany('Comments');
     $comment = new Entity(['comment' => 'Value']);
     $article = new Article(['comments' => [$comment]]);
     $this->Form->create([$article]);
     $result = $this->Form->input('0.comments.1.comment');
     //@codingStandardsIgnoreStart
     $expected = ['div' => ['class' => 'input textarea'], 'label' => ['for' => '0-comments-1-comment'], 'Comment', '/label', 'textarea' => ['name', 'id' => '0-comments-1-comment', 'rows' => 5], '/textarea', '/div'];
     //@codingStandardsIgnoreEnd
     $this->assertHtml($expected, $result);
     $result = $this->Form->input('0.comments.0.comment');
     //@codingStandardsIgnoreStart
     $expected = ['div' => ['class' => 'input textarea'], 'label' => ['for' => '0-comments-0-comment'], 'Comment', '/label', 'textarea' => ['name', 'id' => '0-comments-0-comment', 'rows' => 5], 'Value', '/textarea', '/div'];
     //@codingStandardsIgnoreEnd
     $this->assertHtml($expected, $result);
     $comment->errors('comment', ['Not valid']);
     $result = $this->Form->input('0.comments.0.comment');
     //@codingStandardsIgnoreStart
     $expected = ['div' => ['class' => 'input textarea error'], 'label' => ['for' => '0-comments-0-comment'], 'Comment', '/label', 'textarea' => ['name', 'class' => 'form-error', 'id' => '0-comments-0-comment', 'rows' => 5], 'Value', '/textarea', ['div' => ['class' => 'error-message']], 'Not valid', '/div', '/div'];
     //@codingStandardsIgnoreEnd
     $this->assertHtml($expected, $result);
     TableRegistry::get('Comments')->validator('default')->allowEmpty('comment', false);
     $result = $this->Form->input('0.comments.1.comment');
     //@codingStandardsIgnoreStart
     $expected = ['div' => ['class' => 'input textarea required'], 'label' => ['for' => '0-comments-1-comment'], 'Comment', '/label', 'textarea' => ['name', 'required' => 'required', 'id' => '0-comments-1-comment', 'rows' => 5], '/textarea', '/div'];
     //@codingStandardsIgnoreEnd
     $this->assertHtml($expected, $result);
 }
 /**
  * @param \Cake\ORM\Entity $entity
  * @return void
  */
 protected function invalidate($entity)
 {
     $errorMessage = $this->_config['validationError'] !== null ? $this->_config['validationError'] : __('Could not geocode this address. Please refine.');
     if ($errorMessage === false) {
         return;
     }
     $fields = (array) $this->_config['address'];
     foreach ($fields as $field) {
         if (!is_array($errorMessage)) {
             $entity->errors($field, $errorMessage);
         }
         $message = !empty($errorMessage[$field]) ? $errorMessage[$field] : null;
         if (!$message) {
             continue;
         }
         $entity->errors($field, $message);
     }
 }
Example #10
0
 /**
  * Tests __debugInfo
  *
  * @return void
  */
 public function testDebugInfo()
 {
     $entity = new Entity(['foo' => 'bar'], ['markClean' => true]);
     $entity->accessible('name', true);
     $entity->virtualProperties(['baz']);
     $entity->dirty('foo', true);
     $entity->errors('foo', ['An error']);
     $entity->source('foos');
     $result = $entity->__debugInfo();
     $expected = ['new' => true, 'accessible' => ['*' => true, 'name' => true], 'properties' => ['foo' => 'bar'], 'dirty' => ['foo' => true], 'original' => [], 'virtual' => ['baz'], 'errors' => ['foo' => ['An error']], 'repository' => 'foos'];
     $this->assertSame($expected, $result);
 }
 /**
  * Password reset, compares the two passwords and saves the new password.
  *
  * @param \Cake\ORM\Entity $user Entity object.
  * @return void
  */
 public function resetPassword(Entity $user)
 {
     if (!$user->errors()) {
         $user->{$this->_field('password')} = $this->hashPassword($user->{$this->_field('password')});
         $user->{$this->_field('passwordToken')} = null;
         $user->{$this->_field('passwordTokenExpires')} = null;
         return $this->_table->save($user, ['checkRules' => false]);
     }
     return false;
 }
 /**
  * Test the generation of fields for a multi record form.
  *
  * @return void
  */
 public function testMultiRecordForm()
 {
     $this->loadFixtures('Article', 'Comment');
     $articles = TableRegistry::get('Articles');
     $articles->hasMany('Comments');
     $comment = new Entity(['comment' => 'Value']);
     $article = new Article(['comments' => [$comment]]);
     $this->Form->create([$article]);
     $result = $this->Form->input('0.comments.1.comment');
     $expected = array('div' => array('class' => 'input textarea'), 'label' => array('for' => '0-comments-1-comment'), 'Comment', '/label', 'textarea' => array('name', 'type', 'id' => '0-comments-1-comment'), '/textarea', '/div');
     $this->assertTags($result, $expected);
     $result = $this->Form->input('0.comments.0.comment');
     $expected = array('div' => array('class' => 'input textarea'), 'label' => array('for' => '0-comments-0-comment'), 'Comment', '/label', 'textarea' => array('name', 'type', 'id' => '0-comments-0-comment'), 'Value', '/textarea', '/div');
     $this->assertTags($result, $expected);
     $comment->errors('comment', ['Not valid']);
     $result = $this->Form->input('0.comments.0.comment');
     $expected = array('div' => array('class' => 'input textarea error'), 'label' => array('for' => '0-comments-0-comment'), 'Comment', '/label', 'textarea' => array('name', 'type', 'class' => 'form-error', 'id' => '0-comments-0-comment'), 'Value', '/textarea', array('div' => array('class' => 'error-message')), 'Not valid', '/div', '/div');
     $this->assertTags($result, $expected);
     TableRegistry::get('Comments')->validator('default')->allowEmpty('comment', false);
     $result = $this->Form->input('0.comments.1.comment');
     $expected = array('div' => array('class' => 'input textarea required'), 'label' => array('for' => '0-comments-1-comment'), 'Comment', '/label', 'textarea' => array('name', 'type', 'required' => 'required', 'id' => '0-comments-1-comment'), '/textarea', '/div');
     $this->assertTags($result, $expected);
 }
 /**
  * Validates a resource and returns the validation errors.
  *
  * @param Cake\ORM\Entity $resource
  *
  * @return array
  */
 public function validateResource(Entity &$resource)
 {
     $errors = $this->getResourceValidator()->errors($resource->toArray());
     $resource->errors($errors, null, true);
     return $errors;
 }
Example #14
0
 /**
  * Callback for Model.beforeSave event.
  *
  * @param \Cake\Event\Event $event The afterSave event that was fired.
  * @param \Cake\ORM\Entity $entity The entity that was saved.
  * @param \ArrayObject $options Options.
  * @return void
  */
 public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
 {
     $slugField = $this->config('field');
     $fields = (array) $this->config('displayField');
     $separator = $this->config('separator');
     if (!$entity->isNew() || $entity->dirty($slugField)) {
         return;
     }
     $parts = [];
     foreach ($fields as $field) {
         if ($entity->errors($field)) {
             return;
         }
         $parts[] = $entity->{$field};
     }
     $entity->set($slugField, $this->slug($entity, implode($separator, $parts), $separator));
 }
 /**
  * Changes the password for an user.
  *
  * @param \Cake\ORM\Entity $entity User entity
  * @return boolean
  */
 public function changePassword(Entity $entity)
 {
     $validator = $this->_table->validator('default');
     $validator->provider('userTable', $this->_table);
     $validator->add('old_password', 'notBlank', ['rule' => 'notBlank', 'message' => __d('userTools', 'Enter your old password.')]);
     $validator->add('old_password', 'oldPassword', ['rule' => ['validateOldPassword', 'password'], 'provider' => 'userTable', 'message' => __d('user_tools', 'Wrong password, please try again.')]);
     $this->_table->validator('default', $validator);
     if ($entity->errors()) {
         return false;
     }
     $entity->password = $this->hashPassword($entity->password);
     if ($this->_table->save($entity, ['validate' => false])) {
         return true;
     }
     return false;
 }