0

Sorry, i am a beginner for laravel 5, I try to validate register form but it not showing error message. Any idea why errors are not displaying. Please help.

thank you so much.

Here are my code

View

                        <form role="form" method="post" action="{{ url('/backoffice/register') }}">
                      <div class="form-group{{ $errors->has('fullname') ? ' has-error' : '' }}">
                        <label for="name">Full Name</label>
                        <input  type="text" name="fullname" class="form-control" id="fullname" value="{{ old('fullname') ?: '' }}" placeholder="Enter your fullname">
                        @if ($errors->has('fullname'))
                            <span class="help-block">{{ $errors->first('fullname') }}</span>
                        @endif
                      </div>
                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="control-label">Your email address</label>
                            <input type="text" name="email" class="form-control" id="email" value="{{ old('email') ?: '' }}" placeholder="Enter your email">
                            @if ($errors->has('email'))
                                <span class="help-block">{{ $errors->first('email') }}</span>
                            @endif
                        </div>
                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="control-label">Choose a password</label>
                            <input type="password" name="password" class="form-control" id="password" placeholder="Enter your password">
                            @if ($errors->has('password'))
                                <span class="help-block">{{ $errors->first('password') }}</span>
                            @endif
                        </div>
                        <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
                            <label class="control-label">Confirm Password</label>
                            <input type="password" class="form-control" name="password_confirmation" placeholder="Re-Enter your password">

                            @if ($errors->has('password_confirmation'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password_confirmation') }}</strong>
                                </span>
                            @endif
                        </div>
                        <input type="hidden" name="activated" value="0">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-warning btn-sm">Submit</button>
                        <button type="reset" class="btn btn-default btn-sm">Reset</button>
                    </form>

Controller

public function postRegister(Request $request) {

    $this->validate($request, [
        'email' => 'required|email|max:255',
        'password'  =>'required|alphaNum|Between:4,8|Confirmed',
        'password_confirmation'=>'required|alphaNum|Between:4,8',
        'fullname' => 'required|max:255',
        'activated' => 'required',
    ]);

    Admin::create([
        'email' => $request->input('email'),
        'password' => $request->input('password'),
        'fullname' => $request->input('fullname'),
        'activated' => $request->input('activated')
    ]);

    return redirect('backoffice/register')->with('info', 'register successfully!');

}

Route

Route::get('backoffice/register', 'AdminAuthController@getRegister');
Route::post('/backoffice/register', 'AdminAuthController@postRegister');

2 Answers 2

2

do you display errors in your blade? I mean this

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

if you validate data in controller, you also can do this

    public function postRegister(Request $request)
    {
       $validator = Validator::make($request->all(), [
        'title' => 'required|',
        'body' => 'required',
        // your validation here...
    ]);

    if ($validator->fails()) {
       return redirect('youFormPage')->withErrors($validator)->withInput();
     }
   }

read more at documentation

Sign up to request clarification or add additional context in comments.

Comments

0

Try to remove "\Illuminate\Session\Middleware\StartSession::class" from 'middleware' in Kernel.php, it must be only in the 'web' group in the same file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.