Form with FileInput and update action

Hi,

I’m trying to construct form in which i will have FileInput for image upload.

Problem is with upload form. I would like to display previously uploaded image/filename and let user to select new image or leave this field unchanged (i think its standard scenario).

I’ve tried somthing like:




    

    if (Yii::$app->controller->action->id != 'update') {

    }else{

        echo($form->field($model, 'thumbnailFilename')->textInput(['readonly' => true]));

    }

       

    echo($form->field($model, 'thumbnail')->fileInput());



But when "thumbnail" is required in model, form is not valid on submit.

How should I build form and model for this scenario?

I think the best way is to define different scenarios (http://www.yiiframework.com/doc-2.0/guide-input-validation.html) with different rules. In the update scenario, "thumbnail" field should not be a required field.

Thanks. Finally it’s working with rules code:




[['thumbnail'], 'required', 'when' => function($model) {

                return Yii::$app->controller->action->id == 'create';

            }, 'whenClient' => "function () { return " . Yii::$app->controller->action->id == 'create' . "; }"]



whenClient solution looks a little bit strange, but it’s the only solution I found to get client-side validation work.