Question

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

Was it helpful?

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'];

OTHER TIPS

If you're using CActiveForm widget to create the form, which in youre case i think is true because you have generated the model through Gii, you can always set htmlOptions property of methods that generate form inputs, like:

$form->textField($model,'attribute_name',array('name'=>'your_custom_name'));

In above case, the input value will be accessible through $_POST['your_custom_name'], meaning that massive assignment won't work.

Also be aware that if you won't specify the id property of the input it will be the same as the name property ('your_custom_name').

Regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top