Well, I can restrict users access permissions(i.e. view, create, update or delete) to forms or views based on access control using behaviors.
But I wonder how I can restrict a specific user from editing some of the fields in the form i.e. allow specific fields is read-only for some users and editable by some users.
Can I provide any kind of access rule in the model or attach some rule in the _form.php itself.
As a rule of thumb: If it’s possible in PHP then it’s possible in Yii.
As far as I know, the framework core doesn’t provide any built in mechanism for that. You have to check user’s permissions in the form and add “disabled” attribute dinamically.
Also, you have to check the permissions in the model and declare attribute as safe or unsafe dinamically. Implementation may vary, depending on the usage of scenarios.
Thanks Timmy78 You have spotted the problem correctly. Now If I remove the "age" from the integer rule, the data is not getting inserted in that field.
Thank you so much once again.
However I have to make a little modification i.e. put the suggested code before the line "if ($model->load(Yii::$app->request->post()) && $model->save())" like
public function actionCreate()
{
$model = new PatientEntry();
if(Yii::$app->user->can('admin'))
$model->scenario = 'admin';
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}