2

I have successfully integrated dropzone with my form using jquery. However, I have an issue with validation of the other form inputs. It seems the other form inputs do not respect the validation such as "required". I also tried using parsley for validation but does not work. when I remove the dropzone field, the validation works well. Here is the form.

<form class="form-vertical"
                              id="createPropertyForm"
                              enctype="multipart/form-data"
                              method="POST"
                              >
                            <div class="col-md-12 col-lg-12 col-sm-12" >
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class=" control- 
label">Country</label>
                                        <div class="inputGroupContainer">
                                           <input id="country" 
name="country" placeholder="Country" class="form-control" required 
value="" type="text">
                                        </div>
                                    </div>
                                </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control- 
label">County</label>
                                            <div 
class="inputGroupContainer">
                                               <input id="county" 
name="county" placeholder="County" class="form-control" required value="" 
type="text">
                                            </div>
                                        </div>
                                    </div>
                            </div>

                            <div class="col-md-12 col-lg-12 col-sm-12" >
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class=" control- 
label">Town</label>
                                        <div class=" inputGroupContainer">
                                       <input id="town" name="town" 
placeholder="Town" class="form-control" required value="" type="text">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control- 
label">Postcode</label>
                                        <div class="inputGroupContainer">
                                            <input id="postcode" 
name="postcode" placeholder="Postcode" class="form-control" required 
value="" 
type="text">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-12 col-lg-12 col-sm-12" >
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class=" control- 
label">Description</label>
                                        <div class=" inputGroupContainer">
                                           <textarea id="description" 
name="description" placeholder="Description" class="form-control" 
required="true" value="" type="text"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control- 
label">Address</label>
                                        <div class="inputGroupContainer">
                                  <input id="address"  name="address" 
placeholder="Address" class="form-control" required value="" type="text">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12 col-lg-12 col-sm-12" >
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class=" control- 
label">Bedrooms</label>
                                        <div class=" inputGroupContainer">
                                            <select class=" form-control" 
name="bedrooms" id="bedrooms" required>

                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label 
">Bathrooms</label>
                                        <div class=" inputGroupContainer">
                                                <select 
class="selectpicker bathrooms form-control" name="bathrooms" 
id="bathrooms" 
required>

                                                </select>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-12 col-lg-12 col-sm-12" >
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class=" control- 
label">Price</label>
                                        <div class="inputGroupContainer">
                                          <input id="price" name="price" 
 placeholder="Price" class="form-control" required value="" type="number">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control- 
label">Property Type</label>
                                        <div class=" inputGroupContainer">

                                                <select 
class="selectpicker form-control" name="propertyType" id="propertyType">
                                                    <option 
value="">Choose type</option>
                                                    <?php
                                                    foreach 
($propertyTypes as $propertyType) {

                                                        ?>
                                                        <option value="<?= 
$propertyType->id ?>"><?= $propertyType->title ?></option>
                                                        <?php
                                                    }
                                                    ?>

                                                </select>

                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-12 col-lg-12 col-sm-12" >
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class=" control- 
label">Type</label>

                                        <div class="col-md-12">
                                            <div class="col-md-6 ">
                                                <label><input type="radio" 
name="type"  class="form-control type" required>Sale</label>
                                            </div>
                                            <div class="col-md-6">
                                                <label><input type="radio" 
name="type"   class="form-control type" required>Rent</label>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control- 
label">Upload Image</label>
                                        <div class=" inputGroupContainer">
                                            <div class="dropzone" 
id="create-dropzone" >
                                                <div class="fallback">
                                                    <input name="file" 
type="file" required/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6 col-sm-6">
                                    <div class="dropzone-previews"></div>
                                </div>
                            </div>
                            <div class="col-md-6 col-sm-6">
                                <button class="btn btn-success btn-lg" 
type="submit" id="submitCreateForm"> Submit </button>
                            </div>
                        </form>.

Here is the jquery code:

// Parsley for form validation
$('#createPropertyForm').parsley();
$('#editPropertyForm').parsley();

Dropzone.options.createDropzone = {
    url: `${baseUrl}administrator/properties`,
    autoProcessQueue: false,
    parallelUploads: 1,
    maxFiles: 1,
    maxFileSize:2048,
    uploadMultiple: false,
    acceptedFiles: "image/*",

    init: function () {

        var submitButton = document.querySelector("#submitCreateForm");
        var wrapperThis = this;

        submitButton.addEventListener("click", function (e) {
            e.preventDefault();

            if (wrapperThis.files.length) {
                wrapperThis.processQueue();
            } else {
                wrapperThis.submit();
            }

        });

        this.on("addedfile", function (file) {

            var removeButton = Dropzone.createElement("<button class='btn btn-block btn-danger'><i class='fa-times fa'></button>");

            removeButton.addEventListener("click", function (e) {
                e.preventDefault();
                e.stopPropagation();

                wrapperThis.removeFile(file);
            });

            file.previewElement.appendChild(removeButton);
        });

        this.on('sending', function (data, xhr, formData) {
            formData.append("country", $("#country").val());
            formData.append("county", $("#county").val());
            formData.append("town", $("#town").val());
            formData.append("postcode", $("#postcode").val());
            formData.append("description", $("#description").val());
            formData.append("address", $("#address").val());
            formData.append("bathrooms", $("#bathrooms").val());
            formData.append("price", $("#price").val());
            formData.append("bedrooms", $("#bedrooms").val());
            formData.append("propertyTypeId", $("#propertyType").val());
            formData.append("type", $(".type").val());
        });

        this.on('success', function (files, response) {

                toastr.success(response.message);

            setTimeout(function () {
                location.reload();
            }, 1000);

        });

        this.on('error', function (file, error, xhr) {

            file.status = 'queued';
            if (xhr.status === 422){
                toastr.error('An error occurred, please confirm that you have filled all inputs and try again');
            }else{
                toastr.error('An error occurred');
            }

        });

        this.on("maxfilesexceeded", function(file) {
            wrapperThis.removeFile(file);
        });

    }
};
Twisty
  • 30,304
  • 2
  • 26
  • 45
Franklin Ekoh
  • 83
  • 1
  • 11
  • Please add your code, without it we can't say what is wrong. – Zydnar Dec 15 '19 at 12:17
  • @Zydnar done that. please take a look. everything works fine. except the other form input do not validate. e.g "required" html attribute does not work – Franklin Ekoh Dec 15 '19 at 12:32
  • HTML attribute required works, but not in the way you think. If you define pattern https://www.w3schools.com/tags/att_input_pattern.asp it can be checked as it should. Problem is native required property without JavaScript is useless, because inputs are marked as red only if you blur out of them, so if you don't focus into that fields and than click somewhere else, they are not checked as invalid at all. – Zydnar Dec 15 '19 at 12:43
  • @Zydnar, so whats the solution to this. because when I remove the file input field on that form i.e . the one that initiates dropzone, everything works as expected. I even tried using parsley but it doesn't work too. my guess is that parsley relies on the html attribute. What is the solution? – Franklin Ekoh Dec 15 '19 at 12:58
  • i don't know parsley, sry. You may try some alternative validation to parsley. General idea is to validate inputs with JS before any changes to inputs, and also on key up. – Zydnar Dec 15 '19 at 14:03

0 Answers0