Re: Re: speaking of control structures...

From: Date: Thu, 07 May 2009 15:56:17 +0000
Subject: Re: Re: speaking of control structures...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-292384@lists.php.net to get a copy of this message
On 5/6/09 4:02 PM, "Al" <news@ridersite.org> wrote: > Here's the way I handle validating user form inputs. Each function validates > several things and throws an error with the message stating what's wrong. > > try > { > checkEmailAddr($userSubmitedDataArray[EMAIL_ADDR_FIELD]); > checkPhoneDigits($userSubmitedDataArray[PHONE_NUM_FIELD], > 'phone'); > checkNotes($userSubmitedDataArray, $sizesArray); > if(!empty($userSubmitedDataArray[CELLPHONE_NUM_FIELD])) > { > checkPhoneDigits($userSubmitedDataArray[CELLPHONE_NUM_FIELD], > 'cell'); > checkCellCarrier($userSubmitedDataArray['carrier']); > } > } > > catch (Exception $e) > { > $userErrorMsg = $e->getMessage(); //Message text in check > function > } > > A typical function looks like this: > > function checkEmailAddr($emailAddr) > { > if(empty($emailAddr)) > { > throw new Exception("No email address provided"); > } > > if(!preg_match("%\w+@%", $emailAddr)) > { > throw new Exception("Email address missing mailbox name."); > } > > if(!filter_var($emailAddr, FILTER_VALIDATE_EMAIL)) > { > throw new Exception("Email address error. Syntax is wrong. "); > } > $domain = substr(strchr($emailAddr, '@'), 1); > if(!checkdnsrr($domain)) > { > throw new Exception("Email address warning. Specified domain > \"$domain\" appears to be invalid. Check carefully."); > } > return true; > } thanks for the example, Al. the combination of checker functions and exceptions (as far as i understand them, the exceptions chapter of the php manual is a little terse) so you can throw from inside the checker seems convenient.

« previous php.general (#292384) next »