I have a validation function that returns either true or false .
However, I want him to provide information on what the problem is, when it is.
Let's say the function is this:
function is_valid($val) { $result = true; if( rule_1_not_met ) $result = false; if( rule_2_not_met ) $result = false; return $result; }
Used as
$val = $_GET['some_param']; if(!is_valid($val)) $out .= 'Not so helpful feedback.'; ...
I thought I could change it like this:
function is_valid($val) { $result = array(true, array()); if( rule_1_not_met ) $result[1][] = 'Reason 1'; if( rule_2_not_met ) $result[1][] = 'Reason 2'; if(count($result[1]) > 0) $result[0] = false; return $result; }
And use it as follows:
$val = $_GET['some_param']; $validation_result = is_valid($val); if(!$validation_result[0]) $out .= implode('<br/>', $validation_result[1]); ...
My question
- Do I have for unexpected results with this?
- Are there any better ways to achieve this?
PS Would make this community a wiki
source share