anchoress
anchoress

Reputation: 111

Yii2 ActiveForm field widgets not posting values to the model

I'm trying to incorporate a DateTimePicker widget into an ActiveForm in Yii2 as a field like this in the View:

 $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',
        ],
    ]); 

Upon submission, it validates as empty and does not pass a value to the model. The widget also does not display the default value in the text field, despite the value attribute being set.

I can make the process work by displaying the widget like this in the view:

 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',
       ],
   ]);

with this in the 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]);
     }

The cooresponding values in the model look like this:

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'],
    ];
}

The text input fields in my form work fine. Is there something more I need to add to the controller or the model to ensure the widget values are posted on submit?

I am using the Kartik DateTimePicker widget https://github.com/kartik-v/yii2-widget-datetimepicker, but I have tried using other widgets with the same problem, so presumably I'm missing something in my code.

Upvotes: 0

Views: 2206

Answers (2)

anchoress
anchoress

Reputation: 111

I was able to fix this by putting the $model->execquery() call after the values were set:

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]);
     }

Upvotes: 0

Kiran Muralee
Kiran Muralee

Reputation: 2070

I have gone through the documentation of DateTimePicker widget,I have seen the usage with model and Active Form (with no default initial value) like

// Usage with model and Active Form (with no default initial value)
echo $form->field($model, 'date_from')->widget(DateTimePicker::classname(), [
    'options' => ['placeholder' => 'Enter event time ...'],
    'readonly' => true,
    'removeButton' => false,
    'pluginOptions' => [
        'format' => 'yyyy-mm-ddThh:ii:ssZ',            
        'autoclose' => true,            
    ]
]);

Source http://demos.krajee.com/widget-details/datetimepicker

I have not seen the usage of 'value' => 'xxxxxxx' there.(If value is used then there is a name attribute also in other examples).But the documentation says the widget can have all parameters that one would pass for any Yii Input Widget. But my suggestion is to remove it and give a try.If you want to set a default value,then pass it from the controller.

The controller code should be like

 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]);
         }
    }

Otherwise you always have the option to use

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',
       ],
   ]);

Upvotes: 0

Related Questions