Error message in custom attribute

Permalink
Hi all,

I made a custom attribute based on this how to:http://www.concrete5.org/documentation/how-tos/developers/custom-at...

My problem is that the error message in this example overrides all the other errors addded to the error message. Any idee how i add errors to the original error/helper in an custom attribute controller? Thanks!

brianvandelden
 
joannebail replied on at Permalink Reply
I too would like to know the answer!
JohntheFish replied on at Permalink Reply
JohntheFish
The problem is that each attribute creates its own error helper, so whichever attribute is last on the page with an error overwrites everything before. You can fix this in your own attribute, but it will then fall foul of any that are after it on any edit page.

In validateForm()
$sets = $this->getSets();
if (is_object($sets['error'])){
  $e = $sets['error'];
} else {
  $e = Loader::helper('validation/error');
}
// ... validation code that may add error messages to $e ...
return $e;

The above looks for any error object set by a previous attribute and uses it, or creates one otherwise.

Then in saveForm()
$e = $this->validateForm($data);
if (is_object($e) && $e->has()){
  $this->set('error', $e);
} else {      
  $this->saveValue($data['value']);
}

This checks for a current error object, and either sets it for display or saves the validated value. It also gets round another glitch in custom attributes where the attribute value could be saved even if it failed validation.