zibra
(Tomasz Przysiwek)
December 9, 2014, 10:37am
1
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?
_ker
(_ker_)
December 9, 2014, 11:17am
2
zibra:
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.
zibra
(Tomasz Przysiwek)
December 9, 2014, 3:39pm
3
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.