在注册页面添加验证消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在注册页面添加验证消息相关的知识,希望对你有一定的参考价值。

我正在使用make:auth对注册进行验证,laravel在我的输入未经过身份验证时也给出了验证消息,但那是因为它使用laravel的默认设计,因为我使用自己的注册页面设计,我想要还使用一个laravel给出的添加验证消息,但每当我输入错误然后注册,他们没有给出验证消息,有没有人知道如何连接我的注册设计与laravel注册auth?

答案

这是如何在刀片中实现它的示例:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

更多:https://laravel.com/docs/5.2/validation#working-with-error-messages

另一答案

既然你没有提到你如何覆盖默认注册,我将假设你从register控制器覆盖AppHttpControllersAuthRegisterController方法。如果这是你的情况,那么你只需要在register方法中添加一行

public function register(Request $request)
{
    // Add this line at the beginning of your register method
    $this->validator($request->all())->validate();
    // Add your registration code below
}

您还应该在模板文件中显示错误。以下是显示email字段错误的方法:

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
    <div class="row">
        <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
            <label for="email" class="control-label">@lang('auth/register.email_address')</label>
        </div>
        <div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
            <div class="input-div">
                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

                @if ($errors->has('email'))
                    <span class="help-block">
                        <strong>{{ $errors->first('email') }}</strong>
                    </span>
                @endif
            </div>
        </div>
    </div>
</div>
另一答案

这样做

# add rule 
$rules = [
    'username' => 'required|string|email',
    'password' => 'required',
];

# add custome messages, if needed 
$msg = [
    'username.required' => 'Username is required',
    'password.required' => 'Password is required',
];

# validating those
$validator = Validator::make(Input::all(), $rules, $msg);

if ($validator->fails()) {
    # if validation fails
    return Redirect::to('URL')->withErrors($validator)->withInput();
}
else {
    $username = Input::get('username');
    $password = Input::get('password');

    if (!Auth::attempt([ 'email' => $username, 'password' => $password])) {
        # if login fail
        return Redirect::to('URL');
    }
    else {
    # if success
        return Redirect::to('dashboard');
    }
}

可能有用

use IlluminateSupportFacadesInput;
use IlluminateSupportFacadesValidator;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesRedirect;
另一答案

要验证表单,您可以使用以下代码

在你的控制器中

public function store(Request $request)
    {
        //
            $this->validate($request,[
                    'enq_source'=>'required|not_in:-1',
                    'c_person'=>'required',
                    'p_contact'=>'required',
                    'email1'=>'required',

                ]);

            $enquiry=new Enquiry;
            $enquiry->enq_source=$request->enq_source;
            $enquiry->enq_date=date("Y-m-d", strtotime($request->enq_date));
            $enquiry->company_name=$request->company_name;
            $enquiry->c_person=$request->c_person;
            $enquiry->c_designation=$request->c_designation;
            $enquiry->state=$request->st;
            $enquiry->district=$request->District;
            $enquiry->address=$request->address;
            $enquiry->p_contact=$request->p_contact;
            $enquiry->c_contact=$request->c_contact;
            $enquiry->email=$request->email1;
            $enquiry->c_profile=$request->c_profile;
            $enquiry->purpose=$request->purpose;
            $enquiry->sales_person=$request->sales_person;
            if(sizeof($request->product_name)>0){
                $enquiry->product_name=implode(',', $request->product_name);

            }



            $enquiry->quantity=$request->quantity;
            $enquiry->status=$request->status;
            $enquiry->comment=$request->comment1;
            $enquiry->save();

            $request->session()->flash('success', 'Enquiry successfully Added');
            return redirect(route('admin.dashboard'));

    }

并制作如下视图

<input id="company_name" name="company_name" type="text" class="form-control" placeholder="Company Name" value="{{old('company_name')}}">
                      @if ($errors->has('company_name'))
                                <div class="error">{{ $errors->first('company_name') }}</div>
                            @endif

以上是关于在注册页面添加验证消息的主要内容,如果未能解决你的问题,请参考以下文章

注册后返回主屏幕

为啥这段代码没有将数据插入数据库?

在模式引导程序中验证注册表单

教程4 - 验证和权限

我需要/何时需要验证码?

对 Firebase 身份验证做出反应