views:

219

answers:

10

Hi,

I'm doing some validation now in PHP, and I'm running allot of conditional statements, for example:


if ($this->email->isValid($email))
return false;
if ($this->username->isValid($username))
return false;

ect..

Is there a nice way to do this? Or do I just run ten If statements like the ones above? I can't use switch obviously, but I'm looking for that type of solution..

P.S... I'm using Zend Framework for validation

+3  A: 

You could OR them like this:

if(cond1||
   cond2||
   cond3||
   cond4)
{
  return false;
}
Blindy
i have to know where something went wrong.. so that won't work
Daniel, with your current code, you won't know what's going wrong either. Can you post a better example?
Matthew Flaschen
A: 

If this is data from a form, take a look at Zend_Form, as recommended by a poster in your other question

I posted some example code which details adding validators to form elements

David Caunt
two different daniels..
Just a coincidence that you write your questions in the same style, and use similar code then!
David Caunt
A: 

Just make sure that you put the most obvious validation at the top, so if its going to fail it will trip before it runs through every statement. Also, I don't like if statements without curly brackets, but then thats just a matter of opinion.

Christian
A: 
if ($this->email->isValid($email) || 
    $this->username->isValid($username))
        return false;
Matthew Flaschen
A: 

It is always good to use some var than multiple returns

In case these are the only return conditions. We dont need to check for all conditions. like check only for true conditions return false by default or vice-versa.

$result = false; //return false by default.

if (cond1 || cond2) { //check only for conditions which result in returning true. :)

  $result = true;

}

return $result;

Cheers,

-Ratnesh

Ratnesh Maurya
be careful of that word: "always"
nickf
yeah.. u r right. thanks!!
Ratnesh Maurya
A: 

Maybe something like this:

foreach( $form->getElements as $element => $value )
{ 
    if( !$element->isValid( sanitize($value))){
       return false;
    }
}

BUT if you are using ZF this is your oneliner answer because you check all the form in one not individual fields:

$form = new My_Zend_Form(); // in the controller action that you send the form to

if ( $form->isValid($_POST)) {
    // Success /// do whatever you want with the data
    $data = $form->getValues();
} else {
    //error
}
Elzo Valugi
+1  A: 

Doing something like this is called a Guardian clause. It can be improved so clauses that return the same value should be grouped together.

if ($this->email->isValid($email) || $this->username->isValid($username))
{
    return false;
}

Or like this, if there are many (btw how I format the if is just so it can read better, any other way should be fine as well)

if (
    $this->email->isValid($email) || 
    $this->username->isValid($username) || 
    $this->somethingelse()
   )
{
    return false;
}
Ólafur Waage
A: 

I would make them data members of my class. Obviously here you will have to have a form driven class. So here for example the email could be wrapped into a class and initialised with in the constructor of the class that has them as member variables. Now the email wrapper class will do validation for email on initialisation/construction.

IMHO that would look less cluttered and you can wrap up any validation or specific methods to the email-wrapper class.

I know it might be a sledge hammer in some context; so choose wisely!

CodeMedic
A: 

Do something like this:

$errlvl = 0;

if($errlvl == 0 && $this->email->isValid($email)){
    $errlvl++;
}

if($errlvl == 0 && $this->username->isValid($username)){
    $errlvl++;
}

// your validation list keeps going

if($errlvl > 0){
    return false;
}

this will

  1. Reduce redundancy because if there's an error in front, the following will not be checked.
  2. you can keep adding on to the list
  3. if you want to know how many errors occurred, you can remove $errlvl == 0 in the statements
thephpdeveloper
A: 

Add all objects to an array and the iterate it (I guess you or Zend are using an interface "validator" or something like that each component (username, email, etc) implements the "isValid" method):

$validators = array();
$validators[] = $this->email;
$validators[] = $this->username;

foreach ($validators as $validator)
{
    if (!$validator->isValid())
    {
        return false;
    }
}
inakiabt