繁体   English   中英

使用Laravel 5.1的表单数组

[英]Form arrays with Laravel 5.1

在Laravel 5.1中使用Form:: Facade创建多维表单数组的最优雅方法是什么?

我知道我可以使用以下语法创建表单字段:

{!! Form::label('work_type', 'Work Type') !!}
{!! Form::select('work_type', WorkTypes::show(), ['id' => 'work_type']) !!}

{!! Form::label('date', 'Date & Time Worked') !!}
{!! Form::text('date', date('d/m/Y'), ['id' => 'date', 'class' => 'datepicker']) !!}

{!! Form::label('overtime_start_hour', 'Overtime Start Hour') !!}
{!! Form::select('overtime_start_hour', Hours::show(), ['id' => 'overtime_start_hour']) !!}

{!! Form::label('overtime_start_minute', 'Overtime Start Minute') !!}
{!! Form::select('overtime_start_minute', Minutes::show(), ['id' => 'overtime_start_minute']) !!}

但是,我正在寻找创建多维表单数组。 我希望数组在提交表单时看起来像这样:

array(
    [0] => 
        [work_type]             => 'My work type value for row 1...',
        [date]                  => '20/03/2015',
        [overtime_start_hour]   => array(
                                    [0] => 01
                                    [1] => 03
                                )
        [overtime_start_minute] => array(
                                    [0] => 00
                                    [1] => 00
                                )
    [1] => 
        [work_type]             => 'My work type value for row 2...',
        [date]                  => '21/03/2015',
        [overtime_start_hour]   => array(
                                    [0] => 09
                                    [1] => 07
                                )
        [overtime_start_minute] => array(
                                    [0] => 30
                                    [1] => 00
                                )
)

有人可以帮忙吗?

将您的选择更改为倍数,将名称更改为数组,如下所示:

{!! Form::select('overtime_start_hour[]', Hours::show(), null, ['id' => 'overtime_start_hour', 'multiple' => true]) !!}

{!! Form::select('overtime_start_minute[]', Minutes::show(), null, ['id' => 'overtime_start_minute', 'multiple' => true]) !!}

然后,您可以从选择框中选择多个项目,并获得所需的阵列。 这就是您想要的方式吗?

编辑:您可以尝试在添加新行时将行数组添加到所有字段,并增加行ID和表单输入ID的时间

{!! Form::label('work_type0', 'Work Type') !!}
{!! Form::select('row[0][work_type]', WorkTypes::show(), null, ['id' => 'work_type0']) !!}

{!! Form::label('date0', 'Date & Time Worked') !!}
{!! Form::text('row[0][date]', date('d/m/Y'), null, ['id' => 'date0', 'class' => 'datepicker']) !!}

{!! Form::label('overtime_start_hour0', 'Overtime Start Hour') !!}
{!! Form::select('row[0][overtime_start_hour]', Hours::show(), null, ['id' => 'overtime_start_hour0']) !!}

{!! Form::label('overtime_start_minute0', 'Overtime Start Minute') !!}
{!! Form::select('row[0][overtime_start_minute]', Minutes::show(), null, ['id' => 'overtime_start_minute0']) !!}

下一行(显然)...

{!! Form::label('work_type1', 'Work Type') !!}
{!! Form::select('row[1][work_type]', WorkTypes::show(), null, ['id' => 'work_type1']) !!}

暂无
暂无

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

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