Answer the question
In order to leave comments, you need to log in
[[+content_image]]
Why is the form not validating in Yii2?
Hello.
Faced such a problem.
Specified in the rules
[['formFile'], 'file', 'extensions' => 'jpg', 'skipOnEmpty' => false]
and now the fileInput field does not pass validation, because supposedly I did not upload the file (although in fact uploaded).
If you remove this validation and do
[['formFile'], 'file', 'extensions' => 'jpg', 'skipOnEmpty' => true]
everything works as it should, and the file is saved.
In the controller:
private function uploadPromoFile($model)
{
// Get the upload folder
$uploadPath = $this->module->getUploadPath();
$model->
if (!$model->formFile) {
return false;
}
// Form the file name
$imageName = md5_file($model->formFile->tempName) . '.' . $model->formFile->extension;
// Form the full absolute path to the file
$absoluteFilePath = $uploadPath . '/'. $imageName;
// Load file
$model->formFile->saveAs($absoluteFilePath, false);
return $imageName;
}
Help, please, to understand.
Answer the question
In order to leave comments, you need to log in
Use 'extensions' => 'png, jpg, jpeg, gif'. And keep in mind that jpg and jpeg are different image validation extensions. And for errors there is $model->errors. In this array you will find what you need)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question