1

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:

enter image description here

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',
    ];
}
}
breakline
  • 5,776
  • 8
  • 45
  • 84

2 Answers2

2

Simply remove it from the _form in view of said controller. In rules its safe, so it should be ok.

Nimer
  • 357
  • 6
  • 12
0

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).

Gireesh
  • 468
  • 7
  • 13