Search code examples
formsmodelyiigii

Custom form element name attributes on CForm Yii Framework


I am using CModel (Gii specifically) to generate forms for my Yii application. I have a problem with how it names the name="" attributes of my form elements. They look exactly like my table fields on my DB. So, for a table like: Users. I get a form element like <input type="text" name="Users[usr_username]" ... />

This gives away my table name, and the fields. I understand that I can change the model name to something else to avoid showing my real table name, but I would also like to change usr_username to username at least. Or even remove the model name on the element's name attribute and have name="username" instead.

I would really appreciate your input.

Regards


Solution

  • So you could use simple text fields like this:

    <?php echo CHtml::textField('username'); ?>
    

    And then map them with your model attributes in the controller:

    $model->usr_username = $_POST['username'];
    

    But you won't be able to do massive assignment:

    $model->attributes = $_POST['Users'];