简体   繁体   中英

how to build form of multiple models in yii2?

I am trying to create a form that use multiple model instances and submit them all at once (tabular input) , i have read this article on wiki yiiframework , but what i am trying to do is to build a custom model that contains properties and attributes from multiple models then use this model in my form.

[Example]

model1

namespace app\models;

use Yii;
use \yii\db\ActiveRecord;

class Country extends ActiveRecord
{
   ........
}

molel2

 namespace app\models;

use Yii;
use \yii\db\ActiveRecord;

class User extends ActiveRecord
{
   ........
}

Creating your own model is simple. The more detailed explanation could be found by following link .

Your own model might look like this:

namespace app\models;

use yii\base\Model;

class Custom extends Model
{
    public $name;
    public $surname;
    public $email;
    public $country;
    public $city;

    public function rules()
    {
        return [
        // the name, surname, city attributes are required
        [['name', 'surname', 'country', 'city'], 'required'],

        // the email attribute should be a valid email address
        ['email', 'email'],
    ];
    }

    public function customFunction()
    {
        //some custom things
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM