External form and validation
Permalink 2 users found helpful
Hi,
I'm building a custom form (Done with External Form block), and i'm able to parse and validate the fields, the problem is that Concrete doesn't seem to set the fields with errors with any class, so i'm unable to highlight the errors to the user.
Is there any way to do this?
I'm building a custom form (Done with External Form block), and i'm able to parse and validate the fields, the problem is that Concrete doesn't seem to set the fields with errors with any class, so i'm unable to highlight the errors to the user.
Is there any way to do this?
Thanks for your answer mnakalay, that's actually what i'm using, but i don't know how to get exact elements that didn't validate.
Yeah, while c5 preserves data entered into fields on POST, it doesn't add error classes to form elements or even give field names in the error helper output, unfortunately. But it's not too hard to add this in yourself to your custom controller and view. It's just a matter of going the extra step to have two validation helpers -- one for the string to display to the user and one to store field names for you to exploit in your view and add error classes appropriately. Something like this in your controller:
Then if there are errors, you can iterate through the output of $val_name in your view to see which elements to add an error class to, either by PHP or javascript.
It's far from elegant, but it works.
Cheers,
Benji
$val_msg = Loader::helper('validation/form'); $val_name = Loader::helper('validation/form'); $val_msg->setData($this->post()); $val_name->setData($this->post()); $val_msg->addRequired('fname', 'Please provide your first name'); $val_name->addRequired('fname', 'fname');
Then if there are errors, you can iterate through the output of $val_name in your view to see which elements to add an error class to, either by PHP or javascript.
It's far from elegant, but it works.
Cheers,
Benji
Thanks Benji, i ended up doing something similar (Not as elegant as your code).
Gotta change it for yours!! :)
Gotta change it for yours!! :)
seehttp://www.concrete5.org/documentation/developers/system/error-hand...