Laravel-mongodb: Exist uniqueness validation not handled by Jensseger

Created on 2 Apr 2014  路  3Comments  路  Source: jenssegers/laravel-mongodb

I have a registration form with unique email validation, when i enter multiple duplicate emails it does not apply unique validation for email i have rules below, Note: i am using mongodb.
public static $rules = array(
'email' => 'required|email|unique:users'
);

Most helpful comment

In case someone runs into this again, the likely culprit is if you're using multiple database connections and MongoDB is not your default. In this case, you must set the presence verifier's connection to your MongoDB connection key prior to validating, then all should work well.

One way to do it is in the authorize() method of a custom FormRequest class (because it is called prior to validate()). Example:

public function authorize(){
    // Make MongoDB the presence verifier
    $this->getValidatorInstance()->getPresenceVerifier()->setConnection('mongodb');
    // in this case we're authorizing any user
    return true;
}

Other solution is to specify the database connection in unique validation

'email' => 'unique:mongodb.users,email_address'

All 3 comments

In case someone runs into this again, the likely culprit is if you're using multiple database connections and MongoDB is not your default. In this case, you must set the presence verifier's connection to your MongoDB connection key prior to validating, then all should work well.

One way to do it is in the authorize() method of a custom FormRequest class (because it is called prior to validate()). Example:

public function authorize(){
    // Make MongoDB the presence verifier
    $this->getValidatorInstance()->getPresenceVerifier()->setConnection('mongodb');
    // in this case we're authorizing any user
    return true;
}

In case someone runs into this again, the likely culprit is if you're using multiple database connections and MongoDB is not your default. In this case, you must set the presence verifier's connection to your MongoDB connection key prior to validating, then all should work well.

One way to do it is in the authorize() method of a custom FormRequest class (because it is called prior to validate()). Example:

public function authorize(){
    // Make MongoDB the presence verifier
    $this->getValidatorInstance()->getPresenceVerifier()->setConnection('mongodb');
    // in this case we're authorizing any user
    return true;
}

Other solution is to specify the database connection in unique validation

'email' => 'unique:mongodb.users,email_address'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cherbert picture cherbert  路  3Comments

ghost picture ghost  路  3Comments

phuocduy1988 picture phuocduy1988  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments

yupangestu picture yupangestu  路  3Comments