Skip to content

Instantly share code, notes, and snippets.

@lav45
Created December 28, 2016 18:48
Show Gist options
  • Save lav45/b758aa6ef72d4a17af70a3a869dca9c3 to your computer and use it in GitHub Desktop.
Save lav45/b758aa6ef72d4a17af70a3a869dca9c3 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: lav45
* Date: 28.12.16
* Time: 21:18
*/
namespace demo;
use Yii;
use yii\db\ActiveRecord;
use yii\web\Controller;
use yii\widgets\ActiveForm;
use unclead\widgets\MultipleInput;
/**
* Class Post
* @package demo
*
* @property integer $id
* @property string $emails json
*/
class Post extends ActiveRecord
{
public function rules()
{
return [
// each не подайдет ((
// https://github.com/yiisoft/yii2/issues/12414
[['emailList'], 'safe'],
];
}
/**
* @return array
*/
public function getEmailList()
{
return json_decode($this->emails, true);
}
public function setEmailList(array $data)
{
$this->emails = json_encode($data, 320);
}
}
class SiteController extends Controller
{
public function actionCreate()
{
$model = new Post();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
}
return $this->render('create', [
'model' => 'model',
]);
}
}
// _form.php
/**
* @var $model Post
*/
$form = ActiveForm::begin();
echo $form->field($model, 'emailList')->widget(MultipleInput::class);
echo '<button type="submit">Save</button>';
ActiveForm::end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment