2

I am working in Laravel.

I have a URL like as below:

http://localhost/udonls/public/mastercandidate/candidate/reset-password/11/[email protected] 

Now has a function which is like

public function sendCanResLink(Request $request, $id, $email) 
{
       // So in this function how should i validate "$email" using laravel validation ?
}

Here is the route:

Route::get('/candidate/reset-password/{id}/{email}', [
   'as'    => 'candidate.reset.password',
   'uses'  => 'CandidateSmdnContoller@sendCanResLink'
]);

Overall !! I want to validate my email address which I get in the GET request in larvae with Laravel validation.

I know regex technique but I want to use the Laravel validation technique.

1
  • First you have to change the route method. Commented May 6, 2020 at 8:45

2 Answers 2

2

Maybe something like this:

 public function sendCanResLink(Request $request, $id, $email) 
 {
    try {
         $validator = Validator::make(compact('email'), [
            "email" => "required|email:rfc,dns,filter,spoof",
         ])->validate();
    } catch(ValidationException $validation) {
        return redirect()->back();
    }
  }
Sign up to request clarification or add additional context in comments.

5 Comments

I will try this solution and back with the result
Did it work? Also have you tried to throw that exception to be perfectly sure that it did work as intended?
It definitely works, I have just tried as of 04/2022. But for the DNS part, ti checks only if that domain exists, if the account doesn't, still passes, but, better than nothing or complex regexes.
You are referring to this: github.com/egulias/EmailValidator/blob/master/src/Validation/… ? Well, I'll look for a solution for the account "check", If I find something I'll edit the solution
This is a paid service (but the pricing is actually nice) : emaillistverify.com
2

Why spend resources on validation in this case, if you can check one on one for the user?

Route::get('/candidate/reset-password/{user}/{email}', [
   'as'    => 'candidate.reset.password',
   'uses'  => 'CandidateSmdnContoller@sendCanResLink'
]);

public function sendCanResLink(Request $request, User $user, $email) 
{
   if($user->email !== $email) return 'some error';

   // reset code here

   return 'ok'; 
}

2) With validator

public function sendCanResLink(Request $request, $id, $email) 
{
   Validator::make(compact('email'), [
       'email' => ['required', 'string', 'regex:/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/', 'max:255', 'unique:YOUR_TABLE']
   ])->validate();
  // auto redirect back if error and use 
  // @error('email') {{ $message }} @enderror in blade 
}

2 Comments

Actually i was updating the email also and here the latest mail is not yet updated but added in the form so talking direct email form the form.
@always-a-learner I added a second method as you want. he still checks the uniqueness in the database

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.