Question

Typically, the common way to handle an error is to simple do the redirect like so:

if ({something that is error}) {
    $this->Session->setFlash(__('Error message'), true);
    $this->redirect(array('controller' => 'some_controller', 'action' => 'some_action'));
}

However, I there are multiple checks that occur during the if ($this->request->is('post')) { section of the method. If any of the checks fail, I want to exit the rest of the checks, return the user to the form with all of the settings they entered previously so they do not have to fill out the forms again. How would I accomplish this?

public function custom_method() {
    // get some information here

   if ($this->request->is('post')) {
       // do_check
       if (!do_check) {
          // set flash
          // log error
          // don't run the rest of the checks - go to end of the if ($this->request->is('post'))
       }

       // do other check
      if (!do_other_check) {
          // set flash
          // log error
          // don't run the rest of the checks - go to end of the if ($this->request->is('post'))
      }

      // do database update
   }

   // do other stuff here
   // then it goes to render view 
}
Was it helpful?

Solution

I worked out a great solution to this after talking to a good friend of mine (thanks @utoxin)! He helped push me in the right direction with his mention of model validation.

Here is how I will accomplish this:

public function prepare() {

   // do some stuff here

   if ($this->request->is('post')) {
      $postValid = true;


      if ($postValid and !some_check) {
    $this->log('Log error here', 'error');
    $this->Model->invalidate('form_field', 'No valid installation file was found for this version.');
    $postValid = false;
      }

      if($postValid){
        // no error, do redirect, continue successfully
      }
   }

   // do other stuff here
   // show form
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top