选择带有2个空选项值的输入

时间:2018-12-06 13:32:38

标签: laravel-5

在我的laravel 5.7 / mysql 5应用程序中,我的投票表和模型中定义了boolean is_quiz字段:

protected $casts = [
    'is_quiz' => 'boolean',
];
...

并包含可能使用此字段的值/键的数组

private static $voteIsQuizLabelValueArray = Array(1 => 'Is Quiz', 0 => 'Is Not Quiz');

在控件中,我为空选择器添加了空值:

$viewParamsArray['voteIsQuizValueArray']         = $this->SetArrayHeader(['' => ' -Select Is Quiz- '], Vote::getVoteIsQuizValueArray(false));

此数组具有值:

$viewParamsArray['voteIsQuizValueArray']::Array
(
    [] =>  -Select Is Quiz- 
    [1] => Is Quiz
    [0] => Is Not Quiz
)

在我的表单中,此数组为:

{{ Form::select('is_quiz', $voteIsQuizValueArray, isset($vote->is_quiz) ? $vote->is_quiz : '', [ "id"=>"is_quiz", "class"=>"form-control editable_field select_input " ] ) }}

在呈现的html源代码中,我看到选择了2个选项:

<select id="is_quiz" class="form-control editable_field select_input valid" name="is_quiz" aria-invalid="false" aria-describedby="is_quiz-error"><option value="" selected="selected"> -Select Is Quiz- </option><option value="1">Is Quiz</option><option value="0" selected="">Is Not Quiz</option></select>

和validator.w3.org在此处引发了错误。 我看到语法错误的原因是什么,但我不知道有没有简单的方法来解决它?

谢谢!

1 个答案:

答案 0 :(得分:1)

自Laravel 5起,不推荐使用表单构建器

Why are Form and HTML helpers deprecated in Laravel 5.x?

作为一种选择,您可以尝试使用Laravel Collective

https://packagist.org/packages/laravelcollective/html

但是IMO最好的决定是通过刀片建立选择:

<select id="is_quiz" 
    class="form-control editable_field select_input valid" 
    name="is_quiz" 
    aria-invalid="false"
    aria-describedby="is_quiz-error"
>
    @foreach ($voteIsQuizValueArray as $k => $v)
            <option 
                    value="{{ $k }}" 
                    @if( $k === old('is_quiz', '') ) selected="selected" @endif
            >{{ $v }}</option>
    @endforeach
</select>