0

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

1 Answer 1

1

You are missing the input in the validator, you did not defined it

Use it like

$input = Input::all();

$validator = Validator::make($input, $rules);

or

$validator = Validator::make(Input::all(), $rules);

and take a look at the forums, this will help you more than that blog: http://forums.laravel.io/viewtopic.php?id=12104

Sign up to request clarification or add additional context in comments.

4 Comments

Should the validation happen in the controller or the model? There is no clear information on this but I would guess the controller given it is logic
choose what ever suits your app what you are building, but its optional to place it in models, and one thing check out this package github.com/laravelbook/ardent, lot of people use it including my self, will save you a lot of time and its powerful
Thanks got it working from controller. Although recommendations in some blogs state to use it in the model, I decided to go with the controller given it is logic and data should never reach model unless ready for storage.
thats why i higlighted, it depends on your app, it is just optional to store your validation data in a model, but not required :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.