unique validator with file upload

hi community,

I have stuck in my project.

i have unique validation of email and username.

also have profile pic to upload on user creation.

usermodel:


  

[['name', 'username','email','pass','profilepic'], 'required'],

[['email','username'], 'unique', 'targetClass' => '\backend\models\User', 'message' => 'This email or username has already been taken.'],

usercontroller:

to validate unique filed i need to use $model->validate();


if ($model->load(Yii::$app->request->post()) && $model->validate()) 

{ 

}

my issue is if im using $model->validate() profilepic is get null after submitting form.

if im not using validate() unique validation is not working.

can someone please help me in this.

regards.

if you post controller, model and view code. Then people may be able to help. cant do much with the tiny snippet

#sitecontroller:#


 public function actionUpload()

    { 

        $model = new User();

       $model->scenario = 'upload';

        if ($model->load(Yii::$app->request->post()) ) { 

            if($model->validate())

            {

                $file = UploadedFile::getInstance($model, 'profilepic');

               if($file){

                    $file->saveAs(\Yii::$app->basePath.'/web/uploads/'.$file->name);

                    $model->profilepic = $file->name; 

                    $model->save(false);

                    Yii::$app->getSession()->setFlash('success', 'Successfully registered.');

                        return $this->render('upload', [

                    'model' => $model,

                    ]);

                 }

            }

            else

            {

                echo "<pre>";

                print_r($model->geterrors());exit;

                Yii::$app->getSession()->setFlash('error', 'Error in registration.');

                    return $this->render('upload', [

                    'model' => $model,

                    ]);

            }

        }

#site/upload#




    <?php $form = ActiveForm::begin(['id' => 'form-signup',

        'type' => ActiveForm::TYPE_HORIZONTAL,

        'formConfig' => ['showErrors' => true,'showLabels' => true],

        'options' => ['enctype' => 'multipart/form-data']

      

    ]); 

             ?>

      

 <div class="form-group kv-fieldset-inline">

  <?= $form->field($model, 'profilepic')->widget(FileInput::classname(),[ 'pluginOptions' => ['showUpload' => false]]); ?>

  </div> 

                <div class="form-group">

                    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>

                </div>

            <?php ActiveForm::end(); ?>

#User Model#




public function rules()

    {

        return [

         

            ['profilepic', 'required'],

            [['profilepic'], 'required', 'on' => 'upload'],

            ['profilepic', 'file', 'extensions'=>'jpg, jpeg, png'], 


             

        ];

    }   

     public function scenarios()

    {

        

        $scenarios = parent::scenarios();


        $scenarios['upload'] = ['profilepic'];//Scenario Values Only Accepted 


        return $scenarios;


    }

validate() method is not working with file upload.

webin

I think you should call validate() (or save()) method before you call getInstance(). Look at this code, it may be helpful:




if (Yii::$app->request->isPost) {

    $model->load(Yii::$app->request->post());

    $model->profilepic = UploadedFile::getInstance($model, 'profilepic');

    if ($model->validate()) {

        // do your things...

    }

}






still facing the same issue

can nyone please guide me to solve this?