R
R
Ruslan Tilyaev2019-04-29 16:35:10
Yii
Ruslan Tilyaev, 2019-04-29 16:35:10

Why does image upload validation fail, yii2?

I'm trying to upload a picture, but the field does not pass validation, it says "you need to upload a file."
The form:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'passport')->fileinput()->label(false) ?>
<?= Html::submitButton('Отправить') ?>
<?php ActiveForm::end() ?>

Model:
public function rules()
    {
        return [
            [['passport'], 'file', 'extensions' => 'png, jpg'],
        ];
    }
public function upload(){
        if ($this->validate()){
            $path = 'img/store/' . $this->passport->baseName . '.' . $this->passport->extension;
            $this->passport->saveAs($path);
            return true;
        }else{
            return false;
        }
    }

Controller:
public function actionCreate()
    {
        $model = new Clients();

        $model->passport = UploadedFile::getInstance($model, 'passport');
        if($model->passport){
            $model->upload();
        }

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if ($model->save()) {
                Yii::$app->session->setFlash('success', 'Спасибо, мы получили вашу анкету. В скором времени мы с вами свяжемся.');
            } else {
                Yii::$app->session->setFlash('error', 'Ошибка... Попробуйте еще раз...');
            }
            return $this->refresh();
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2019-04-29
@slo_nik

Good afternoon.
Try doing this:

if ($model->load(Yii::$app->request->post()) && $model->save()) {
       $model->passport = UploadedFile::getInstance($model, 'passport');
        if($model->passport){
            $model->upload();
        }
// остальной код действия.
}

And, if you store the model in a row
then why in this condition you are trying to save the model again?
if ($model->save()) {
                Yii::$app->session->setFlash('success', 'Спасибо, мы получили вашу анкету. В скором времени мы с вами свяжемся.');

ps
This path must point to the web directory
I think it's better to use aliases here, something like this
$path = Yii::getAlias('@web') . 'img/store/' . $this->passport->baseName . '.' . $this->passport->extension;

R
Roman Sokolov, 2017-03-29
@jimquery

vskidka
select count (*)
from table1
where (A> 0) and (B < 9)
With the conditions it is not entirely clear from the question, but I hope it will not be difficult to prescribe the necessary ones.
"But the condition is also suitable for me if in one record A>0, and in the other record B<9."
If so, then instead of AND use OR
upd:
join's are easier to solve:

select count(*) as total
from table1 as t1 inner join table1 as t2
where t1.a > 0 and t2.b < 9

E
Eugene, 2017-03-29
@kevin

BOTH conditions must be met in the table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question