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

  1. How to 'stick' a message to the desired form element?
  2. 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] 

    1 answer 1

    If I understand the question correctly, then you can stick the "message" like this:

      $('#w0').yiiActiveForm('updateMessages', { 'item-imagefiles-'+id+'': ['sometext'], }, true); 

    Of course, it is better to assign an id to the form explicitly, using # w0 can lead to a bug, well, I had such an applicant, I warned.

    It also turns out that you still need to code the array, for example, where all the assigned IDs of the new dynamic inputs are stored, so that you can conveniently take them from there and also attach the necessary event handlers to them. Here also pay attention to this piece of code https://github.com/yiisoft/yii2/blob/master/framework/assets/yii.activeForm.js#L498 That is, you can apparently use something like this:

     $('#w0').yiiActiveForm('validateAttribute',$form, attribute, forceValidate, validationDelay);