There is a form form field
$form->field($model, 'imageFiles[]')->fileInput(['accept' => 'image/*'])->label(false)
Fields are added dynamically on the client, via jQuery
$(document).on('click', '#add_image', function(e) { e.preventDefault(); var id = $('.block').length; console.log(id); var image_block = '<input type="hidden" name="Item[imageFiles]['+id+']" value=""><input type="file" id="item-imagefiles-'+id+'" name="Item[imageFiles]['+id+']" accept="image/*">'; $('.images').append(image_block); });
But Yii2 does not see them point-blank, as if there are none at all, i.e. the remaining fields are validated, and these fields do not exist at all :(
Found https://github.com/samdark/yii2-cbookbook/blob/master/book/forms-activeform-js.md
Writing
$('#w0').yiiActiveForm('add', { 'id': 'item-imagefiles-'+id+'', 'name': 'imageFiles['+id+']', 'container': '.field-item-imagefiles-'+id+'', 'input': '#item-imagefiles-'+id+'', 'error': '.help-block' });
Nothing happens at all, although as far as I understand, should this add field validation on the client?
PS: it is clear that you can send it to the server, return the answer, etc. But validation on the client interests.
On the client it does not work, I send it to the server, I get a response in json format, 2 problems
- How to 'stick' a message to the desired form element?
If the image field is not filled, it ignores it, because Yii itself if an empty field does not take it into account, i.e. I get images like this:
$ model-> images = UploadedFile :: getInstancesByName ('Item [images]');
If you leave 3 empty file entries 3, then validation will take place only for 2, the remaining 3 will not be validated at all, although the rule is set in the rule model
[['images'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxFiles' => 10]