0

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.

2
  • Have you tried to debug this to see where it's failing exactly? Is it passing when it shouldn't be or failing when it shouldn't be? Try dumping the Validator object after running the validation. Commented May 9, 2013 at 0:56
  • The validation error is that email is required which means that there are my issues and validation doesnt pass the rules but I don't see why this is happening? Commented May 9, 2013 at 8:01

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are so right. Great and self-documented Laravel says that $attributes in make method is an array. That is why in all other more complicated forms I ve done, validation was fine because I had multiple attributes passed with an array.

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.