如何在需要时仅提交隐藏/显示字段数据之一-Laravel [英] How to submit only one of hide/show fields data when required - Laravel

查看:88
本文介绍了如何在需要时仅提交隐藏/显示字段数据之一-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个表单包含多个字段.一个字段是是/否"下拉列表.选择是时,将显示另一个下拉列表.如果未选择,则会出现一个文本输入字段. 根据是/否下拉列表,应将字段的数据之一提交给数据库.

A form contains multiple fields. One field is "yes/no" dropdown. When yes selected, another dropdown appears. When no selected a text input field appears. Depending on yes/no dropdown one of the fields' data should be submitted to the database.

但是,两个字段数据都发送到数据库.但是数据库需要一个字符串,现在它得到一个数组.可以提交数据. 应该采取什么措施来防止这种情况,以便仅将显示字段中的数据通过控制器,而不是此时隐藏字段中的数据?

However, both fields data is sent to the database. But the database wants a string and now it gets an array. Data can be submitted. What should be adapted to prevent this, so only data from the showed field is put through the controller and not also data from at that moment hidden field?

注意:必须填写1个字段(下拉列表的输入).

Note: 1 field (input of dropdown) should be filled in, required.

    window.onload = function() {
            document.getElementById('ifYes').style.display = 'none';
            document.getElementById('ifNo').style.display = 'none';
    }
    function hideShow() {
        var D = document.getElementById("yesno")
        var Y = document.getElementById('isYes')
        var N = document.getElementById('isNo')
            if (D.selected) {
            if (D.value == "yes"){
                    Y.style.display = 'block';
                N.style.display = 'none';
                Y.required = true;
            }
            else{
                    N.style.display = 'block';
                Y.style.display = 'none';
                N.required = true;
                } 
        }
    }

的HTML

<div class="row">
    <div class="col-6"> 
          <select name="agree" class="form-control" onchange="hideShow();" id="yesno" required>
<option></option>
              <option id="yes">Yes</option>
              <option id="no">No</option>
          </select>
    </div> 
    <div class="col-6">
           <select name="type" class="form-control" id="ifYes">
                    <option>Option 1</option>
                    <option>Option 2</option>
                    <option>Option 3</option>
            </select>
            <input type="text" id="ifNo" class="form-control input-text" name="type">
     </div>
</div> 
<div class="row">
    <button type="submit" class="btn btn-primary btn-block" style="margin: 10px;">New</button>
</div>

推荐答案

在我的控制器中,我已经对要求的机构进行了验证.现在,我将其删除.通过改编javascript代码,错误消失了.

In my controller, I have performed validation on institute saying required. Now, I remove this. And with adapting the javascript code the error is gone.

$this->validate($request, [
            'institute'             => 'min:2', 
            ...
]); 

$project = new Project();
...
$project->type = request('type');
...
$project->save();

Javascript添加了禁用字段

    function hideShow() {

        var x = document.getElementById('YesNo');
        var r = x.options[x.selectedIndex].value;
        var y = document.getElementById("ifyes");
        var n = document.getElementById("ifno");
        if ( r == "Yes"){
            y.style.display = "block";
            n.style.display = "none";
            y.disabled = false;
            n.disabled = true;
        y.required = true;
        n.required = false;
        }
        else if ( r == "No") {
            y.disabled = true;
            n.disabled = false;
            y.style.display = "none";
            n.style.display = "block";
        y.required = false;
        n.required = true;
        } else{
            y.disabled = true;
            n.disabled = true;
            y.style.display = "none";
            n.style.display = "none";
        y.required = false;
        n.required = true;
        }
    }

这篇关于如何在需要时仅提交隐藏/显示字段数据之一-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆