Welcome Guest, Not a member yet? Register   Sign In
Throw Exception, Validation and Error Handling
#1

(This post was last modified: 04-04-2022, 09:13 PM by spreaderman.)

Struggling here.  I have a form with validation.  I have a custom rule with a callback method.  In the callback method, I have a try/catch exception on a google lib that has a good number of custom validation messages eg, not a valid phone number, phone number too long, etc).  How can I validate the form and also get back the custom message

PHP Code:
          // validates the phone number.  takes a phone number and a country code.
            $validation =  \Config\Services::validation();    
            $rules 
= [
                "dh_phone_number" => [
                  "label" => "Phone"
                  "rules" => "teleNumberValidation[dh_phone_number,dh_phone_country]",

                ], 

in the teleNumberValidation i want to do something like this;

   
Code:
    try {
            checkNumber(dh_phone_number,dh_phone_country);
        } catch(\Exception $e) {
            return $e->getMessage();
        } finally {
            return true;
        }

but validation only accepts true or false so how can I get $e->getMessage() into something like this;

     
Code:
          return redirect()->back()
        ->with('errors', $validation->getErrors())
        ->with('warning', 'Invalid Data')
        ->withInput();
Any pointers appreciated.
Reply


Messages In This Thread
Throw Exception, Validation and Error Handling - by spreaderman - 04-04-2022, 09:13 PM



Theme © iAndrew 2016 - Forum software by © MyBB