I am working with Laravel 4 validation right now. My basic setup is done and tested. I can fill the form in view and submit to controller. I can save all details to database using model. I am having a problem now with validation.
I use Input::get() to capture each of the posted variables in the controller. I read that validation should ideally be done in the model. where am I supposed to invoke the validator? Model or Controller? and how am I supposed to pass the validator the $input? is it an array of all variables posted or am I missing something?
Laravel 4 documentation really fails to illustrate with examples answers to common usage questions.
This is the validator I set up in my model:
public static function validate($input)
{
$rules = array(
# place-holder for validation rules
'firstname' => 'Required|Min:3|Max:40|Alpha',
'lastname' => 'Required|Min:3|Max:40|Alpha',
'email' => 'Required|Between:3,64|Email|Unique:users',
'country' => 'Required',
'password' =>'Required|AlphaNum|Between:7,15|Confirmed',
'password_confirmation'=>'Required|AlphaNum|Between:7,15'
);
# validation code
$validator = Validator::make($input, $rules);
/*if( $validator->passes() ) {
} else {
# code for validation failure
}*/
}
controller:
public function register()
{
/*Create new user if no user with entered email exists. Use validator to ensure all fields are completed*/
$user = new User;
/*Handle input in POST*/
$email = Input::get('email');
$password = Input::get('password');
$passwordConfirmed = Input::get('password_confirmation');
$firstName = Input::get('firstname');
$lastName = Input::get('lastname');
$country = Input::get('country');
$user->email = $email;
$user->password = Hash::make($password);
$user->firstname = $firstName;
$user->lastname = $lastName;
$user->country = $country;
//$user->save();
$this->layout->content = View::make('test');
}
and I've been following this link so far when it comes to validation. Please help as I am new to this framework