Yii2: File input forces uploading file with every update. even thumb image value available but it gives error to thumb image is required.

Created on 29 Nov 2017  路  5Comments  路  Source: yiisoft/yii2

model code :- [['thumbnail_image'], 'required', 'message' => 'Please upload an image.'],

form code :-
$form->field($model, 'thumbnail_image')->fileInput(['accept' => 'image/*', 'onchange' => 'readURL(this)'])->label('Thumbnail Image', ['class' => 'control-label thumbnail-image-class'])

File input forces uploading

file with every update. even thumb image value available but it gives error to thumb image is required.
-

need more info

All 5 comments

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

when I create a new record or change the attribute in update it works correctly. However when I update the record without changing the file attribute I get a javascript error 'thumb image is required' despite the file is already saved and I don't want to update it

Please provide code and steps to reproduce it starting with clean basic app. Thanks.

You can use the when and whenClient options.

[['attribute'], 'required', 'when' => function($model) {
  return $model->isNewRecord;
}, 'whenClient' => '() => { return !/\?id=\d+/i.test(document.location.href); }' ],

For server side we can check if the record is new $model->isNewRecord.
Client side depends on how you track records, the example above will check the url for an id query string, if one doesn't exist the rule applies.

Yes its work,
thank you so much.

Was this page helpful?
0 / 5 - 0 ratings