Yii2 adding an extra field to the form to add to database - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, May 30, 2021

Yii2 adding an extra field to the form to add to database

On an existing project where there is a table with 3 fields (ID, name, label)

  `id` int(11) NOT NULL,
  `name` varchar(32) DEFAULT NULL,
  `label` varchar(1) DEFAULT 'A'

Currently on the page where to add new record to the above table there is the form with only one field for the 'name', and works fine, and I need to add another field for the 'label' field; so I go to the related model (models/Products.php) and I have:

    public function attributeLabels()
class Products extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'products';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 32],
            [['name'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', 'Name'),
        ];
    }

I add the following to the above file, but no new input field is added to the form on the page

public function attributeLabels()
{
    return [
        'id' => Yii::t('app', 'ID'),
        'name' => Yii::t('app', 'Name'),
        'label' => Yii::t('app', 'Prefix'),
    ];
}

I also tried adding to the rules like this, but no joy

public function rules()
{
    return [
        [['name', 'label'], 'string', 'max' => 32],
        [['name', 'label'], 'unique'],
    ];
}

Appreciate if you can point me to what I am missing.



source https://stackoverflow.com/questions/67756659/yii2-adding-an-extra-field-to-the-form-to-add-to-database

No comments:

Post a Comment