Marios Fakiolas
Marios Fakiolas

Reputation: 1545

Laravel 3 Validation of a single input form

I am using spectacular Laravel Framework but i have a validation issue that i cannot solve in any way trying to validate a single email field.

My form input is:

{{ Form::email('email', Input::old('email'), array('id' => 'email', 'class' => 'span4', 'placeholder' => Lang::line('newsletter.email')->get($lang), 'novalidate' => 'novalidate')) }}

My controller

public function post_newsletter() {

    $email = Input::get('email');

    $v = Newsletter::validator($email, Newsletter::$rules);

    if($v !== true)
    { ... }
    else 
    { ... }
}

and my model

class Newsletter extends Eloquent {

/**
 * Newsletter validation rules
 */
public static $rules = array(
    'email' => 'required|email|unique:newsletters'
);

/**
 * Input validation
 */
public static function validator($attributes, $rules) {
    $v = Validator::make($attributes, $rules);

    return $v->fails() ? $v : true;
}
}

I ve done this so many times with success with much complicated forms but now even if i enter a valid email i get an error message about required fied etc Am I missing something? Maybe is the fact that i try to validate just one single field? I really don't get it why this happens.

Upvotes: 0

Views: 2033

Answers (1)

Jason Lewis
Jason Lewis

Reputation: 18665

I believe this might be because you're not passing in an array of attributes (or an array of data, basically).

Try using the compact function to generate an array like this.

array('email' => $email);

Here is how you should do it.

$v = Newsletter::validator(compact('email'), Newsletter::$rules);

Upvotes: 2

Related Questions