Answer the question
In order to leave comments, you need to log in
Why is a field with type file erroneously not validated?
I display the form, I upload the file successfully, but when editing a model that already has a saved file, it does not save, because "the file is not selected". Model validation costs );
echo $form->field($model, 'image')->fileInput(['id'=>'image']);
ActiveForm::end();
What could be the reason?
The model is responsible for working with images:
class UploadForm extends Model
{
/**
* @var UploadedFile
*/
public $imageFile;
public $imageName;
public function rules()
{
return [
[['imageFile'], 'file', 'skipOnEmpty' => false],
];
}
public function upload($path, $name = null)
{
if ($this->validate()) {
$this->imageFile->saveAs($path .'/'.$name);
return true;
} else {
return false;
}
}
}
Answer the question
In order to leave comments, you need to log in
Try to change
In the following way
$form = ActiveForm::begin(
[
'id' => 'form_sponsor_main'
'options' => [
'enctype'=>'multipart/form-data',
]
]
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question