Browse files

Restoring the validationErrors property in FormHelper, it will be che…

…cked first before the validation errors in the model
  • Loading branch information...
1 parent 84aba00 commit d5ce09cd967ce331549732c4756cc85afebcfbf3 @lorenzo lorenzo committed Jul 15, 2011
Showing with 48 additions and 5 deletions.
  1. +11 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +37 −5 lib/Cake/View/Helper/FormHelper.php
View
11 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -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);
+ }
}
View
42 lib/Cake/View/Helper/FormHelper.php
@@ -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);
@markstory
CakePHP member

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

@lorenzo
CakePHP member
lorenzo added a note Jul 16, 2011

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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;
}
}

0 comments on commit d5ce09c

Please sign in to comment.