简体   繁体   English

提交表单时如何防止模态隐藏?

[英]How can I prevent modal to hide when submitting form?

I'm working with my server-side validation with inspiring from laravel. 我正在使用laravel鼓舞人心的服务器端验证。 I'm struggling putting validation in my modal and also with type="submit" button. 我正在努力将验证放在我的模态中,并且还使用type =“submit”按钮。 When I click the save button with empty inputs the modal is closing and when I open the modal again the error is already there. 当我单击带有空输入的保存按钮时,模态正在关闭,当我再次打开模态时,错误已经存在。 I want to avoid modal to hide when submitting the form. 我想在提交表单时避免使用模态隐藏。 How can I stop modal to close if the inputs are empty? 如果输入为空,如何停止模态关闭? I'm using type="submit" button? 我正在使用type =“submit”按钮?

  addUnitCategory : function() {
        axios({
            method : "POST",
            url : this.urlRoot + "unit_category/add_unit_category.php",
            data : {
                description : this.unit_category_description,
                unit : this.unit_category_unit
            }
        }).then(function (response){
            vm.retrieveUnitCategory();
            swal("Congrats!", " New unit category added!", "success");  
            vm.clearData();
        }).catch(error => {
            console.info(error.config);
        });
    },
    validationCategoryUnit : function() {
        if (this.unit_category_description || this.unit_category_unit) {
            $('#myModal').modal('hide');
            vm.addUnitCategory();
            return true;
        }
        if (!this.unit_category_description || !this.unit_category_unit) {
            return false;
        }   
    },

   <--Form-->
   <form method="post" @submit="validationCategoryUnit()">
   <--Button-->
   <div class="modal-footer">
    <button type="submit" @click="validationCategoryUnit()">Save</button>
   </div>

   <--Validation-->

   if($_SERVER['REQUEST_METHOD'] == "POST") {
    $validation = new Validation();
    $data = [
        "unit_category_description" => $_POST['unit_category_description'] ? 'unit_category_description' : '',
        "unit_category_unit" => $_POST['unit_category_unit'] ? 'unit_category_unit'  : '',
    ];
    $validation->validate($data, [
        "unit_category_description" => "required|maxlen:45",
        "unit_category_unit" => "required|minlen:5|maxlen:20"
    ]);

    $errors = $validation->getErrors();
} else {
    $data = [
       "unit_category_description" => "",
       "unit_category_unit" => ""
    ];
    $errors = [
        "unit_category_description" => "",
        "unit_category_unit" => ""
    ];
}
 if (isset($_POST['submit'])) {
    if($data) {
    echo $data; 
}

I you want you can submit the form from your js code instead of using submit button. 我希望你能从你的js代码提交表单,而不是使用submit按钮。 In Jquery it will look like this: 在Jquery中它看起来像这样:

$("#MyForm").submit()

Using that you can control when the form is submitted according to the modal. 使用它可以控制何时根据模态提交表单。 Hope it helped. 希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM