Change The Id That Model Creates For Form Inputs


I’m used to create my form manually.

there is disadvantage to this and that is when I’m validating my inputs the model sends back a message that doesn’t comply with the way that I handle forms.

see this example of an error message that the model ‘Users’ sends to me

"{"Users_email":["Email is not a valid email address."]}"

the model is using the name "Users_email" cause normally this is the id of email input created by form widget

but in my custom made form I have a different id and stuff.

so I’m looking for a way that model would assign a different name for my input the way I need it to be.

tanx for this awesome community

There are few solutions for you.

First, the best one, most recommended. Recode your application to use the proper way of Yii.

Second, if you only need to focus on getting the validation error message and keep the way of your view built, use the form widget and display the error message with

<div class="row">

    <?php echo $form->error($model, 'email'); ?>


Third, the focus is similar with the second one but you don’t want to use the form widget, modifies your controller to add the lines for getting the validation error message of each model’s attribute, keep it in the variable (one variable for one attribute), and pass the variable on view rendering. Your controller should add the line like this:


$emailErrMessage = $model->getErrors('email');

Ok, You know I’m Using it the proper way just I need the id to match my javascript, that’s all.

anyway thanks.

I had to change my javascript file to get things to work.