Skip to content

Commit

Permalink
Restoring the validationErrors property in FormHelper, it will be che…
Browse files Browse the repository at this point in the history
…cked first before the validation errors in the model
  • Loading branch information
lorenzo committed Jul 15, 2011
1 parent 84aba00 commit d5ce09c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
11 changes: 11 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -7318,4 +7318,15 @@ public function testIntrospectModelFromRequest() {
CakePlugin::unload();
App::build();
}

/**
* Tests that it is possible to set the validation errors directly in the helper for a field
*
* @return void
*/
public function testCustomValidationErrors() {
$this->Form->validationErrors['Thing']['field'] = 'Badness!';
$result = $this->Form->error('Thing.field', null, array('wrap' => false));
$this->assertEquals('Badness!', $result);
}
}
42 changes: 37 additions & 5 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -114,7 +114,27 @@ class FormHelper extends AppHelper {
* @var array
*/
protected $_models = array();


/**
* Holds all the validation errors for models loaded and inspected
* it can also be set manually to be able to display custom error messages
* in the any of the input fields generated by this helper
*
* @var array
*/
public $validationErrors = array();

/**
* Copies the validationErrors variable from the View object into this instance
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
$this->validationErrors =& $View->validationErrors;
}

/**
* Guess the location for a model based on its name and tries to create a new instance
* or get an already created instance of the model
Expand Down Expand Up @@ -162,13 +182,14 @@ protected function _getModel($model) {
* - key: Returns the name of the primary key for the model
* - fields: Returns the model schema
* - validates: returns the list of fields that are required
* - errors: returns the list of validation errors
*
* If the $field parameter is passed if will return the information for that sole field.
*
* `$this->_introspectModel('Post', 'fields', 'title');` will return the schema information for title column
*
* @param string $model name of the model to extract information from
* @param string $key name of the special information key to obtain (key, fields, validates)
* @param string $key name of the special information key to obtain (key, fields, validates, errors)
* @param string $field name of the model field to get information from
* @return mixed information extracted for the special key and field in a model
*/
Expand Down Expand Up @@ -198,6 +219,13 @@ protected function _introspectModel($model, $key, $field = null) {
}
}

if ($key === 'errors' && !isset($this->validationErrors[$model])) {
$this->validationErrors[$model] =& $object->validationErrors;
return $this->validationErrors[$model];
} elseif ($key === 'errors' && isset($this->validationErrors[$model])) {
return $this->validationErrors[$model];
}

if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
$validates = array();
if (!empty($object->validate)) {
Expand Down Expand Up @@ -258,9 +286,13 @@ protected function _isRequiredField($validateProperties) {
*/
public function tagIsInvalid($model = null, $field = null, $modelID = null) {
$entity = $this->entity();
if (!empty($entity) && $object = $this->_getModel($entity[0])) {
array_shift($entity);
return Set::extract($object->validationErrors, join('.', $entity));
$model = array_shift($entity);

This comment has been minimized.

Copy link
@markstory

markstory Jul 16, 2011

Member

It might be a better idea to use $this->model() here. Sometimes the model is tricky to get.

This comment has been minimized.

Copy link
@lorenzo

lorenzo Jul 16, 2011

Author Member

That is how it was working before, I tried using model() but got several errors. I'm merging this as it is passing the tests, but I'm open to suggestions for improving it

if (!empty($entity) && isset($this->validationErrors[$model])) {
return Set::classicExtract($this->validationErrors[$model], join('.', $entity));
}
if (!empty($entity)) {
$errors = $this->_introspectModel($model, 'errors');
return ($errors) ? Set::classicExtract($errors, join('.', $entity)) : false;
}
}

Expand Down

0 comments on commit d5ce09c

Please sign in to comment.