1

I need to be able to catch an error. I have the following code

if($call['status_id'] != '' && $call['datetime_required'] != '')
{
   //do stuff
}
else
{
  // tell them how it failed
}

How would I go about displaying the section on which ti failed. So for example I can return a dynamic error message ie

return 'You did not fill in the field $errorField';

Where

$errorField

Is the if check on which it failed.

UPDATE

I currently code like so

if($call['status_id'] != '')
{
    if ($call['datetime_required'] != '')
    {
      //do stuff
    }
    else
    {
      // tell them it failed due to the second condition
    }
}
else
{
   // tell them it failed due to the first condition
}

But would like to do the check in one line and change the message depending on where ti failed.

Note @jack had already posted his answer before this update.

4
  • If this is in a function, you certainly can return a string that includes the error message and check to see if the return value === TRUE otherwise assume an error condition exists and the return value is the error. Commented Oct 3, 2013 at 15:44
  • Yes this is a class method Commented Oct 3, 2013 at 15:47
  • Is all the data being validated stored in the class members that could be iterated through? Commented Oct 3, 2013 at 15:47
  • The database allows null fields and therefore I am validating the data before assigning it to the relevant properties and saving it in the database. I am a bit new at this btw Commented Oct 3, 2013 at 15:52

2 Answers 2

3

I'm not sure I fully understand you, you mean something like this?

function check($call, $req_fields) {
    $failed = array();
    foreach($req_fields as $field) {
        if(!$call[$field]) {
            $failed[] = $field;
        }
    }
    if(count($failed)) {
        return join(', ', $failed) . ' are required.';
    }
    return 'success?' ;
}

$message = check($call, array('status_id', 'datetime_required'));
Sign up to request clarification or add additional context in comments.

Comments

2
if($call['status_id'] != '')
{
    if ($call['datetime_required'] != '')
        //do stuff
    }
    else
    {
        // tell them it failed due to the second condition
    }
}
else
{
  // tell them it failed due to the first condition
}

5 Comments

I like this solution as it provides granularity on what failed versus a blanket "failed" statement. It's more user friendly.
I've been doing this, but a colleague of mine recommended that this may bloat the code too much and cause too many nested if statements. So it may be better to do all the checks at once and then return the error.
@JonPaulH Then you should use OneOfOne's solution: stackoverflow.com/a/19163455/662761. All the fields have to be stored in an iterable for it to work.
Ok I thought there may be a way to catch which one it failed on. I had seen it before with pdo for example but this is obviously something to do with exceptions etc. Need to read more I think. Cheers everyone
I normally code as above because then I know exactly what I expect and no gremlins can get in, but someone told me otherwise (more experienced than me). Think I'll stick to this though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.