[[+content_image]]
D
D
dantemante2016-05-05 21:06:31
Yii
dantemante, 2016-05-05 21:06:31

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

[[+comments_count]] answer(s)
D
Deleted User, 2016-05-06
@Toler

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)

W
webmister, 2017-05-05
@webmister

Also check the beforeValidate() method if you have it declared in the model. And make sure it returns TRUE.

public function beforeValidate(){
      //some code

     return parent::beforeValidate();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question