如何在Laravel中表单提交失败时保留输入字段中的值



我在Laravel-5.8中有这段代码。有两种型号:

class HrLeaveType extends Model
{ 
protected $table = 'hr_leave_types';
protected $primaryKey = 'id';
protected $fillable = [
'leave_type_name',
'leave_type_code',
'description',
];   
public function leavetypedetail()
{
return $this->hasMany('AppModelsHrHrLeaveTypeDetail');
}  
}
class HrLeaveTypeDetail extends Model
{
protected $table = 'hr_leave_type_details';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'leave_type_id',
'company_id',
'employee_type_id',
'no_of_days',
];
protected $casts = [
'data' => 'array',
];
public function leavetype()
{
return $this->belongsTo('AppModelsHrHrLeaveType', 'leave_type_id', 'id');
}   
public function employeetype()
{
return $this->belongsTo('AppModelsHrHrEmployeeType', 'employee_type_id', 'id' );
}        
}

请求规则

public function rules()
{
return [
'leave_type_name' => [
'required', 
'string',
'min:3',
'max:80',                 
Rule::unique('hr_leave_types')->where(function ($query) {
return $query->where('leave_type_name', $this->leave_type_name)
->where('company_id', $this->company_id);
})                
],     
'leave_type_code' => [
'nullable', 
'string',
'max:10',                 
Rule::unique('hr_leave_types')->where(function ($query) {
return $query->where('leave_type_code', $this->leave_type_code)
->where('company_id', $this->company_id);
})                
],    
'no_of_days'           => 'required|array',
'no_of_days.*' => [
'required', 
'numeric',
'max:120'               
],                                         
'employee_type_id'           => 'required|array', 
'employee_type_id.*' => [
'required',                 
],                     
];
}      
}

控制器

public function create()
{
$userCompany = Auth::user()->company_id;
$employeetypes   =       HrEmployeeType::where('company_id', $userCompany)->get(); 
$leavetype = new HrLeaveType();

return view('leave.leave_types.create')
->with('leavetype', $leavetype)
->with('employeetypes', $employeetypes);
}
public function store(StoreLeaveTypeRequest $request)
{
$userCompany = Auth::user()->company_id;
$leavetype = new HrLeaveType();
$leavetype->leave_type_name             = $request->leave_type_name;
$leavetype->leave_type_code             = $request->leave_type_code;
$leavetype->description                 = $request->description;
$leavetype->save();        
foreach ($request->employee_type_id as $key => $employee_type_id){
$insert_array = [
'no_of_days'                        => $request->no_of_days[$key],
'employee_type_id'                  => $request->employee_type_id[$key],                
'leave_type_id'                     => $leavetype->id,
];
HrLeaveTypeDetail::create($insert_array );
}

Session::flash('success', 'Leave Type is created successfully');
return redirect()->route('leave.leave_types.index');        
}
}  

LeaveTypeDetail是动态创建的

创建刀片

@if (Session::has('error'))
<div class="alert alert-warning" align="left">
<a href="#" class="close" data-dismiss="alert">&times;</a>
<strong>!</strong> {{Session::get('error')}}
</div>
@endif  
<br>
@include('partials._messages')   
<form  action="{{route('leave.leave_types.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}


<div class="card-body">
<div class="form-body">
<div class="row">  
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Name:<span style="color:red;">*</span></label>
<input  type="text" name="leave_type_name" value="{{ old('leave_type_name', $leavetype->leave_type_name) }}" placeholder="Enter leave type name" class="form-control @error('leave_type_name') is-invalid @enderror">
@error('leave_type_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror                 
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Code:</label>
<input  type="text" name="leave_type_code" value="{{ old('leave_type_code', $leavetype->leave_type_code) }}" placeholder="Enter leave typecode" class="form-control @error('leave_type_code') is-invalid @enderror">
@error('leave_type_code')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror                 
</div>
</div>                


<div class="col-sm-12">
<div class="form-group">
<label>Description</label>
<textarea rows="2" name="description" class="form-control @error('description') is-invalid @enderror" value="{{old('description',$leavetype->description)}}" placeholder="Enter Description here ...">{{old('description',$leavetype->description)}}</textarea>
@error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror                 
</div>
</div>
<div class="col-sm-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Employee Type<span style="color:red;">*</span></th>
<th scope="col">Leave Days<span style="color:red;">*</span></th>
<th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
</tr>
</thead>
<tbody>
<tr>
<td width="60%">                    
<option value="0" selected="true" disabled="true">Select Employee Type</option>
@if($employeetypes->count() > 0 )
@foreach($employeetypes as $employeetype)
<option name="employee_type_id[]"  value="{{$employeetype->id}}">{{$employeetype->employee_type_name}}</option>
@endforeach
@endif                                         
</select>
</td>
<td width="35%"><input type="text" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120"></td>
<td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>          
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('leave.leave_types.index')}}'" class="btn btn-default">Cancel</button>
</div>           

</form>
<script type="text/javascript">
$(document).ready(function(){
$('.addRow').on('click', function () {
addRow();
});
function addRow() {
var addRow = '<tr>n' +
'     <td width="60%"><select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="employee_type_id[]">n' +
'             <option value="0" selected="true" disabled="true">Select Employee Type</option>n' +
'              @if($employeetypes->count() > 0 )n' +
'                 @foreach($employeetypes as $employeetype)n' +
'                 <option value="{{$employeetype->id}}">{{$employeetype->employee_type_name}}</option>n' +
'                 @endforeachn' +
'             @endifn' +
'         </select></td>n' +
'     <td width="35%"><input type="text" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120"></td>n' +
'     <td width="5%"><a   class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>n' +
'    </tr>';
$('tbody').append(addRow);
addRemoveListener();
};
addRemoveListener();
});
</script>

一种休假类型有许多休假类型的详细信息。对于控制器和视图,我有动态表单输入。leave_type_details是数组。

当用户提交时,如果操作失败,则所有字段都应零售其值。这种情况只发生在leave_type中,但leave_type_details字段没有保留其值。它清除了。

我该如何做到这一点?

感谢

在刀片中:

@if(old('input_name'))
@foreach(old('input_name') as $key => $value)
<your_html>{{old("input_name.{$key}"}}</your_html>
@endforeach
@endif

相关内容

  • 没有找到相关文章

最新更新