Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Validation function to return true|false, AND a message if false

Tags:

function

php

I have a validation function which returns either true or false.
However, I want it to provide info as to what the problem is, when there is one.

Let's say the function is like this:

function is_valid($val) {
  $result = true;
  if( rule_1_not_met ) $result = false;
  if( rule_2_not_met ) $result = false;
  return $result;
}

Which is used like this

$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 like this:

$val = $_GET['some_param'];
$validation_result = is_valid($val);
if(!$validation_result[0]) $out .= implode('<br/>', $validation_result[1]);
...

My question is

  • Am I in, for unexpected results with this?
  • Are there better ways to achieve this?

P.S. Would make this community wiki

like image 706
Majid Fouladpour Avatar asked Apr 23 '11 15:04

Majid Fouladpour


People also ask

How can return true or false function in PHP?

Definition and Usage The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.

What returns true or false values?

Booleans represent one of two values: True or False .

Is return 0 the same as false?

The values 1 and 0 are of type int and are not implicitly convertible to boolean, that means: return 0: returning false from a function. return 1: returning true from a function.

What is return false in PHP?

return $oh || false does not work in PHP like it works in JavaScript. It would always return a boolean value (true or false). – Matthew. Apr 13, 2011 at 15:15. $result = $oh OR false will work as expected, since OR has a lower precedence than the return (but the second option must be a boolean).


3 Answers

You are in the right track but I would like to do this in this way

function is_valid($val,&$mes) {
  $result = true;
  if( rule_1_not_met ) { $mes[]='message one'; $result = false; }
  if( rule_2_not_met ) { $mes[]='Message two'; $result = false; }
  return $result;
}

$mes=array();
if(isvalid($val,$mes) ===false)  $out .= implode('<br/>', $mes);
like image 189
Shakti Singh Avatar answered Nov 15 '22 13:11

Shakti Singh


You could use a Result object that encapsulates return data, a message and a status.

i.e.

class Result( $bResult, $sMessage, $mData ) {
    public function __construct() {
        $this->bResult = $bResult;
        $this->sMessage = $sMessage;
        $this->mData = $mData;
    }
}

In Your code:

$result = new Result(true, 'some helpful message here', null);
like image 27
Brad Thomas Avatar answered Nov 15 '22 14:11

Brad Thomas


$reasons = array();
function is_valid($val)
{
    global $reasons;
    if ( rule_1_not_met ) $reasons[] = 'Reason 1';
    if ( rule_2_not_met ) $reasons[] = 'Reason 2';
    if ( count($reasons) == 0 )
        return TRUE;
    else
        return FALSE;
}

if (!is_valid($condition))
{
    echo 'Was not valid for these reasons<br />';
    foreach($reasons as $reason)
        echo $reason, '<br>';
}
else
    echo 'Is valid!';
like image 26
Mark Tomlin Avatar answered Nov 15 '22 14:11

Mark Tomlin