将dropzone与正常形式集成

时间:2019-12-15 12:15:27

标签: javascript jquery dropzone.js dropzone

我已使用jquery成功将dropzone与表单集成在一起。但是,我对其他表单输入的验证存在问题。似乎其他形式的输入不尊重验证,例如“ required”。我也尝试使用欧芹进行验证,但无法正常工作。当我删除dropzone字段时,验证效果很好。 这是表格。

<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>.

这是jquery代码:

// 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);
        });

    }
};

0 个答案:

没有答案