Throw Exception, Validation and Error Handling - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Throw Exception, Validation and Error Handling (/showthread.php?tid=81663) |
Throw Exception, Validation and Error Handling - spreaderman - 04-04-2022 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. in the teleNumberValidation i want to do something like this; Code: try { but validation only accepts true or false so how can I get $e->getMessage() into something like this; Code: return redirect()->back() RE: Throw Exception, Validation and Error Handling - kenjis - 04-04-2022 See https://codeigniter4.github.io/userguide/libraries/validation.html#creating-custom-rules Use &$error. RE: Throw Exception, Validation and Error Handling - spreaderman - 04-05-2022 (04-04-2022, 10:16 PM)kenjis Wrote: See https://codeigniter4.github.io/userguide/libraries/validation.html#creating-custom-rules Hi Kenjis, thank you for the link. I have read through it but not sure still how I would return the exception error message. If you could kindly let me know a bit more. I have tried to dig through other projects like bonfire 2 to try to find how it is done but I am also finding it hard to get an example. RE: Throw Exception, Validation and Error Handling - kenjis - 04-05-2022 Something like this: PHP Code: public function teleNumberValidation( See also https://codeigniter4.github.io/userguide/libraries/validation.html#allowing-parameters RE: Throw Exception, Validation and Error Handling - spreaderman - 04-05-2022 Thank you Kenjis. Almost clear. I don’t understand the need to pass in &$error in my case. Also, no need to return $error. I am also not clear on references but am reading about them still. Thanks! RE: Throw Exception, Validation and Error Handling - kenjis - 04-05-2022 See https://www.php.net/manual/en/language.references.pass.php The validation method must return boolean. So it can't return error strings. So we use Passing by Reference. RE: Throw Exception, Validation and Error Handling - spreaderman - 04-07-2022 Fantastic. Read all about references. Understood now. Many Thanks Kenjis. |