Yii Html class for use in non Yii projects
22 Oct 2014Forms are essential for any web development project. Tasks we do in building and populating forms, such as checking value in $_POST
, $_GET
and populating that, populating drop downs with data from database and pre selecting data, are redundant. While using Yii framework I loved their CHtml class. It had all the effort done for you, you only had to use it properly and it would do most of redundant tasks for you.
Recently I was working on a flat code project and had to perform all these tasks with flat code, so instead of continuing to write redundant code I refactored Yii’s CHtml class to be used in general non Yii projects. So following modifications were done to make it compatible with Non Yii projects:
- No support for Active Controls
- Error methods don’t accept model(s) as argument instead they accept key as attribute
- If an input control’s name is set in
$_REQUEST
it is picked as value and supplied value is ignored - No support for clientChange option in htmlOptions
- No javascript validation support
- Name was changed to Html
I encourage you to use it, it will reduce your development time and will produce good consistent code.
Usage
Note: PHP >= 5.4 required
Here’s simple usage example:
Read source code comments for understanding further details.