Support the ongoing development of Laravel.io →
Validation
Last updated 2 years ago.
0

Here is something I'd do

public function store()
{
    $input = Input::all();

    $validation = Validator::make($input, User::$rules);

    if ($validation->fails())
    {   
        return Redirect::back()->with('message', 'The form failed to validate.')->withErrors($validation)->withInput();
    }

    User::create($input); // Don't forget to set $fillable array in user model

    // Return user to login form with message.
    return Redirect::to('users/login')->with('message', 'The user was created successfully.'); // No need to redirect him back to /create if successful. One less step for him to log in 
}

And the User model

protected $fillable = array('username', 'email_address', 'password', 'role_id')

public static $rules = array(
    'username' => 'required|min:5|unique:users',
    'email_address' => 'required|email|unique:users',
    'password' => 'required|confirmed|min:5', // confirmed rule looks if password_confirmation is the same so no need to validate it
    'role_id' => 'required|integer'
);

Your controller is a bit cleaner and easier to read now, but all in all your code looks good as is, except for the 'password_confirmation' rule you had.

You could also validate if the User::create() actually worked in a similar way as "if ($validation->fails())"

Last updated 2 years ago.
0

This looks good however the purpose of this is not a register for anybody to use. This is for an admin who is logged in can fill out a form to create a login for a new user. Also what do you mean with the check to see if the $validation->fails()?

Last updated 2 years ago.
0

I have also tried the above and I am receiving an error like this:

The HTTP status code "0" is not valid.

Not sure why.

Last updated 2 years ago.
0
$user = User::create($input);

if($user->fails()) {
    // user not saved, return error
}
// user saved, return ok

jeffreydavidson said:

I have also tried the above and I am receiving an error like this:

The HTTP status code "0" is not valid.

Not sure why.

Might be a typo or something in my code as I didn't check if it works

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.