0

There is a database field called path it is text type and it is used to store paths marking strings as CSV, something like the following:

*:*,site/about,site/* Originally, it is used as a text input field like the following:

<?= $form->field($model, 'path')->textarea(['rows' => 6]) ?>

I want to, temporary, convert those paths into a dropDownSelect list in the view, then in the controller I will implode the selected items into string again to be validated and saved.

In the view _form.php I have the following:

<?= $form->field($model, 'path')->dropDownList($model->getPathSelectList($model->path), ['multiple' => 'multiple']) ?>

In the controller:

public function actionCreate()
    {
        $model = new StaticHtml();
        // var_dump($model->path);
        // die();
        if (is_array($model->path)) $model->path = implode(',', $model->path);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            $model->path = $model->getPathsList(Yii::$app->getRequest()->getQueryParam('path'));
            return $this->render('create', [
                'model' => $model,              
            ]);
        }
    }

Please Notice the two commented lines var_dump($model->path) and die() I set them to debug, because every time I select from the dropDownList and submit the form, the validation fails and tell me that Path must be a string and of course there is no any selection reserved unlike other fields and the var_dump($model->path) prints NULL:

enter image description here

In the code above, there is two model's methods:

  1. getPathsList() it is called from the controller and it convert an initial path to path marks and set them as csv string.
  2. getPathSelectList in the view and it convert the csv into an associative array which its keys and values are equals for each one to be data list for the dropDownList field.

Till this point, I think it will need more for update, but however, What's the problem in my code that prevents posting the $model->path so it prints NULL with var_dump()?

I'm pretty sure that it is a problem in receiving data from the form because I tried to:

var_dump(Yii::$app->getRequest()->post('path'));
var_dump(Yii::$app->getRequest()->post('title')); 
var_dump(Yii::$app->request->post('path'));

and all of them returns NULL

SaidbakR
  • 13,303
  • 20
  • 101
  • 195

1 Answers1

0

if your validation rule you can add

skipOnEmpty => true

Otherwise you can override the check this way

['path', 'required', 'isEmpty' => function ($value) {
    return empty($value);
}]
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thank you very much, but I used `beforeValidation` callback in the model to perform the transformation of the value. The point still not clear, is why I could not able to receive the value(s) submitted from the form in the controller?! – SaidbakR Jul 31 '16 at 13:51
  • 1
    1 ) - Have you tried the suggestion provided .. what's happend .. you have error .. or is not executed ... 2 ) if you perform a beforeValidation .. this could happend at model level before the sending to the controller .. (beforeValidation should be execute .. before a validation function call is performed )... anyway a good place for your conversion is the afterFind() method in model and the controller before safe so don't interfere with validation .. – ScaisEdge Jul 31 '16 at 14:02
  • Do you mean the fail of receiving data from the form in the controller due to the data submitted to the model first, so the validation fail, and then it does not pass the data to the controller? – SaidbakR Jul 31 '16 at 14:19
  • No .. i don't know your code and i can't affirm this without a deep analisys .. .But firts what' happend with check for empty value .. (i think the main problem is this .. ) – ScaisEdge Jul 31 '16 at 14:21