How to make only a looping image mandatory

Asked

Viewed 42 times

0

Good morning, I have a code that inserts images with a looping (for the user to insert at most 5 images), now the 5 images are as mandatory but I wanted to leave only one mandatory and the rest optional.

    <div class="crowd-field-wrap">
                                        <label><?php _e('Fotos', 'crowd'); ?> <em><?php _e('(Adicione 5 fotos, uma delas com seu logo ou foto pessoal para aparecer no seu cadastro.)', 'crowd'); ?></em>
                                        </label>
                                        <?php for ($i = 0; $i < CROWD_PLUGIN_MAX_IMAGES; $i++) : ?>
                                            <div class="crowd-file-upload">
                                                <div class="crowd-file-upload-box">
                                                    <input type="file" name="location_image[]" value="" required/>
                                                    <span><?php _e('Clique e envie sua foto', 'crowd'); ?></span>
                                                    <div class="crowd-file-upload-image"></div>
                                                    <a class="crowd-file-upload-trigger"></a>
                                                </div><!-- /.crowd-file-upload-box -->
                                            </div><!-- /.crowd-file-upload -->
                                        <?php endfor; ?>
                                    </div><!-- /.crowd-field-wrap -->

1 answer

0


From what I understand, the problem is in the attribute required that is being applied to all inputs generated by the loop.

In this case, you can apply the required only in the first input making a if when the value of $i the loop is equal to 0:

<input type="file" name="location_image[]" value="" <?php if($i == 0){ ?> required<?php } ?>/>
                                                                     ↑                  ↑

Or using echo:

<input type="file" name="location_image[]" value="" <?php if($i == 0){ echo "required"; } ?>/>

Thus, only the first input will be mandatory via required.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.