Error handling / form validation for block save()

Permalink 1 user found helpful
I'm building a custom block type. When the user adds or edits one of these blocks, I want to validate the information submitted in the add/edit forms.

What's the best way to do this?

The way I'd like to do it is in the BlockController::save() method, check whether certain required fields are filled out and that they are of the correct form. If not, I'd like to throw an exception.

However, when I do it this way, the exception isn't caught by C5 and instead of the user getting a nicely formatted error message, he gets a "Fatal Error" and then a huge call stack.

Is there a better way to do form validation and error handling here?

Thanks!

 
ScottC replied on at Permalink Reply
ScottC
look at the validation helper:

http://www.concrete5.org/documentation/developers/forms/basic-valid...

But before you try to commit to the db i'd do a if(!empty($this->errorArray)) return;


This'll keep you from getting errors from your required db field stuff, you can put in a simple if using $this->set and provide ui feedback as you see fit. Returning before committing though is at the very least what you should be doing, validation helper will well help that.