T
T
tkirsan4ik2016-04-11 00:51:32
Yii
tkirsan4ik, 2016-04-11 00:51:32

Yii2. How to redefine the name of the input field on the form not by the name of the field in the database?

Hello dear experts!
I need to name the input fields on the form not by the name of the field in the database, but as I want. Example: there is a "name" field in the database, I need the name on the form of the input field to sound like "name10", but in the database the values ​​​​are written exactly in the "name" field.
Can you please tell me how this can be implemented?
PS I'm new to yii2.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita, 2016-04-11
@tkirsan4ik

In the model:

public function getName10() {
  return $this->name;
}

public function setName10($value) {
  $this->name = $value;
}

Use like this:
$model->name10;

M
Maxim Timofeev, 2016-04-11
@webinar

If you are interested in the name in the form label, then for this there is:

public function attributeLabels()
    {
        return [
            'name' => 'Your name',
            'email' => 'Your email address',
            'subject' => 'Subject',
            'body' => 'Content',
        ];
    }

If the name of the name attribute of the form itself, then:
Create a variable, public $myvar;
write validation rules for it, a label and a function
public function setOldvar() {
  return $this->oldvar = $this->myvar;
}

Also read about beforeSave www.yiiframework.com/doc-2.0/yii-db-baseactivereco...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question