0

I am trying to have my own simple custom validation function

Here is my code :

    $rules = array(
                    'first_name'=>'required|alpha|min:2',
                    'last_name'=>'required|alpha|min:2',
                    'email'=>'sometimes',
                    'password'=>'alpha_num|between:6,12|confirmed',
                    'password_confirmation'=>'alpha_num|between:6,12'
                    );
                    // run the validation rules on the inputs from the form
                    $input = Input::all();
                    $validator = Validator::make($input, $rules ); 
                    $validator->sometimes('email', 'required|email', function($input)
                    {
                            // get user info from db 
                          $user = User::find(Input::get('id'));

                      if (Input::get('email') != $user->email)
                         {
                            // find that email in the database 
                             $foundemail = DB::table('users')->where('email', '=' ,Input::get('email'))->get();
                            if(!empty($foundemail))
                             return false;


                          }
                          else
                          {
                            return true;

                          }
                  });   

how do I add a custom message to this , basically I need to check for email field: 1). required 2). email 3). if email edited is the same as the one if the database , then continue with the edit. if email is different I need to check inputed email if it is in the database and show an error

how could I do that please help

Thanks

1 Answer 1

1

Laravel provides built-in rule to check whether value is unique in database or not, please check this out: http://laravel.com/docs/validation#rule-unique

Sign up to request clarification or add additional context in comments.

11 Comments

yes I know but the problem I am doing an edit not an add new , if I use unique:users,email , it will check that is the user is not unique and it gives me an error how to by pass that?
You mean you'd like to check if it's unique, but between all records except the one that is edited? So if you edit record with ID = 5, it should check if the email is unique where ID != 5?
I tried this 'email'=>'required|email|unique:users,email', and I get and error message saying my email is taken , but I am doing an update to that email
yes exactly how can I do that , if its unique except the one I am editing , basically its for an eidt user
unique rule provides that, please read carefully all use cases in the link I provided :)
|

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.