【问题标题】:Multiple forms with same inputs name check which form input has error Laravel具有相同输入名称的多个表单检查哪个表单输入有错误 Laravel
【发布时间】:2016-07-09 12:12:32
【问题描述】:

我的代码中有这样的内容:

@foreach($cars as $key => $car)
{!!Form::open(['action' => 'CarController@Update'])!!}
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="form-group">
        <label class="col-lg-6  control-label">Color</label>
        <div class="col-lg-6">
            {!!Form::text('carColor', $car->color, ['class' => 'form-control input-sm'])!!}
        </div>                        
    </div>                        
</div>   
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="form-group">
        <label class="col-lg-6  control-label">Price</label>
        <div class="col-lg-6">
            {!!Form::text('carPrice', $car->price, ['class' => 'form-control input-sm'])!!}
        </div>                        
    </div>                        
</div>
{!!Form::close()}
@endforeach

此视图的目标是更新汽车的价格和颜色。但是有很多,所以每辆车都有一个表格。如果输入没有通过验证,我怎么知道哪些表单输入是无效的,因为输入命名相同。

【问题讨论】:

  • 你想同时编辑所有汽车的详细信息???
  • 不,用户一次只能编辑一辆车。每辆车有一个表格。但是如果某种形式的输入无效,我想知道它来自哪个形式
  • 为什么您可以只列出所有汽车及其详细信息,然后添加一个编辑按钮,您可以将其重定向到编辑页面或简单地弹出一个模式然后通过 ajax 进行编辑请求?
  • @Gokigooooks 我的问题是我真正问题的一个缩小示例。我有一个设置页面,这个设置是预定义的。所以我不希望用户认为他在创造、编辑新东西。我希望用户有一个配置面板的想法。

标签: laravel laravel-4 laravel-5 laravel-validation


【解决方案1】:

问题是 laravel 有一个旧输入会话,但没有表单。 但是使用 javascript 或 jquery 很容易完成这项任务。

@foreach($cars as $key => $car)
{!!Form::open(['action' => 'CarController@Update'])!!}
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="form-group">
        <label class="col-lg-6  control-label">Color</label>
        <div class="col-lg-6">
            {!!Form::text('carColor', $car->color, ['class' => 'form-control input-sm', 'id' => 'carColor'])!!}

        </div>                        
    </div>                        
</div>   
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
    <label class="col-lg-6  control-label">Price</label>
    <div class="col-lg-6">
        {!!Form::text('carPrice', $car->price, ['class' => 'form-control input-sm', 'id'=> 'carPrice'])!!}
        </div>                        
   </div>                        
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <div class="form-group">
        <input type="hidden" id="carID" value="{{ $car->id }}">

        <button class="btn btn-warning" id="editDetails">edit</button>
    </div>
</div>
{!!Form::close()}
@endforeach

在你的 javascript 中

$('#editDetails').on('click', function () {
    carColor = $(this).parent().parent().parent().find('#carColor').val();
    carPrice = $(this).parent().parent().parent().find('#carPrice').val();
    car_id = $(this).parent().parent().parent().find('#carID').val();
    _token=  $('meta[name="csrf-token"]').attr('content'); //add meta header in top head section see below

    //here you can also check validation like check for empty fields 

    $.ajax({
        url : 'url/to/edit/route',
        method : 'patch', //this could be post. whatever you defined as your route
        data : {
            carColor : carColor,
            carPrice : carPrice,
            carId : carId,
            _token : _token
        },
        success : function() {
            alert('successful edit!');
            //you can do something like
            $(this).parent().parent().append('<p>success</p>');
            //to show which line was successful
        },
        error : function(xhr,status,err) {
            //you can do something like append an alert div to the parent form element of the erroneous input row.
            $(this).parent().parent().append('<p>error editing here</p>');

        }
    });
});

将此带有 csrf 会话的元标记作为内容添加到主布局的 head 标记中

<meta name="csrf-token" content="{!! csrf_token() !!}">

希望这能让您了解如何解决问题。


编辑 由于 OP 需要在没有 javascript 的情况下处理此问题

首先在foreach循环形式中添加一个key的隐藏输入来传递哪一行抛出错误

<input type="hidden" id="key" value="{{ $key }}">

然后在您的控制器中,您可以将其添加到错误处理块中

Session::flash('error', Input::get('key')); //use flash which behaves the same as as old input session
//a one time use session

然后在循环内部添加一个 if 语句检查错误

@if(Session::has('error') || Session::get('error') == $key)
    <div class="alert-box success">
        <h2>error here</h2> 
    </div>
@endif

希望对你有帮助

【讨论】:

  • 谢谢!这实际上解决了我添加一些前端代码的问题。但不幸的是,我们正在使用 Laravel 100% 使用服务器端的应用程序
  • 哦,好吧,我会再做一个使用服务器端代码的答案
猜你喜欢
  • 2014-11-28
  • 2014-12-19
  • 1970-01-01
  • 2021-12-19
  • 2018-02-20
  • 2013-02-03
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多