Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restoring the validationErrors property in FormHelper, it will be che…
…cked first before the validation errors in the model
- Loading branch information
Showing
2 changed files
with
48 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
*/ | ||
|
@@ -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)) { | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lorenzo
Author
Member
|
||
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; | ||
} | ||
} | ||
|
||
|
It might be a better idea to use $this->model() here. Sometimes the model is tricky to get.