A
A
alex995052019-07-04 12:07:32
Yii
alex99505, 2019-07-04 12:07:32

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

[[+comments_count]] answer(s)
M
Mikhail Bratenkov, 2019-07-04
@TheRikipm

Try to change
In the following way

$form = ActiveForm::begin(
        [
            'id' => 'form_sponsor_main'
            'options' => [
                'enctype'=>'multipart/form-data',
            ]
        ]
    );

Perhaps the file is not transmitted via POST at all. enctype is wrong.
For the future: if you encounter a similar error, look through DevTools what goes to the server and apply to the question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question