0

Fatal error: Uncaught exception 'Exception' with message '

Error: The following fields have not been filled out- Last Name ' in /vagrant/web/Assignment4/Person.php on line 9

I am trying to check a form to make sure that all of the fields are filled in. If any of them are empty, I want to throw an error that says which fields are empty. I am having a hard time understanding how catching exceptions work, so could anyone tell me how they would fix this?

Person.php

 public function insert()
{

  //Storing required $_POST array fields in variable for isset function    

   $expectedValues = array(
       "firstName"   => "First Name",
       "lastName"    => "Last Name",
       "title"       => "Title",
       "office"      => "Office",
       "phoneNumber" => "Phone Number",
       "email"       => "Email",
       );


   //Checking to see if all fields are filled in 

    foreach ($expectedValues as $field => $humanName) { 
       if (empty($_POST[$field])) { 
        $errorArray[] = $humanName;
         foreach($errorArray as $print){
            throw new Exception("<p>" . "Error: The following fields have not been filled out- " . $print . "</p>");
         }

         try{
             (count($errorArray) = 0);
         }
         catch(Exception $e){
             echo "<p>" . $e->getMessage() . "</p>";
         }
       } 
     }


       //If they are, insert them into the Perosn table    

               $insert = $this-> doQuery("INSERT INTO Person 
                                         VALUES(
                                        '$_POST[firstName]',
                                        '$_POST[lastName]',
                                        '$_POST[title]',
                                        '$_POST[office]',
                                        '$_POST[phoneNumber]',
                                        '$_POST[email]')");

                                  $insert;


         //If insert query is successful, return true 
            if ($insert === true){
                return true; 
                echo "Congragulations! You now work for Assignment 4 Incorporated";
            }


         //If not, throw an exception    

            //else{
              //  throw new Exception
               // ("<p>" . "Error: Query was unsuccessful:" 
               // . " " . $this->error . "</p>");
               // }





   }

1 Answer 1

1

If you want to display errors to user then throwing exceptions is wrong way, try this:

foreach ($expectedValues as $field => $humanName) { 
   if (empty($_POST[$field])) { 
       $errorArray[] = $humanName;
   } 
 }
 if (count($errorArray) > 0) {
      echo 'Following fields are empty: '.implode(' ', $errorArray);  
 }

Also for the fun, check out HTML5 property required:

<input type="text" required="required" value="" />
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't it be (count($errorArray) > 0) though? isn't = 0 saying that if there is nothing in the error array to print it out?

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.