Search code examples
phpformslaravellaravel-4laravel-validation

Laravel : All validation rules fail always


I am trying to validate a retrieved form with User::$rules and the validation always return failed with all the fields required ! and when i died and dump the Input::All() in the controller i get all the input fields i gave when submitted the form which means the issue is in the Model, right ? I never had such a weird problem ! Help please

Controller : UsersController

public function postSinscrire() {

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


    if($validator->passes())  {
        $user = new User;
        $user->email = Input::get('email');
        $user->email_confirmation = Input::get('email_confirmation');
        $user->number = Input::get('number');
        $user->pseudo = Input::get('pseudo');
        $user->banned_user = 0;
        $user->activation_email = 0;
        $user->activation_number = 0;
        $user->sex = Input::get('sex');
        $user->birth = Input::get('birth');
        $user->wilaya_code = Input::get('wilaya_code');
        $user->user_type = 1;
        $user->password = hash::make(Input::get('password'));
        $user->password_confirmation('password_confirmation');
        $user->save();

        return Redirect::to('membre')
            ->with('message', 'Welcome');
    }
    if($validator->failed()) {
            return Redirect::to('membre/sinscrire')
                ->with('message','Une erreur s\'est produise, Corrigez puis réessayez a nouveau')
                ->withErrors($validator)
                ->withInput();
            }

Model : User

 protected $hidden = array('password', 'remember_token');

    protected $fillable = array(

        'email','number','pseudo','sex','birth','wilaya_code',

        );

    public static $rules = array(

        'email'=>'max:254|unique:users|email|required',
        'email_confirmation'=>'max:254|unique:users|email|required',
        'number'=>'between:8,15|unique:users',
        'pseudo'=>'max:35|unique:users|required|alpha_num',
        'banned_user'=>'boolean|integer',
        'activation_email'=>'boolean|integer',
        'activation_number'=>'boolean|integer',
        'sex'=>'boolean|integer|required',
        'birth'=>'required|date',
        'wilaya_code'=>'integer|required|max:2',
        'activation_date'=>'date',
        'user_type'=>'integer',
        'password'=>'required|between:8,12|alpha_num|confirmed',
        'password_confirmation'=>'required|between:8,12|alpha_num'

        );

And then i get the required error for all fields !

  • The email field is required.
  • The email confirmation field is required.
  • The pseudo field is required.
  • The sex field is required.
  • The birth field is required.
  • The wilaya code field is required.
  • The password field is required.
  • The password confirmation field is required.

Updated : View

{{ Form::open(array(
    'url'=>'membre/sinscrire',
    'method'=>'POST'
    )) }}

<label for="email">Email :</label>
{{ Form::text('email')}} <br>

<label for="email_confirmation">Email :</label>
{{ Form::text('email_confirmation')}} <br>

<label for="pseudo">Identifiant ( pseudo ) :</label>
{{ Form::text('pseudo')}} <br> 

<label for="password">Mot de passe :</label>
{{ Form::password('password')}} <br>

<label for="password_confirmation">Confirmation :</label>
{{ Form::password('password_confirmation')}} <br>

<label for="gender">Sexe :</label>
{{ Form::select('gender',array('0'=>'Homme', '1'=>'Femme'))}} <br>

<label for="sana_hilwa">Date de naissance :</label>
{{ Form::selectRange('year', 1930, 2011); }}
{{ Form::selectRange('month', 01, 12); }}
{{ Form::selectRange('day', 01, 31); }} <br>

<label for="al_wilaya">Wilaya :</label>
{{ Form::select('al_wilaya', array(
            '1' => 'Adrar',
            '2' => 'Chlef',
            '31' => 'Oran'
    )) }} <br>

En cliquant sur “Je m'inscris”, vous indiquez que vous avez lu, compris et accepté les <a href="/apropos"><strong>conditions d\'utilisation</strong></a> de Ouedkniss. <br>

{{ Form::submit('Inscrivez vous')}}

{{ Form::close()}}

and when i dd('Input::all');

array (size=11)
  '_token' => string 'mCVboVC123456789nDpOJZXyjiY5YsUrjRMGunPx' (length=40)
  'email' => string '[email protected]' (length=11)
  'email_confirmation' => string '[email protected]' (length=11)
  'pseudo' => string 'foo' (length=3)
  'password' => string 'testpass' (length=8)
  'password_confirmation' => string 'testpass' (length=8)
  'gender' => string '0' (length=1)
  'year' => string '1930' (length=4)
  'month' => string '1' (length=1)
  'day' => string '1' (length=1)
  'al_wilaya' => string '1' (length=1)

Note: the birth field is not done correctly from the view

i was hoping get only the birth field required, if this is the problem, any view script that is compatible with Carbon ? Thank you

update : DD('User::$rules');

array (size=14)
  'email' => string 'required|max:254|email|unique:users|confirmed' (length=45)
  'email_confirmation' => string 'required|max:254|email|unique:users' (length=35)
  'number' => string 'between:8,15|unique:users' (length=25)
  'pseudo' => string 'required|alpha_num|max:35|unique:users' (length=38)
  'banned_user' => string 'boolean|integer' (length=15)
  'activation_email' => string 'boolean|integer' (length=15)
  'activation_number' => string 'boolean|integer' (length=15)
  'sex' => string 'required|boolean|integer' (length=24)
  'birth' => string 'required|date' (length=13)
  'wilaya_code' => string 'required|max:2|integer' (length=22)
  'activation_date' => string 'date' (length=4)
  'user_type' => string 'integer' (length=7)
  'password' => string 'required|between:8,12|alpha_num|confirmed' (length=41)
  'password_confirmation' => string 'required|between:8,12|alpha_num' (length=31)

Solution

  • Check your validation rules carefully.

     'email'=>'max:254|unique:users|email|required',
     'email_confirmation'=>'max:254|unique:users|email|required'
     'number'=>'between:8,15|unique:users',
    

    If you are updating and used previous email or number, validation will always fail.

    If that the case, before you update, fetch your email and number first and check them against input data. If they are match with existing data, don't use validation rules for those fields, else use these validation rules.