Welcome Guest, Not a member yet? Register   Sign In
Form validation with redirect
#10

I have a view which looks something like this:

PHP Code:
<h1>Test Page</h1>
<
p>Lorem ipsum etc.</p>
<
h2 id='my_form'>Form</h2>
<?
php if (! empty($errorMessage)) : ?>
<div class='alert alert-error'>
    <h4 class='alert-heading'><?php echo lang('test_model_error_heading'); ?></h4>
    <?php echo $errorMessage?>
</div>
<?php
endif;
if (
validation_errors()) :
?>
<div class='alert alert-error'>
    <h4 class='alert-heading'><?php echo lang('test_validation_errors_heading'); ?></h4>
    <?php echo validation_errors(); ?>
</div>
<?php 
endif;
echo 
form_open(site_url('myform/home') . '#my_form''class="form-class"'); 
?>
    <fieldset>
         <?php 
        
// form fields here
 
       ?>
    </fieldset>
    <?php if ($canEdit) : ?>
    <fieldset class='form-actions'>
        <input type='submit' name='submit' value="<?php echo lang('test_edit_button'); ?>" class='edit-button' />
    </fieldset>
    <?php endif; ?>
<?php 
echo form_close(); ?>

Note that '#my_form' is appended to the URL in form_open(), which causes the page to return to the form if I don't redirect in the controller, without the need for JavaScript. Also note that validation_errors() returns an empty string if there are no validation errors, so I only need to set additional variables for error handling if I'm dealing with error messages generated by another part of the system, like my model (if validation didn't catch something).


Then the controller does something like this:

PHP Code:
class Myform extends CI_Controller
{
 
   // ...

 
   public function home()
 
   {
 
       $data = array();

 
       // $this->canEdit is a stand-in for checking whether the user 
 
       // is authorized to submit this form.
 
       if ($this->input->post('submit') && $this->canEdit) {
 
           $result $this->handleForm();
 
           if ($result) {
 
               // I redirect on success, when appropriate, in which case 
 
               // I usually omit the else clause and handle the errors 
 
               // without the additional nesting.
 
           } else {
 
               if (isset($this->errorMessage)) {
 
                   $data['errorMessage'] = "Failed to submit form: {$this->errorMessage}";
 
               }
 
           }
 
       }
 
       
        
// Setup the page to view
 
       // ...

 
       $this->load->view('myform/home'$data);
 
   }

 
   protected function handleForm()
 
   {
 
       // get post data
 
       // ...

 
       // validate form
 
       if ($this_>form_validation->run() === false) {
 
           return false;
 
       }

 
       // insert/update data
 
       // ...
 
       if (! $result) {
 
           $this->errorMessage $this->myform_model->errorMessage;
 
           return false;
 
       }

 
       return $result;
 
   }


The method which displays the form simply checks whether the form was posted, and, if it was, it calls another method in the controller to process the form. That method returns false if there was an error, or it can return true (or possibly an ID on a successful insert) if everything is OK. I usually redirect if the form was submitted successfully, because I can send the user to a thank you page or back to a list page, but you can do whatever you like on success. If there's an error (validation or otherwise), it simply sets an error message if one is available, then continues loading the page the same way it would if the user was visiting for the first time.
Reply


Messages In This Thread
Form validation with redirect - by msheath - 10-26-2015, 04:42 AM
RE: Form validation with redirect - by Martin7483 - 10-26-2015, 05:34 AM
RE: Form validation with redirect - by msheath - 10-26-2015, 07:26 AM
RE: Form validation with redirect - by Martin7483 - 10-26-2015, 07:44 AM
RE: Form validation with redirect - by msheath - 10-26-2015, 08:09 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:39 AM
RE: Form validation with redirect - by Narf - 10-27-2015, 03:42 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:54 AM
RE: Form validation with redirect - by Narf - 10-28-2015, 04:21 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 04:50 AM
RE: Form validation with redirect - by Martin7483 - 10-27-2015, 12:16 PM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:15 AM
RE: Form validation with redirect - by mwhitney - 10-27-2015, 12:29 PM
RE: Form validation with redirect - by Martin7483 - 10-28-2015, 02:41 AM
RE: Form validation with redirect - by Martin7483 - 10-28-2015, 05:00 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 05:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB