Pregunta

In my app there is a simple form with one field (email) that give the possibility to register to the newsletter.

If i entry a new email, all it works fine. If i entry an email that already exists in the database i get the error SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry... Because i had defined that field as unique in the database.

All i want to do is to redirect::back()->with('message', 'email already registered') But i do not know how can i do this? I can just put an if statement in the method controller? Or i have to define it in $rules in the model, adding another rule:

public static $rules = array(
    'email' => 'required',);

Thank you!

¿Fue útil?

Solución

Just define a unique rule on your users table:

public static $rules = array(
    'email' => 'required|unique:users|email');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top