0

Problem

Hey, my array $isActiefErr and $naamErr doesn't fill up after I throw an exception, but the foreach loop does continue to validate the values. I know the foreach loop keeps on looping, because the array which should have all the values is filled, but the error array stops after it encounters its first error.

What I have tried:

I've tried to print the array but it only pushes the first error into my array. It then stops validating. This also shows in my error log. I think the problem would be the try/catch, but I simply cannot confirm this, because of my lack of knowledge regarding try/catches.

Question

How can I get the array to push and display all the errors with a try/catch block surrounding it? Or am I simply missing something and isn't the problem related to the try/catch?

This is my code:

$validateOk = 0;
$setVar = new SetVariable();
$teller = 1;
$save = null;
$validateOk = 0;
$con->beginTransaction();
$weg = TypeQuery::create()->find();
$weg->delete();
try {
 foreach ($_POST as $key => $value) {
 if (isEven($teller)) {
 $validateOk += $setVar->nameval($key)
 ->onerror($isActiefErr[]) //errors aren't being pushed into this array, besides the first one.
 ->validator(v::numericVal())
 ->go();
 if ($validateOk == 0) {
 $save->setCode(substr($key, 1));
 $save->setIsActief($value);
 $save->save();
 } else {
 //ROLLBACK/CATCH
 throw new Exception($error);
 }
 } else {
 $validateOk += $setVar->nameval($key)
 ->onerror($naamErr[])
 ->validator(v::alpha('/()éá&., '))
 ->go();
 if ($validateOk == 0) {
 $save = new Type();
 $save->setCode(substr($key, 1));
 $save->setNaam($value);
 } else {
 //ROLLBACK/CATCH
 throw new Exception($error);
 }
 }
 $teller += 1;
 }
 $con->commit();
} catch (Exception $e) {
 $error = "Het opslaan is fout gegaan!";
} {
 $con->rollback();
}
asked Feb 7, 2020 at 10:04
3
  • I'm not sure I understand, you say Hey, my array $isActiefErr and $naamErr doesn't fill up after I throw an exception but you've also said I know the foreach loop keeps on looping, because the array with values are all filled, surely that's a contradiction? Commented Feb 7, 2020 at 10:07
  • 1
    Correct, my bad. What I rather meant was: my array with values receives all the values, but the array which should receive the errors to later on output, stops after it encounters an error. It doesn't push the other 'not legal' values into the error array. I hope this made it a bit clearer :) Commented Feb 7, 2020 at 10:15
  • 1
    An exeption immediately terminates the try block. No loop will continue looping unless the try-catch construct is within the loop. Commented Feb 7, 2020 at 10:21

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.