I have a basic user editing form, the simplest as it can be, generated in whole by gii. I changed only it’s title and breadcrumbs part to display user name (where originally ID is, if I’m not mistaken). So:
If user uses this form and makes user name filed empty (which is incorrect, as this one is required) Yii will handle this properly showing (upon reload) proper error message. But in the same time user name will disappear from both breadcrumbs and page title.
For above code I’ll get then: “Edit user „”” for title and “Main page » Admin » Users » » Edit user” for breadcrumbs.
Where I should get: "Edit user „USERNAME”" for title and "Main page » Admin » Users » USERNAME » Edit user" for breadcrumbs.
Either I’m missing something or form generated by gii takes user name (or any other data) from form model, not actually user model. Strange! Is there any way to fix this?
looks like after you submit the form model is updating attributes to fill the form but doesn’t save it because of error. you might send the username separate from the model.
You answer suggest that this is normal behaviour (?) not an error in my application. And this is what I want to know. If this is normal situation, I’ll try to find a fix for it. Your idea seems interesting. Thank you.
You have to take the user name before doing setAttributes and pass it to the view.
public function actionUpdate()
{
$model=$this->loadModel();
$username= $user->username;
....
$this->render('update', array('model'=>$model, 'username'=>$username));
}
In the view, for breadcrumbs and page title use $username instead of $model->username.
But, since this problem occurs only on update and only in case of error (works fine in other views), I will have to update all views (and controllers) to the solution zaccaria provided. Or add some check and either $username or $user->username are empty and print out the one, isn’t.
Thanks again! I just wanted to make myselt sure, if this is normal behaviour or if I made some mistake in the code. EoT.