dongqiuqiu4736 2019-05-07 13:12
浏览 43

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

I'm working with my server-side validation with inspiring from laravel. I'm struggling putting validation in my modal and also with type="submit" button. 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?

  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; 
}
  • 写回答

1条回答 默认 最新

  • drxdai15012937753 2019-05-07 15:57
    关注

    I you want you can submit the form from your js code instead of using submit button. In Jquery it will look like this:

    $("#MyForm").submit()
    

    Using that you can control when the form is submitted according to the modal. Hope it helped.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等