We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

What is the best way to throw custom validation errors when the publish form submits?

Development and Programming

Ben Parizek's avatar
Ben Parizek
128 posts
14 years ago
Ben Parizek's avatar Ben Parizek

I have recently been working on an add-on that validates if categories have been selected or not and alerts the user with an error message if they try to submit the publish form without selecting the required categories. In the process, I’ve been trying to understand what my options are to throw the error message for this, and how to redirect the user back to the publish form so they can add the required categories.

Can anyone help explain to me the following methods and if I am understanding them correctly? My goal is to find a way to throw an error on the publish form page before the user gets redirected to another page, and maintain all the current form data.

The first method I looked at was used in the BW Required Category add-on. It uses javascript and I couldn’t seem to get it to work as I don’t fully understand what the code is doing. I think this may be the solution I want but I don’t understand what is going on with this code.

$this->EE->javascript->output('$.ee_notice("'.$this->EE->lang->line('forgot_category').'", {type : "error"})');
$this->EE->api_channel_entries->_set_error('forgot_category', 'category');
$this->EE->extensions->end_script = TRUE;

The second method I discovered was the show_error() method. This throws an error on a separate page after the Publish Form is submitted, maintains some publish form data but loses data in other custom fields.

show_error('Categories are required.');

The third method I found throws an error on a separate page after the Publish Form is submitted. The user can click back and the form data is maintained on the original page.

$this->EE->output->show_user_error('categories', $this->EE->lang->line('forgot_category'));

Personally, I don’t like the second and third options as they bring the user to an inbetween page and force them to click back before updating things. Ideally, I’d find a way to have an error message appear on the publish form page and alert the user of what needs to be done. I think that’s what the first option may be doing. And I don’t know if there are other options I’m overlooking. Any help to understand my options better would be appreciated!

       
Gareth Davies's avatar
Gareth Davies
491 posts
about 14 years ago
Gareth Davies's avatar Gareth Davies

Have you seen the Javascript notification plugin that the BW Required Category extension is using? This should largely explain what is going on for the first method.

http://ellislab.com/expressionengine/user-guide/development/cp_javascript/notification.html

       
Ben Parizek's avatar
Ben Parizek
128 posts
about 14 years ago
Ben Parizek's avatar Ben Parizek

Thanks Gareth. I didn’t realize that was a documented plugin. Thanks for pointing me towards the docs!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.