Sunday, March 12, 2017

Save multiple textInput Array value using Yii2


controller action:

namespace app\controllers;

use Yii;
use yii\base\Model;
use yii\web\Controller;
use app\models\Experienceskills;

    public function actionExperience()
    {
        $settings = Experienceskills::find()->indexBy('id')->all();
        if (Model::loadMultiple($settings, Yii::$app->request->post()) && Model::validateMultiple($settings)) {
            foreach ($settings as $setting) {
                $setting->save(false);
            }
            Yii::$app->session->setFlash(
                'success',
                Yii::t(
                    'user',
                    'Your experience have been updated.'
                )
            );
            return $this->redirect(['profile']);
        }

        return $this->render('updateexp', ['settings' => $settings]);
    }

view:

use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin();

foreach ($settings as $index => $setting) {
 echo $form->field($setting, "[$index]skill_id")->label('skill');
echo $form->field($setting, "[$index]experience_year")->label('Experience');
}

ActiveForm::end();

No comments:

Post a Comment