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.



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

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


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.


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


 public function actionUpload()


        $model = new User();

       $model->scenario = 'upload';

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



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



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


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

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

                    'model' => $model,






                echo "<pre>";


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

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

                    'model' => $model,





    <?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 class="form-group">

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


            <?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.


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->profilepic = UploadedFile::getInstance($model, 'profilepic');

    if ($model->validate()) {

        // do your things...



still facing the same issue

can nyone please guide me to solve this?