Revision #2 has been created by Boaz on Mar 31, 2014, 7:21:16 PM with the memo:
dropping extra words
« previous (#1) next (#3) »
Safely checking if a model class exists (without exploding)
error handling, validation
When is this needed?
While rewriting the [PcReportContent extension](http://www.yiiframework.com/extension/pcreportcontent/ "PcReportContent extension") I'm maintaining I've bumped to a challenge. This wiki was born out of it
and below are its details.
This **PcReportContent** extension is about reporting inappropriate content by users, to some predefined email address. This works using a widget that is plant-able in any location on the website, and which gets a 'model name' and 'model id' when rendered. It plants this information in the report form so when the user submits the form, it reaches the server side with this information, along of course with the user text explaining what's wrong with the content its reporting of. This allows for several such 'report content' boxes (=widget instances) to live (happily ever after) one along side each other on the same page. Each contains different form values (meaning model name and id), in accordance to where it was 'rendered'.
**Here's the problem**: When processed on server side, the server side counterparts needs to validate the client side data, as any data from client side is never to be trusted, right? Ok, so my initial validation on the _model name_ was a 'class_exists()' firing. Problem is, if the class does not exists, without rather complex changes to Yii's auto loader or to PHP's environment (silencing PHP errors - another thing you don't want to operate under - you *want* to be aware of problems in your code/application) a PHP Error (WARNING) will be triggered and the application operation will abort. I wanted to catch this situation and return nicely to client side rather then this abortion.