从控制器重定向后在 Laravel 中显示错误消息

2022-01-08 00:00:00 php laravel laravel-4 blade

如何在 Laravel 中重定向的视图中显示验证消息?

这是我在控制器中的功能

公共函数 registeruser(){$firstname = 输入::get('firstname');$lastname = 输入::get('姓氏');$data = 输入::except(array('_token')) ;$规则=数组('名字' =>'必需的','姓氏' =>'必需的',) ;$validator = Validator::make($data,$rule);if ($validator->fails()){$messages = $validator->messages();return Redirect::to('/')->with('message', 'Register Failed');}别的{DB::insert('插入用户(名字,姓氏)值(?,?)',数组($名,$姓));return Redirect::to('/')->with('message', '注册成功');}}

我知道下面给出的代码是一个不好的尝试,但我该如何解决它,什么是我错过了

@if($errors->has())@foreach ($errors->all() as $error)<div>{{ $error }}</div>@endforeach@万一

更新:以及如何在特定字段附近显示错误消息

解决方案

Laravel 4

当验证失败时返回验证错误.

if($validator->fails()) {return Redirect::back()->withErrors($validator);}

您可以使用

在您的视图中捕获错误

@if($errors->any()){{ implode('', $errors->all('<div>:message</div>')) }}@万一

更新

要在每个字段下显示错误,您可以这样做.

has('firstname'))<div class="error">{{ $errors->first('firstname') }}</div>@万一

使用 css 获得更好的显示样式.

您可以在这里参考文档.p>

更新 2

一次显示所有错误

@if($errors->any()){!!内爆('', $errors->all('<div>:message</div>')) !!}@万一

在每个字段下显示错误.

@error('firstname')<div class="error">{{ $message }}</div>@enderror

How can I display the validation message in the view that is being redirected in Laravel ?

Here is my function in a Controller

public function registeruser()
{
    $firstname = Input::get('firstname');
    $lastname = Input::get('lastname');
    $data  =  Input::except(array('_token')) ;
    $rule  =  array(
                'firstname'       => 'required',
                'lastname'         => 'required',
                   ) ;
    $validator = Validator::make($data,$rule);
    if ($validator->fails())
    {
    $messages = $validator->messages();
    return Redirect::to('/')->with('message', 'Register Failed');
    }
    else
    {
    DB::insert('insert into user (firstname, lastname) values (?, ?)',
                                array($firstname, $lastname));
    return Redirect::to('/')->with('message', 'Register Success');
    }
    }

I know the below given code is a bad try, But how can I fix it and what am I missing

@if($errors->has())
    @foreach ($errors->all() as $error)
        <div>{{ $error }}</div>
    @endforeach
@endif

Update : And how do I display the error messages near to the particular fields

解决方案

Laravel 4

When the validation fails return back with the validation errors.

if($validator->fails()) {
    return Redirect::back()->withErrors($validator);
}

You can catch the error on your view using

@if($errors->any())
    {{ implode('', $errors->all('<div>:message</div>')) }}
@endif

UPDATE

To display error under each field you can do like this.

<input type="text" name="firstname">
@if($errors->has('firstname'))
    <div class="error">{{ $errors->first('firstname') }}</div>
@endif

For better display style with css.

You can refer to the docs here.

UPDATE 2

To display all errors at once

@if($errors->any())
    {!! implode('', $errors->all('<div>:message</div>')) !!}
@endif

To display error under each field.

@error('firstname')
    <div class="error">{{ $message }}</div>
@enderror

相关文章