0

I'm creating a custom J3.x component. I'm manually adding my own error notices where needed via

JFactory::getApplication()->enqueueMessage()

However, when a record is not saved by my component, it is triggering the line:

$this->setMessage($this->getError(), 'error');

within the /libraries/src/MVC/Controller/FormController.php which displays a notice like "Save failed with the following error: ", with no error listed.

I would like to either...

  1. (Ideally) Be able to add my own error notice (from within my component code) that is displayed at the end of that phrase via that getError line.

OR ALTERNATELY

  1. Disable that notice - preferably at the component level, and ideally based on the specific view, not globally.

Any ideas how to programmatically accomplish either of these solutions (or possibly have another option so that I can display my own error notices at the component/view level?

1
  • Can you clarify where you're using JFactory::getApplication()->enqueueMessage()? If you're doing it anywhere other than the controller, you're doing it wrong.
    – Sharky
    Jun 21 at 5:36

2 Answers 2

0

Yes, I'm calling from Controller.

I found the solution (or the error), I was doing a 'return false' which was causing the error to be displayed.

Instead, I found another approach (How do I properly raise an error from within a plugin?) and instead just raise the notice to the user via enqueueMessage and then just use: JFactory::getApplication->rediret(JRoute::_('url_to_same_form', false));

That displays my error notice only and puts them back on the same form (even if they use Save & Close).

Not exactly what I had originally looked for, but a better solution in the end.

1
  • But where do you get the error message from? It should be coming from the model.
    – Sharky
    Jun 22 at 6:45
0

If you look at the code just above the one you referenced, you'll see that the controller reads the error message from the model:

$this->setError(\JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');

So it looks like you're not setting the error correctly in your model. Inside the model's save() method, as well as other methods called by the controller, you should set the error message using setError() method before returning false.

public function save($data)
{
    if ($badCondition)
    {
        $this->setError('Some error happened');
        
        return false;
    }
    
    return parent::save($data);
}

Then the message will be shown correctly:

Error
Save failed with the following error: Some error happened
1
  • Thanks. I had tried to use setError originally but when I did that, it was resulting in the component just crashing with an error notice - basically looked like it wasn't even being returned to the controller for 'processing'. Although I didn't dig deeper into that. Although that functionality (stop processing) might be 'appropriate' (i.e. expected) for an error notice, that's not the best workflow for this instance. Jun 22 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.