我有一个更新表单按预期工作但有一个例外 - review
textarea
并不通过验证规则。当我填写它并尝试更新表单时,我得到Review field is empty
(或者这样)。我可以在控制器中看到var_dump($model->getErrors())
。 $_POST['Author']['review']
获得了我给它的值,但无法将其保存在$model->review
列中。
我正在使用CKEditor
。没有它但没有成功的尝试。这是我的控制器:
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,
]);
}
}
包含review
字段的视图部分:
echo '<div class="row">';
echo '<div class="col-sm-12">';
$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>';
最后模型规则:
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'],
];
}
所有其他字段都以正确的方式工作。这是唯一的反叛者。提前谢谢!
答案 0 :(得分:0)
在您的代码中,一切正常。如果您不想验证模型,请在您的控制器中尝试使用此模型,以便保存表单。
var_dump($model->save(false));
这将解决您的问题