PHP Tutorial Laravel 4 Framework (Authentication) - Creating User Account III
Laravel 4
- Installing on Ubuntu - local
- Installing on a Shared host
- Installing on Windows
- Creating users table
- Home page with controller and blade
- Blade Templating
- Database connection and sending emails
- Creating user account I - GET
- Creating user account II - POST
- Creating user account III - Error checking & redirecting input
- Creating user account IV - User::create()
- Activating user account I - Mail::send()
- Activating user account II - Update user's status
- User account sign-in I - Route and link
- User account sign-in II - Validation
- User account sign-in III - Login Authentication
- Singing out
- Remember the user
- Changing Password I
- Changing Password II
- Recovering forgotten password I
- Recovering forgotten password II
- User Profile
- Database Migration using artisan
This tutorial is the continuation from Creating user account II - POST.
We want to have more fields related to the account creation. So, let's modify create.blade.php:
@extends('layout.main') @section('content') <form action="{{ URL::route('account-create-post') }}" method="post"> <div class="field"> Email: <input type="text" name="email"> </div> <div class="field"> Username: <input type="text" name="username"> </div> <div class="field"> Password: <input type="password" name="password"> </div> <div class="field"> Password again: <input type="password" name="password_again"> </div> <input type="submit" value="Create account"> {{ Form::token() }} </form> @stop
Let's see what's been changed when we hit the uri:
If we hit the root:
Now that we have forms, it's time to add functionality to those forms as well as validation and error handling.
Let's open AccountController.php and so a test:
public function postCreate() { print_r(Input::all()); }
When we hit "Create account", it returns an array like this:
Using the Input::all(), we can put validator:
<?php class AccountController extends BaseController { /* Viewing the form */ public function getCreate() { return View::make('account.create'); } /* Submitting the form */ public function postCreate() { $validator = Validator::make(Input::all(), array( 'email' => 'required|max:50|email|unique:users', 'username' => 'required|max:20|min:3|unique:users', 'password' => 'required|min:6', 'password_again'=> 'required|same:password' ) ); if($validator->fails()) { return Redirect::route('account-create') ->withErrors($validator) ->withInput(); // redirect with inputs } else { // create an account } } }
We need to add error output to create.blade.php:
@extends('layout.main') @section('content') <pre>{{ print_r($errors) }}</pre> <form action="{{ URL::route('account-create-post') }}" method="post"> <div class="field"> Email: <input type="text" name="email"> </div> <div class="field"> Username: <input type="text" name="username"> </div> <div class="field"> Password: <input type="password" name="password"> </div> <div class="field"> Password again: <input type="password" name="password_again"> </div> <input type="submit" value="Create account"> {{ Form::token() }} </form> @stop
If we click the "Create account" button without filling any data or wrong format, we get the following on our page:
Obviously, that kind of error messages are not desirable since it's not user friendly. So, we need to handle each error for each field.
<div class="field"> Email: <input type="text" name="email"> @if($errors->has('email')) {{ $errors->first('email')}} @endif </div>
Let's see what changes have been made to the error message if we try to create an account without email input:
Ii I typed in the email that has been taken, tom@bogotobogo.com:
Also, we do not want any valid input disappear and re-type in due to an error. We want to redirect it. Fortunately, there is a way to put the old value back by setting the value attribute to the old one:
<div class="field"> Email: <input type="text" name="email" value="{{ Input::old('email') }}"> @if($errors->has('email')) {{ $errors->first('email')}} @endif </div>
Now, we want to do the same for 'username' as well. Also, error handling should be done to other fields.
@extends('layout.main') @section('content') <form action="{{ URL::route('account-create-post') }}" method="post"> <div class="field"> Email: <input type="text" name="email" value="{{ Input::old('email') }}"> @if($errors->has('email')) {{ $errors->first('email')}} @endif </div> <div class="field"> Username: <input type="text" name="username" value="{{ Input::old('username') }}"> @if($errors->has('username')) {{ $errors->first('username')}} @endif </div> <div class="field"> Password: <input type="password" name="password"> @if($errors->has('password')) {{ $errors->first('password')}} @endif </div> <div class="field"> Password again: <input type="password" name="password_again"> @if($errors->has('password_again')) {{ $errors->first('password_again')}} @endif </div> <input type="submit" value="Create account"> {{ Form::token() }} </form> @stop
If submitted with all empty fields:
With incorrect email format and username already taken while others are correct:
With password pair not matching:
Laravel 4
- Installing on Ubuntu - local
- Installing on a Shared host
- Installing on Windows
- Creating users table
- Home page with controller and blade
- Blade Templating
- Database connection and sending emails
- Creating user account I - GET
- Creating user account II - POST
- Creating user account III - Error checking & redirecting input
- Creating user account IV - User::create()
- Activating user account I - Mail::send()
- Activating user account II - Update user's status
- User account sign-in I - Route and link
- User account sign-in II - Validation
- User account sign-in III - Login Authentication
- Singing out
- Remember the user
- Changing Password I
- Changing Password II
- Recovering forgotten password I
- Recovering forgotten password II
- User Profile
- Database Migration using artisan
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization