php - Yii2 form field stays blank even when filled

688

I have an update form which works as expected with one exception - thereviewtextarea doesn't wan't to pass through the validation rules. When i fill it and try to update the form i getReview field is empty (or sort of this). I can see this withvar_dump($model->getErrors()) in the controller. The$_POST['Author']['review'] got the value that i gave it but can't save it in$model->review column. I am usingCKEditor. Tried without it but without success. Here is my controller:

public function actionUpdate($id)
    {
        $model = $this->findModel($id, true);
        $settings = new Settings();
        if ($model->load(Yii::$app->request->post())) {
            var_dump($model->save());
            var_dump($model->getErrors());die;
            $languages = Lang::find()->all();
            foreach ($languages as $language) {
                if ($language->default != 1) {

                    $names = 'names_' . $language->url;
                    $varNames = Yii::$app->OutData->sanitize($model->$names);
                    $model->$names = $varNames;

                    $review = 'review_' . $language->url;
                    $varReview = Yii::$app->OutData->sanitize($model->$review);
                    $model->$review = $varReview;

                    $metaDesc = 'meta_desc_' . $language->url;
                    $varMetaDesc = Yii::$app->OutData->sanitize($model->$metaDesc);
                    $model->$metaDesc = $varMetaDesc;

                    $url = 'url_' . $language->url;
                    $varUrl = Yii::$app->OutData->sanitize($model->$url);
                    $model->$url = $varUrl;

                    $cBirth = 'country_birth_' . $language->url;
                    $varcBirth = Yii::$app->OutData->sanitize($model->$cBirth);
                    $model->$cBirth = $varcBirth;
                }
                else
                {

                    $model->names = Yii::$app->OutData->sanitize($model->names);
                    $model->review = Yii::$app->OutData->sanitize($model->review);
                    $model->meta_desc = Yii::$app->OutData->sanitize($model->meta_desc);
                    $model->url= Yii::$app->OutData->sanitize($model->url);
                    $model->country_birth = Yii::$app->OutData->sanitize($model->country_birth);
                }
            }       

            //записване на изображенията + thumb
            if (isset($_POST["Author"]["imageFiles"]) and ! empty($_POST["Author"]["imageFiles"])) {

                $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles');
                if (isset($model->imageFiles) and count($model->imageFiles) > 0) {
                    foreach ($model->imageFiles as $key => $file) {
                        $parseProdTitle = MakeURL::parseImageName($model->names.'_'.$model->id);
                        $fileName = $parseProdTitle . '_' . $model->id . '.' . $file->extension;
                        $fileName = Yii::$app->translate->cyr_to_lat($fileName);
                        $model->filename = $fileName;
                        $model->update();
                        $pic = Yii::getAlias('@frontend/web') . '/authors/thumb-270/' . $fileName;
                        $pic2 = Yii::getAlias('@frontend/web') . '/authors/' . $fileName;
                        $file->saveAs(Yii::getAlias('@frontend/web') . '/authors/' . $fileName);
                        $image = file_get_contents(Yii::getAlias('@frontend/web') . '/authors/' . $fileName);
                        file_put_contents($pic, $image);
                        $model->resizeImg($pic);
                        $settings->compress($pic, $pic, 90);
                        $settings->compress($pic2, $pic2, 90);
                    }
                }
            }

            $model->update();

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

The part of the view with thereview field:

echo '<div >';
                    echo '<div >';
                      $textContent = 'review';
                      if (!$model->isNewRecord) {
                        $model->$textContent = OutData::showTXT($model->$textContent);
                      }
                      echo $form->field($model, 'review')->textArea();
                      echo "<script>
                          CKEDITOR.replace( 'Author[review]' );
                        </script>";
                    echo '</div>';
                  echo '</div>';

And finally the model rules:

public function rules()
    {
        $required = ['names', 'review', 'meta_desc', 'url', 'birthday', 'country_birth'];

        return [
            [$required, 'required'],
            [['active', 'sort'], 'required'],
            ['names', 'string', 'max' => 255],
            ['country_birth', 'string', 'max' => 255],
            ['review', 'string'],
            ['homeslider_review', 'string'],
            ['meta_desc', 'string', 'max' => 170],
            ['url', 'string', 'max' => 60],
            [['active', 'sort'], 'integer'],
            [['filename'], 'string'],
        ];
    }

All the other fields work in proper way. This is the only rebel. Thank you in advance!

172

Answer

Solution:

In your code everything is ok.If u don't want to validate the model, Try this one in your controller where u r saving your form.

var_dump($model->save(false));

This will solve your problem

People are also looking for solutions to the problem: php - Simple file urls wordpress, Twenty Thirteen

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.