SVIP's avatar
Level 1

Laravel Registration Problem

I am trying to register a user in laravel as per video 19 of the introductory course.

<?php

namespace App\Http\Controllers;

use App\User;


class RegistrationController extends Controller
{
     public function create()
     {

        return view ('sessions.create');

     }

     public function store()
     {
        $this->validate(request(), [

            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required'

            ]);


        $user = User::create(request(['name','email','password']));


        auth()->login($user);


        return redirect('/');

     }


}

Hitting the register button in my form causes the data in the form to disappear and the same page view remains. There is no redirect to the homepage. It doesn't seem as though the store() function is being called? I have tried to follow exactly as per the video.

0 likes
8 replies
SVIP's avatar
Level 1

This is the web routes files:

<?php


Route::get('/','PostsController@index');
Route::get('/posts/create','PostsController@create');
Route::post('/posts','PostsController@store');
Route::get('/posts/{post}','PostsController@show');

Route::post('/posts/{post}/comments','CommentsController@store');

Route::get('/register','RegistrationController@create');

Route::post('/register','RegistrationController@store');

Route::get('/login','SessionsController@create');

Snapey's avatar

Where does the form post to? Check the URL being called in network tools or view the source for the Form tag

Do you display form errors?

Jaytee's avatar

It sounds like a validation error. If validation fails, it will redirect back to the same view and clear all data in the form.

You can display the errors in the view using the foreach provided above by @ankitparmar372.

@foreach($errors->all() as $error)
   {{ $error  }}
@endforeach

You can also use the old('inputName') in the form to keep the old data there if validation fails.

<input type="text" name="email" value="{{ old('email') }}">

The errors will give you a hint as to what's wrong.

P.S: Make sure you're hashing the user's password before you save it. Currently, you're just passing it as a plain string, which is unsecure. Unless you have an accessor setup on the model to hash it before it's created.

SVIP's avatar
Level 1

Hi Guys. Thanks for all the responses.

@Snapey : The form posts to /register. ...Check the URL being called in network tools or view the source for the Form tag: I'm not sure how to do this. Very new to Laravel.

...Do you display form errors? : After getting the other two responses -- yes.

@ankitparmar372 and @Jaytee :

I have used the code mentioned to display errors and I have noticed that when I enter all the input the form always (incorrectly) displays the error that: 'The email field is required.'

If I enter the name only and omit the other two fields when entering the form the (incorrect) error message is:

'The name field is required. The email field is required. The password field is required.'

If I enter the password only the (correct) error message is: 'The name field is required. The email field is required.'

If I enter the email field only the (incorrect) error message is: 'The email field is required. The password field is required.'

Logically, these do not make sense but this is the output that I get.

I had initially hashed the password field but thought to put everything back to 'basics' just until the form was working properly.

Many thanks..

Snapey's avatar
Snapey
Best Answer
Level 122

on your form, is the field name literally 'email'. It won't work if it is 'Email' for instance

view the source for the Form tag

right click in the browser and choose view source You can review the html

1 like
Jaytee's avatar

Yeah make sure your email input is something like:

<input type="text(or email for extra client validation)" name="email"> // the name part is important.

Also make sure you're entering a valid email, it needs to include the @ symbol

1 like
SVIP's avatar
Level 1

@Snapey : Many thanks for this. This was my error. 'email' was incorrectly labelled.

@Jaytee : Many thanks for your input also.

Much appreciated.

Please or to participate in this conversation.