在视图Laravel中显示验证错误消息

时间:2020-01-23 20:13:10

标签: laravel

我尝试从 store function update function 中显示验证错误消息,
我将“ 添加输入”和“ 编辑输入”放在同一视图中 所以我不知道哪个函数有验证错误

这是我的功能存储代码...

$rules = [
         'section_name' => 'required|min:3',
         'section_pic' => 'image',
         ];

$customMessages = [
                  'section_name.required' => 'Please Enter Section Name ',
                  'section_name.min' => 'Section Name Must Be At Least 3 Character ',
                  'section_pic.image'  => 'You Should Choose Images Only',
                  ];

 $this->validate($request, $rules, $customMessages);

  here some add code .....

 return redirect('/Sections')->with('success_store','Success ! Your Section Added Successfully');

这是我的功能更新代码...

$rules = [
         'section_name' => 'required|min:3',
         'section_pic' => 'image',
         ];

$customMessages = [
                  'section_name.required' => 'Enter Section Name You Want To Update to',
                  'section_name.min' => 'To Update Section Name Must Be At Least 3 Character ',
                  'section_pic.image'  => 'To Update You Should Choose Images Only',
                  ];

$this->validate($request, $rules, $customMessages);

some update code .....

return redirect('/Sections')->with('success_update','Success ! Your Section Updated Successfully');

,这在我的视图 ...

@if($errors->any())
    <script type="text/javascript">
        function openModal() {
            $("#section_add").modal("show");
        }
        window.onload = openModal;
    </script>
@endif

@if($errors->any())
    <script type="text/javascript">
        function openModal() {
            $("#section_edit").modal("show");
        }
        window.onload = openModal;
    </script>
@endif

这是我的添加输入,用于添加模式...

<input type="text" name="section_name" class="form-control name"
value="{{ old('section_name') }}" autofocus required>

@error('section_name')
<small class="text-danger">{{ $message }}</small>
@enderror

这是我在编辑模式中的编辑输入 ...

<input type="text" name="section_name" class="form-control name"
value="{{ old('section_name') }}" autofocus required>

@error('section_name')
<small class="text-danger">{{ $message }}</small>
@enderror

1 个答案:

答案 0 :(得分:0)

我相信最简单的解决方案是使用named error bags。您可以通过Validator::make() manually create the validation

因此,理想情况下,您应该像这样修改控制器逻辑,(我将只编写其中一个方法作为示例,然后可以在另一个方法中复制相同的逻辑)

public function store(Request $request)
{

  $rules = [
    'section_name' => 'required|min:3',
    'section_pic' => 'image',
   ];

  $customMessages = [
    'section_name.required' => 'Please Enter Section Name ',
    'section_name.min' => 'Section Name Must Be At Least 3 Character ',
    'section_pic.image'  => 'You Should Choose Images Only',
  ];

 $validator = Validator::make($request->all(), $rules, $customMessages);

 if ($validator->fails()) {
        return redirect('Sections/create')
                    ->withErrors($validator,'store') // or whatever key you give
                    ->withInput();
    }
  return redirect('/Sections')->with('success_store','Success ! Your Section Added Successfully');
}

您现在可以在视图中使用指定的密钥(例如,在这种情况下, {{ $errors->store->all() }}。希望有帮助