Yii2 ActiveForm字段小部件不向模型发布值

时间:2016-09-16 23:38:40

标签: php forms yii2 widget

我试图将DateTimePicker小部件合并到Yii2中的ActiveForm中作为视图中的字段:

 $form->field($model, 'date_from')->widget(DateTimePicker::className(),
    [
        'value' => '2013-01-01T00:00:00Z',
        'readonly' => true,
        'removeButton' => false,
        'pluginOptions' => [
            'format' => 'yyyy-mm-ddThh:ii:ssZ',
            'autoclose' => true,
            'class' => 'form-control',
        ],
    ]); 

提交后,它会验证为空,并且不会将值传递给模型。尽管设置了value属性,窗口小部件也不会在文本字段中显示默认值。

我可以通过在视图中显示这样的小部件来使这个过程工作:

 echo '<label class="control-label">From</label>';
    echo DateTimePicker::widget([
       'name' => 'ModelName[date_from]',
       'value' => '2013-01-01T00:00:00Z',
       'readonly' => true,
       'removeButton' => false,
       'pluginOptions' => [
           'format' => 'yyyy-mm-ddThh:ii:ssZ',
           'autoclose' => true,
           'class' => 'form-control',
       ],
   ]);

在Controller中使用它:

 public function actionName()
    {
          $model = new ModelName();
          if ($model->load(($data = Yii::$app->request->post())) && $model->execquery()) {
          Yii::$app->session->setFlash('formSubmitted');
          $model->from = time('yyyy-mm-ddThh:ii:ssZ', strtotime($data['ModelName']['date_from']));
          $model->to = time('yyyy-mm-ddThh:ii:ssZ', strtotime($data['ModelName']['date_to']));
          return $this->render('name', ['model' => $model]);
        }
        return $this->render('name', ['model' => $model]);
     }

模型中的相应值如下所示:

public $text1;
public $text2;
public $date_from;
public $date_to;
public $email;

/**
 * @return array the validation rules.
 */
public function rules()
{
    return [
        // positions and dates are required
        [['text1', 'text2', 'date_to', 'date_from'], 'required'],
        ['email', 'email'],
    ];
}

我的表单中的文本输入字段工作正常。是否还需要添加到控制器或模型中以确保在提交时发布窗口小部件值?

我正在使用Kartik DateTimePicker小部件https://github.com/kartik-v/yii2-widget-datetimepicker,但我尝试使用其他具有相同问题的小部件,因此我可能在代码中遗漏了某些内容。

2 个答案:

答案 0 :(得分:0)

我已经浏览了DateTimePicker小部件的文档,我已经看到了模型和活动表格的用法(没有默认初始值),如

 public function actionName()
    {
         $model = new ModelName();
         if ($model->load(Yii::$app->request->post()) && $model->save()) 
         {
             Yii::$app->session->setFlash('formSubmitted');                 
             return $this->render('name', ['model' => $model]);
         }
         else
         {
            $model->date_from= "2013-01-01T00:00:00Z";//some default value
            return $this->render('name', ['model' => $model]);
         }
    }

来源http://demos.krajee.com/widget-details/datetimepicker

我没有看到使用&#39;值&#39; =&GT; &#39; XXXXXXX&#39;那里。(如果使用了值,那么在其他示例中也有一个名称属性)。但是文档说小部件可以包含任何Yii输入小部件可以传递的参数。但我的建议是删除它并试一试。如果你想设置一个默认值,那么从控制器传递它。

控制器代码应该像

echo '<label class="control-label">From</label>';
    echo DateTimePicker::widget([
       'name' => 'ModelName[date_from]',
       'value' => '2013-01-01T00:00:00Z',
       'readonly' => true,
       'removeButton' => false,
       'pluginOptions' => [
           'format' => 'yyyy-mm-ddThh:ii:ssZ',
           'autoclose' => true,
           'class' => 'form-control',
       ],
   ]);

否则您始终可以选择使用

{{1}}

答案 1 :(得分:0)

我能够通过在设置值之后调用$ model-&gt; execquery()来解决这个问题:

public function actionName()
    {
          $model = new ModelName();
          if ($model->load(($data = Yii::$app->request->post()))) {
              Yii::$app->session->setFlash('formSubmitted');
              $model->from = time('yyyy-mm-ddThh:ii:ssZ', strtotime($data['ModelName']['date_from']));
              $model->to = time('yyyy-mm-ddThh:ii:ssZ', strtotime($data['ModelName']['date_to']));
              $model->execquery();
              return $this->render('name', ['model' => $model]);
          }
          return $this->render('name', ['model' => $model]);
     }