Back

Why does my custom register page gives exception?


I have some problem creating the auth part of my site: I tried to adapt my page to the register.blade.php given by laravel but I still can't show errors when there problem about the credentials and still can't redirect to user page

here is my form(it is a modal, It is not a problem given by the tag anyway):

<form id="register-form" style="display:none;" method="POST" action="{{ route('register') }}" >
          {{ csrf_field() }}
          <div class="modal-body">
            <div id="div-register-msg">
              <div id="icon-register-msg" class="glyphicon glyphicon-chevron-right"></div>
              <span id="text-register-msg">Register an account.</span>
            </div>
            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
              <input id="name" name="name" class="form-control" type="text"  placeholder="Username" value="{{ old('name') }}" required>
              @if ($errors->has('name'))
              <span class="help-block">
                <strong>{{ $errors->first('name') }}</strong>
              </span>
              @endif
            </div>
            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
              <input id="email" name="email" class="form-control" type="text" placeholder="E-Mail" value="{{ old('email') }}" required>
              @if ($errors->has('email'))
              <span class="help-block">
                <strong>{{ $errors->first('email') }}</strong>
              </span>
              @endif
            </div>
            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
              <input id="password" name="password" class="form-control" type="password" placeholder="Password" required>
              @if ($errors->has('password'))
              <span class="help-block">
                <strong>{{ $errors->first('password') }}</strong>
              </span>
              @endif
            </div>
            <input id="password_confirmation" name="password_confirmation" class="form-control" type="password" placeholder="Confirm Password" required>
          </div>
          <div class="modal-footer">
            <div>
              <button type="submit" class="btn btn-primary btn-lg btn-block">Register</button>
            </div>
            <div>
              <button id="register_login_btn" type="button" class="btn btn-link">Log In</button>
              <button id="register_lost_btn" type="button" class="btn btn-link">Lost Password?</button>
            </div>
          </div>
        </form>

And this is my RegisterController.php:

<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\View\Middleware\ShareErrorsFromSession;
class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */
    use RegistersUsers;
    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function postRegister(Request $request)
    {
       $validator = $this->validator($request->all());
       if ($validator->fails()) {
           $this->throwValidationException(
               $request, $validator
           );
       }
       return redirect($this->redirectPath());
   }


    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed'
        ]);
    }
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

The error I get is the follwing "The given data was invalid" from the validate() method that i'm using to validate input credential


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us