Reputation: 6073
I have a "BTeam" model class with attributes id, name, created. I generated this with Gii.
I added "TimestampBehavior" which fills the "created" field during model creation with the current timestamp.
How can I remove the field from the "add" page:
My BTeam class:
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class BTeam extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'b_team';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['created'], 'safe'],
[['name'], 'string', 'max' => 200]
];
}
public function behaviors() {
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created']
]
]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'created' => 'Created',
];
}
}
Upvotes: 1
Views: 971
Reputation: 498
You can also add this function to model and generate CRUD with gii(Apply Overwrite)
public function safeAttributes ( ){
return [
'id' ,
'name' ,
];
}
This method must return the only fields which are required for display of fields in create/add page(Generated by gii).
Upvotes: 0
Reputation: 355
Simply remove it from the _form in view of said controller. In rules its safe, so it should be ok.
Upvotes: 2