always-a-learner
always-a-learner

Reputation: 3794

Validate the Email which added in the URL in Laravel with using laravel validation rule only

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.

Upvotes: 2

Views: 873

Answers (2)

Marco Edoardo Duma
Marco Edoardo Duma

Reputation: 71

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();
    }
  }

Upvotes: 2

Kongulov
Kongulov

Reputation: 1161

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 
}

Upvotes: 2

Related Questions