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!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.