Explicitly Refuse A Model Attribute On Update?

Hi,

I have a model ‘user’ that contains an attribute ‘balance’. Upon adding an user, we can set the initial balance but once user has been added, balance MUST NOT be edited via user update form (e.g. this will be dealed seperately via payment option). Could you please kindly advise what would be the best way to do this? I have specify something like this in user model rules:


array('balance', 'unsafe', 'on'=>'update')

but that doesn’t seem to work even the update form still have ‘balance’ textfield.

Also, what would be the right way to top-up user balance (e.g. all transactions will be recorded in payment table and the user balance will be updated accordingly). Shall it be a payment module or extension or component? Any advice is greatly appreciated as I am a total Yiibie :D Thank you!

this will prevent updates in massive upadates but it dos not hide the field in form. you have to do this on your own. however even if the field is present - with ‘unsafe’ rule you should not be able to change the value.

Thank you for your reply. I have checked


$model->isNewRecord

and if not I disable the textfield but I expect if we specify


array('balance', 'unsafe', 'on'=>'update')

there will be an error raised when


$_POST['user']['balance']

detected but it doesn’t seem so.

Anyway, I am now facing a challenge of finding the right way to top-up user balance (e.g. all transactions will be recorded in payment table and the user balance will be updated accordingly). Shall it be a payment module or extension or component? Could anyone show me an efficient way or example please if you knew one. Thank you!

there is no error - the field is just not assigned during massive assignement (i.e. $model->attributes = $_POST[‘ModelName’])

also ‘on’=>‘update’ does not refer anyway to isNewRecord property, but (and only) $model->scenario = ‘update’ (which is set by default for records retrieved from DB)

Oh I see, so is that mean I should specify:


array('balance', 'unsafe', $model->scenario = 'update')

in model function rules? Thank you!