Question

I have a problem when using Route::post in Laravel 4.

This is my User.php (laravel model) code:

class User extends Eloquent implements UserInterface, RemindableInterface {

  public static function validate($input)
  {
    $rules = array(
    'email' => 'Required|Between:3,64|Email|Unique:users',
    'password' => 'Required|AlphaNum|Between:4,8|Confirmed',
    'password_confirmation' => 'Required|AlphaNum|Between:4,8'
    );

    $v = Validator::make($input, $rules);
  }
}

This is my routes.php code:

Route::post('register', function()
{
  $v = User::validate(Input::all());

      if ($v->passes()){
      $u = new User();
      $u->email = Input::get('email');
      $u->password = Hash::make(Input::get('password'));
      $u->save();
      Auth::login($u);

      return Redirect::to('createprofile');
    }
    else{
      return Redirect::to('register')->withErrors($v->getMessageBag());
    }
});

This is my register_user.blade.php code:

@section('content')
    {{ Form::open(array('url' => '/register', 'method' => 'post')) }}
    {{ Form::text('email') }}
    {{ Form::label('email', 'Your Email') }}</br>
    {{ Form::password('password'); }}
    {{ Form::label('password', 'Your Password') }}</br>
    {{ Form::password('password_confirmation'); }}
    {{ Form::label('password_confirmation', 'Confirm Your Password') }}</br>
    {{ Form::submit('Go') }}
  {{ Form::close() }}
@stop

The issue seems to be when the form submits to the Route::post it does not recognize

$v = User::validate(Input::all()) 

as a valid object, instead giving me a call to a member function passes() on a non-object.

var_dump($v)

comes equal to null.

Would anyone know what the problem here is? Is User::validate() the correct way to call the function from the User model?

Was it helpful?

Solution

You forgot to return your Validator instance;

class User extends Eloquent implements UserInterface, RemindableInterface {

  public static function validate($input)
  {
    $rules = array(
    'email' => 'Required|Between:3,64|Email|Unique:users',
    'password' => 'Required|AlphaNum|Between:4,8|Confirmed',
    'password_confirmation' => 'Required|AlphaNum|Between:4,8'
    );

    return Validator::make($input, $rules);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top