在laravel

时间:2015-11-05 04:13:41

标签: validation laravel button radio

我正在使用Laravel 5.1和宅基地。

我的问题是,当验证失败时,单选按钮不会重新填充原始用户输入的数据。

我的验证规则来自PostRequest.php

public function rules()
    {
        //validating input text field --> this one works
        $rules = ['owner' => 'required|max:15',];

        //validating array of input text fields --> this too works
        foreach($this->request->get('item') as $a => $z)
        {
            $rules['item.'.$a] = 'required';
        }

        //validating array of radio buttons --> does not return with data
        foreach($this->request->get('radio') as $b => $y)
        {
            $rules['radio.'.$b] = 'required';
        }


        return $rules;
    }

我视图的一部分(blade.php)

<div class="row">
    <div class="tcell col-xs-6">
        <label for="item[0]" class="sr-only"></label>
        <input class="form-control" id="item[0]" name="item[0]" type="text" placeholder="enter item" value="{ { old('item.0') } }"/>
    </div>
    <div class="tcell col-xs-6">
        <div class="radio">
            <input type="radio" id="s15" name="radio[0]" value="5" /><label for="s15" title="5">5</label>
            <input type="radio" id="s14" name="radio[0]" value="4" /><label for="s14" title="4">4</label>
            <input type="radio" id="s13" name="radio[0]" value="3" /><label for="s13" title="3">3</label>
            <input type="radio" id="s12" name="radio[0]" value="2" /><label for="s12" title="2">2</label>
            <input type="radio" id="s11" name="radio[0]" value="1" /><label for="s11" title="1">1</label>
        </div>
    </div>
</div>

 ...

    <div class="tcell col-xs-6">
        <div class="radio">
            <input type="radio" id="s20" name="radio[1]" value="5" /><label for="s20" title="5">5</label>
            <input type="radio" id="s19" name="radio[1]" value="4" /><label for="s19" title="4">4</label>
            <input type="radio" id="s18" name="radio[1]" value="3" /><label for="s18" title="3">3</label>
            <input type="radio" id="s17" name="radio[1]" value="2" /><label for="s17" title="2">2</label>
            <input type="radio" id="s16" name="radio[1]" value="1" /><label for="s16" title="1">1</label>
    </div>
</div>

old()函数既适用于输入(文本),也适用于输入(文本)数组。但我不知道如何用单选按钮应用它。

由于

2 个答案:

答案 0 :(得分:0)

尝试修改如下

刀片模板:

<div class="radio">
        <input type="radio" id="s15" name="radio" value="5" /><label for="s15" title="5">5</label>
        <input type="radio" id="s14" name="radio" value="4" /><label for="s14" title="4">4</label>
        <input type="radio" id="s13" name="radio" value="3" /><label for="s13" title="3">3</label>
        <input type="radio" id="s12" name="radio" value="2" /><label for="s12" title="2">2</label>
        <input type="radio" id="s11" name="radio" value="1" /><label for="s11" title="1">1</label>
    </div>

表单请求

public function rules()
{
    //validating input text field --> this one works
    $rules = ['owner' => 'required|max:15', 'radio' => 'required'];


    //validating array of input text fields --> this too works
    foreach($this->request->get('item') as $a => $z)
    {
        $rules['item.'.$a] = 'required';
    }



    return $rules;
}

答案 1 :(得分:0)

这是解决方案

<input type="radio" id="s15" name="radio[1]" value="5"  { { old('radio.1')=="5" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio[1]" value="5"  { { old('radio.1')=="4" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s14" title="4">4</label>
...

这只会帮助重定向输入数据,但是,如果没有选择任何内容,验证仍然会失败,因为没有提交任何内容。修复方法是使用for循环而不是foreach循环。您必须获取单选按钮的计数才能进行迭代。