Field can't be empty

So I have a form with a field that is hidden. I want it to be a required field for the purpose of submitting to the server, but I don’t want ordinary users dicking with it, I want it to be their userid.

I used a options array like this to give it a value.


<?php echo CHtml::activeTextField($model,'userId',array('value' => Yii::app()->user->id)); ?>

I look in firebug and the source and indeedy it has a value and it is the id of the logged in user, just what I want.

So when I submit it, why is it telling me that this field is empty?

To be really secure I shouldn’t even have this field on the form at all for ordinariy user, because I know this value, so I just fill in serverside. But for admin user I want to have it so admin can change user if he wants.

How and more importantly where does one do that? and how one set up the model so it not giving errors.

is the userid attribute marked as "safe"? see CModel::safeAttributes()

It is now. Same problem.

could you check if the userid attribute is being filled in the model after the setAttributes() call?

I had problem using this way, too.

Instead of, set your value at Controller, not at view.

Controller:


$model->userId = Yii::app()->user->id;

view:


<?php echo CHtml::activeTextField($model,'userId'); ?>

Is id a primary key? API doc for 1.0 says primary keys aren’t returned by getSafeAttributes (can be overriden in CModel). Thus probably not massively assigned.

Edit:

"See CModel::safeAttributes on how to override this method."

/Tommy