views:

30

answers:

1

I have overriddend the isValid() function for a form, and I was wondering if there are any drawbacks to adding logic there.

Example code:

public function isValid($data)
{
    // conditional requirement of fields

    $isValid = parent::isValid($data);

    if ($isValid) {
        // additional validation
    }

    return $isValid;
}
+2  A: 

No, none. But I'd put an if statement around the parent:

$parentIsValid = parent::isValid($data);
if($parentIsValid) {
    //Extra stuff, you may as well only do this if the parent valid function returns true
    //Set isValid var in here
} else $isValid = false;
return $isValid;
Ashley
Good observation Ashley. I do use a conditional for the additional validation. Is there a specific reason you use two boolean variables, or was that to illustrate the example?
Sonny
Yes just to illustrate the example. You could always use the parent::isValid function inside the if statement or rename parentIsValid just to isValid
Ashley
I'll accept your answer some time tomorrow if no-one weighs in with a pitfall. Thanks Ashley!
Sonny