【问题标题】:How can I edit form input with Laravel?如何使用 Laravel 编辑表单输入?
【发布时间】:2019-09-02 14:33:18
【问题描述】:

我正在尝试在 laravel 中编辑表单输入。当我单击编辑按钮时,它只显示表单而不加载已经选择的值。我能做些什么来解决这个问题?谢谢

来自视图编辑页面的代码:

<form action="/arquiteturas/update" method="post" role="form" class="form-horizontal">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group  {{$errors->has('combo_produto')? ' has-error' : '' }}">
    <label class="col-md-2 control-label">Produto</label>
    <div class="col-md-8">
        <select class="form-control search-select" name="combo_produto" id="combo_produto">
            <option value="0">Selecione</option>
            @foreach($produtos as $value)
            <option @if(old('combo_produto')==$value->id){{'selected'}}@endif value='{{$value->id}}'>{{$value->nome}}</option>
            @endforeach 
        </select>
        <span class="help-block">
        @if($errors->has('combo_produto'))
        @foreach ($errors->get('combo_produto') as $error)
        <b>{{$error}}</b>
        @endforeach
        @endif
        </span>
    </div>
</div>
<!--Projet. Work on it later to fix list-->
<div class="form-group  {{$errors->has('combo_projeto')? ' has-error' : '' }}">
    <label class="col-sm-2 control-label">Projeto</label>
    <div class="col-sm-8">
        <select class="form-control search-select" name="combo_projeto" id="combo_projeto">
            <option value="0">Selecione</option>
            @if(isset($projetos))
            @foreach($projetos as $p)
            <option @if(old('combo_projeto')==$p->id){{'selected'}}@endif value='{{$p->id}}'>{{$p->nome}}</option>
            @endforeach 
            @endif
        </select>
        <span class="help-block">
        @if($errors->has('combo_projeto'))
        @foreach ($errors->get('combo_projeto') as $error)
        <b>{{$error}}</b>
        @endforeach
        @endif
        </span>
    </div>
</div>
<!--Projeto Fim-->
<!--Link-->
<div class="form-group {{$errors->has('txt-link[]')? ' has-error' : '' }}">
    <label class="col-sm-2 control-label">Link da Arquitetura no Git</label>
    <div class="col-sm-8">
        <!--Table -->
        <table border="0"  id="dynamic_field">
            <tr>
                <td><input type="text" placeholder="Digite o link da arquitetura" class="form-control"
                    id="txt-link[]" name="txt-link[]" value="{{old('txt-link[]')}}" style= width:730px;>
                    @if($errors->has('txt-link.0'))
                    @foreach ($errors->get('txt-link.0') as $message)
                    <span class="help-block" style="margin-top:5px; margin-bottom:-5px; color:rgb(170, 56, 56)"><b>{{  $message }}</b></span>
                    @endforeach  
                    @endif  
                </td>
                <td><button type="button" name="add" id="add" 
                    <a class="btn btn-primary"><i class="fa fa-plus"></i>
                    </a></button>
                </td>
            </tr>
        </table>
        <!--Table Fim -->
    </div>
</div>
</div>
<div class="form-group">
    <div class="col-sm-2 col-sm-offset-6"> 
        <a class="form-control btn btn-primary" href="/arquiteturas">
        <i class="fa fa-arrow-circle-left"></i> &nbsp; Cancelar
        </a>
    </div>
    <div class="col-sm-2"> 
        <button type="submit" class="form-control btn btn-primary">Salvar</button> 
    </div>
</div>

来自我的控制器的代码

    function editar($id) {
        $arquitetura = Arquitetura::where('id', $id)->first();
        return view('arquiteturas.editar',  ['produtos'=>Produto::orderBy('nome')->get(), 'arquitetura'=>$arquitetura]);
      }

    //update
    function update(Request $request) {

        $this->validate($request, [

        'combo_produto'=>['not_in:0'],
        'combo_projeto'=>['not_in:0'],
        'txt-link'=>['required'],
        ]);

        $arquitetura = Arquitetura::find($request['id']);
        $arquitetura->produto_id = $request['combo_produto'];
        $arquitetura->projeto_id = $request['combo_projeto'];
        $arquitetura->link = $request['txt-link'];

        $arquitetura->save();
            return redirect('/arquiteturas')->with('msg_success', 'Dados salvos com sucesso!');
          } 

当我点击编辑按钮时,我需要加载用户选择的旧值。

【问题讨论】:

  • 输入和选择?或者只是输入
  • 其实输入和选择都是。我忽略了选择:)

标签: laravel forms edit


【解决方案1】:

您可以将a default value 作为第二个参数传递给old 函数:

<select class="form-control search-select" name="combo_produto" id="combo_produto">
    <option value="0">Selecione</option>
    @foreach($produtos as $value)
    <option @if(old('combo_produto', $arquitetura->combo_produto)==$value->id){{'selected'}}@endif value='{{$value->id}}'>{{$value->nome}}</option>
    @endforeach 
</select>

【讨论】:

  • 感谢您的跟进。但是,它仍然无法正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
相关资源
最近更新 更多