Laravel - 不接收来自嵌套输入的字段

时间:2017-07-04 19:16:18

标签: php laravel validation nested laravelcollective

我正在使用Laravel 5.3和相同的刀片视图来创建/编辑casa(葡萄牙语中的房子)和contato(用葡萄牙语联系)。

我有一个表单可以提交到2个表格中contatos casas)。在我更改前6个字段的name(表contatos)之前,它工作正常。

是:

{!! Form::text('nome_contato') !!}

现在是:

{!! Form::text('contato[nome_contato]') !!}

我必须这样做,因为第一个没有填充输入来编辑它。但现在它告诉我,即使它们不是,这些字段也是空的。

CasaRequest.php↓↓

public function rules()
    {
        return [
            'nome_contato'  => 'required|max:255',
            'telefone'      => 'required|numeric|digits_between:10,11',
            'celular1'      => 'required|numeric|digits_between:10,11',
            'celular2'      => 'numeric|digits_between:10,11',
            'celular3'      => 'numeric|digits_between:10,11',
            'skype'         => 'max:255',
            'nome'          => 'required|max:100',
            'descricao'     => 'required|min:50|max:2000',
            'endereco'      => 'required|max:255',
            'numero'        => 'numeric|min:0',
            'bairro'        => 'required|max:100',
             (etc........)
            ];
}

creteedit.blade.php↓↓

    @if(isset($casa))
        {!! Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put', 'class' => 'register']) !!}      
    @else
        {!! Form::open(['route' => 'casas.store', 'class' => 'register']) !!}
    @endif

    <!-- First step Contatos -->
    <div class="row">   
        <div class="form-group row">
            <div class="col l2 offset-l1">
                Nome para contato
            </div>          
            <div class="col l8">
                {!! Form::text('contato[nome_contato]') !!}             
                <span class="red-text text-lighten-1">{!!$errors->first('nome_contato')!!}</span>
                </div>
            </div>

            <div class="form-group row">
                <div class="col l2 offset-l1">
                    Telefone
                <div class="col l8">
                    {!! Form::text('contato[telefone]') !!}             
                    <span class="red-text text-lighten-1">{!!$errors->first('telefone')!!}</span>
                </div>
            </div>

            <div class="form-group row">
                <div class="col l2 offset-l1">
                    Celular 1           
                <div class="col l8">
                    {!! Form::text('contato[celular1]') !!}             
                    <span class="red-text text-lighten-1">{!!$errors->first('celular1')!!}</span>
                </div>
            </div>

            <div class="form-group row">
                <div class="col l2 offset-l1">
                    Celular 2
                </div>          
                <div class="col l8">
                    {!! Form::text('contato[celular2]') !!}             
                    <span class="red-text text-lighten-1">{!!$errors->first('celular2')!!}</span>
                </div>

        <!-- Here comes more fields from the table `contatos` -->


<!-- Second step Casas -->

<div class="form-group row">
            <div class="col l2 offset-l1">
                    <label class="right">Nome<span class="red-text">*</span></label>
                </div>          
                <div class="col l8">
                    {!! Form::text('nome') !!}              
                    <span class="red-text text-lighten-1">{!!$errors->first('nome')!!}</span>
                </div>
            </div>

            <div class="form-group row">
                <div class="col l2 offset-l1">
                    <label class="right">Descrição<span class="red-text">*</span></label>
                </div>          
                <div class="col l8">
                    {!! Form::textarea('descricao') !!}             
                    <span class="red-text text-lighten-1">{!!$errors->first('descricao')!!}</span>
                </div>
            </div>

            <div class="form-group row">
                <div class="col l2 offset-l1">
                    <label class="right">Endereço <span class="red-text">*</span></label>
                </div>          
                <div class="col l5">
                    {!! Form::text('endereco') !!}              
                    <span class="red-text text-lighten-1">{!!$errors->first('endereco')!!}</span>
                </div>
                <div class="col l1 offset-l1 ">
                    <label class="right">Número</label>
                </div>          
                <div class="col l1">
                    {!! Form::text('numero') !!}                
                    <span class="red-text text-lighten-1">{!!$errors->first('numero')!!}</span>
                </div>
            </div>

第二步的字段正在运行,仅从第一步开始,因为他们namecontato[field_name]

1 个答案:

答案 0 :(得分:4)

这是一个嵌套输入,你必须使用点。

<强>例如

$request->input('contato.nome_contato')

规则相同:

'contato.nome_contato' => 'required|max:255',