2

I am new to Craft but loving its ease of use so far.

I am having a problem however when setting up a plugin to display an error to the user when using the onBeforeSaveEntry hook when a user saves an entry. I am looking to determine the value of a field and then take the user back to the entry page and display an error to the user based on a certain selection. Is this something that can be done with the Craft page when saving an entry?

1 Answer 1

0

Is this something that can be done with the Craft page when saving an entry?

In 2.2, there isn't an easy way. In the upcoming 2.3 release, we added a $performAction property to the event, so your can do something like:

$entry->addError('attribute', 'MyErrorMessage');
$event->performAction = false;

And Craft will not save the entry and return to the edit entry page to display any validation errors.

You should post your second question as an entirely new question.

5
  • Ok, many thanks. Any news on when 2.3 will be available?
    – Neil Young
    Nov 28, 2014 at 16:43
  • Closer than you think!
    – Brad Bell
    Nov 28, 2014 at 16:44
  • Good stuff! :-)
    – Neil Young
    Nov 28, 2014 at 16:46
  • I have managed to get the update installed and have added your code above but I still don't seem to be able to add custom error messages. Although the performAction flag does seem to stop the entry from saving. Any idea of how I can get your code above to work?
    – Neil Young
    Dec 11, 2014 at 15:04
  • 2
    @NeilYoung try $event->params['entry']->addError('MyFieldHandle', 'MyErrorMessage');
    – carlcs
    Jan 20, 2015 at 12:51

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.