Wednesday, March 1, 2017

Why does the unique form validation role stop working in this Laravel application?

I am pretty new to Laravel and I have this strange behavior with form input validation.

The strangest thing is that yesterday this code worked fine and today it stopped working and I can't explain why.

I have these validation rules in a controller method that handle the form submission:

    $data = Input::all();

    Log::info('INSERTED DATA: '.implode("|", $data));

    // Regole di validazione sintattica del contenuto del form di registrazione:
    $rules = array(
        'name' => 'required',
        'surname' => 'required',
        'login' => 'required|unique:pm_user,login',
        'email' => 'required|email|confirmed|unique:pm_user,email',
        //'email_confirmation' => 'required|email|confirmed',
        'pass' => 'required|required|min:6',
        //'passConfirm' => 'required',
        'g-recaptcha-response' => 'required|captcha',

    );


    // Validazione sintattica del form di registrazione:
    $validator = Validator::make($data, $rules);

    /*
     * Se il form di registrazione contiene dati sintatticamente errati, ritorna alla pagina di registrazione
     * passando la lista dei messaggi di errore da visualizzare
     */
    if ($validator->fails()){
        return Redirect::to('/registration')->withInput()->withErrors($validator);
    }

    // Altrimenti se i dati inseriti sono sintatticamente corretti:
    else {

        $token = md5(uniqid($data['email'], true));

        ...............................................................
        ...............................................................
        DO SOMETHING AND WRITE THE DATA INTO THE pm_user TABLE
        ...............................................................
        ...............................................................

     }

As you can see the $rules array contains these two specific rules:

'login' => 'required|unique:pm_user,login',
'email' => 'required|email|confirmed|unique:pm_user,email',

These rules check if in the pm_user table there is a record with the same login and email fields values. If there is a record with the same login value or the same email value, it gives an error.

Until yesterday this worked fine. Now this behavior stopped working. In fact, if I insert in the form a new user with the same username (login field) or/and the same email, it gives no error and enters into the else clause, and inserts the user into the table.

All the other validation rules works fine.

How is it possible? How can I try to fix this issue?




via AndreaNobili

Advertisement