我的代码中有这样的内容:
@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
此观点的目的是更新汽车的价格和颜色。但是有很多,所以每辆车有一种形式。如果输入未通过验证,我怎么知道哪些表单输入无效,因为输入的名称相同。
答案 0 :(得分: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循环形式中添加密钥的隐藏输入,以传递哪一行抛出错误
<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
希望这有帮助