return redirect('student/index')->with('success','成功添加');
- return back()->with('success','change password success');
@if (Session::has('success'))
{Session::get('success')}}
@endif
第二是规则的验证:
public function postData( Request $request) { $validator = Validator::make($request->all(), [ 'username' => 'required|unique:advertisement', 'password' => 'required', 'email' => 'required', 'phone'=> 'required', 'info'=> 'required', ],[ 'username.required'=>'用户名不能为空!', 'password.required'=>'密码不能为空!', 'email.required'=>'邮箱不能为空', 'phone.required'=>'电话不能为空', 'info.required'=>'个人信息不能为空!', ]); if ($validator->fails()) { return redirect('ruleTest/index') ->withErrors($validator) ->withInput(); } } * 可以这样返回模板:
return redirect('register')
->withErrors($validator, 'login');
定义一个: login 字段:之后在模板页直接返回:
{{ $errors->login->first('email') }}
{{ $errors->login->first('phone') }}
在模版页显示同样规则:
在模版页面展示:
@if ($errors->has('phone')){{ $errors->first('phone') }}@endif
@if ($errors->has('email')){{ $errors->first('email') }}@else 必填 @endif
第三种参考:http://blog.csdn.net/slpond/article/details/69055750
http://blog.csdn.net/xiaosevenliang/article/details/44464073
@if ($errors->has('username')){{ $errors->first('username') }}@endif @if(count($errors)>0) @foreach($errors->all() as $value {{$value}} @endforeach @endif 参照laravel:规则验证大全: http://laravelacademy.org/post/3279.html 验证器允许你在验证完成后添加回调 https://d.laravel-china.org/docs/5.5/validation 后续..............
方法一:跳转到指定路由,并携带错误信息
return redirect('/admin/resource/showAddResourceView/' . $customer_id)
->withErrors(['此授权码已过期,请重新生成!']);
- 1
- 2
方法二:跳转到上个页面,并携带错误信息
return back()->withErrors(['此激活码已与该用户绑定过!']);
方法三:validate验证(这种情况应该是最多的)
我们在提交表单的时候,进入控制器的第一步就是验证输入的参数符不符合我们的要求,如果不符合,就直接带着输入、错误信息返回到上一个页面,这个是框架自动完成的。具体例子:
$rules = [
'user_name' => 'unique:customer|min:4|max:255',
'email' => 'email|min:5|max:64|required_without:user_name',
'customer_type' => 'required|integer',
];
$messages = [
'user_name.unique' => '用户名已存在!',
'user_name.max' => '用户名长度应小于255个字符!',
'email.required_without' => '邮箱或者用户名必须至少填写一个!',
'customer_type.required' => '用户类型必填!',
];
$this->validate($request, $rules, $messages);
然后在视图里面引入公共的错误信息:
resources\views\admin\error.blade.php
<div class="form-group">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul style="color:red;">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
如果还需要自定义错误信息,并且把之前传过来的值返回到上一个视图,还需要加个withInput;
if (empty($res)) {
return back()->withErrors(['查不到这个用户,请检查!'])->withInput();
}
如果不使用session,可以使用return view()->with('msg','验证码错误'),
来进行重定向并向模板传递变量,通过取$msg来获取变量。