3 回答

TA貢獻1864條經(jīng)驗 獲得超2個贊
嘗試以下代碼
public function store(Request $request)
{
//Validation the Data
$validatedData = $request->validate([
'designation_type' => ['required','max:255'],
'status' => ['required'],
],
[
'designation_type.required' => 'Designation is required',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
]);
if($validatedData->fails()) {
return Redirect::back()->withErrors($validatedData);
}
//Data Insert into database
$data =[
'designation_type'=>$request->input('designation'),
'status'=>$request->input('status')
];
DB::table('designation')->insert($data);
return redirect('/designation');
}
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation" class="form-control" id="inputDesignation">
@if($errors->has('designation'))
<div class="error">{{ $errors->first('designation') }}</div>
@endif
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected>Select Status</option>
<option >Active</option>
<option >Inactive</option>
</select>
@if($errors->has('status'))
<div class="error">{{ $errors->first('status') }}</div>
@endif
</div>
</div>
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>

TA貢獻1887條經(jīng)驗 獲得超5個贊
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required'
]);
$category = new Category();
$category->name = $request->name;
$category->slug = str_slug($request->name);
$category->save();
Toastr::success('Category Successfully Saved','Success');
return redirect()->route('admin.category.index');
}
// blade
<form method="POST" action="{{ route('admin.category.store') }}">
@csrf
<div class="form-group form-float">
<div class="form-line">
<input value="{{ old('name') }}" name="name" type="text" id="category_name" class="form-control">
<label class="form-label">{{ __('Name') }}</label>
</div>
</div>
<br>
<a href="{{ route('admin.category.index') }}" class="btn btn-danger m-t-15 waves-effect">{{ __('BACK') }}</a>
<button type="submit" class="btn btn-primary m-t-15 waves-effect">{{ __('SUBMIT') }}</button>
</form>

TA貢獻1806條經(jīng)驗 獲得超8個贊
參考此https://laravel.com/docs/5.6/validation#named-error-bags后,我對代碼進行了一些更改,這有助于解決錯誤。
在designation.blade.php添加
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<h6>{{ $error }}</h6>
@endforeach
</ul>
</div>
@endif
在DesignationController.php
用于狀態(tài)下拉驗證。
'status' => 'required|not_in:0',
用于數(shù)據(jù)插入數(shù)據(jù)庫部分
DB::table('designation')->insert($validatedData);
完整代碼
designation.blade.php
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<h6>{{ $error }}</h6>
@endforeach
</ul>
</div>
@endif
</div>
<div class="col-md-2"></div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<form action="{{url('./designation/store')}}" method="POST">
<div class="form-row">
<div class="form-group col-md-7">
<label for="inputDesignation">Designation</label>
<input type="text" name="designation_type" class="form-control" id="inputDesignation">
</div>
<div class="form-group col-md-5">
<label for="inputStatus_Designation">Status</label>
<select name="status" id="inputStatus_Designation" class="form-control">
<option selected value="">Select Status</option>
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
</div>
</div>
{{-- <button type="submit" class="btn btn-primary">Sign in</button> --}}
<button type="submit" class="btn btn-success" id="btn_add_designation">Add</button>
{{ csrf_field() }}
</form>
</div>
<div class="col-md-4"></div>
</div>
指定控制器.php
public function store(Request $request)
{
$validatedData = $request->validate([
'designation_type' => 'required|max:255',
'status' => 'required|not_in:0',
],
[
'designation_type.required' => 'Designation is required !!',
'designation_type.max' => 'Designation should not be greater than 255 characters.',
'status.required' => 'Status is required !!'
]);
DB::table('designation')->insert($validatedData);
return redirect('/designation');
}
謝謝你們花費寶貴的時間來幫助我....!!!
- 3 回答
- 0 關(guān)注
- 169 瀏覽
添加回答
舉報