views:

2927

answers:

7

I am in the process of creating an input form on a record specific page e.g. (property/rentals/rec_id). On submission, the data should be inserted, validated and return to the original page from which it was sent with any relevant messages.

  • If the validation fails, reload the form page a re-populate the form
  • Echo the specific validation error - e.g. invalid email address etc

  • If successful, insert data, reload the page and display success message.

    function class() {

    $this->load->library('form_validation'); $this->form_validation->set_rules('name','name','required|trim'); $this->form_validation->set_rules('email','email','required|trim|valid_email');

    $fields['name'] = 'Name';
    $fields['email'] = 'Email Address';
    
    
    $this->validation->set_fields($fields);
    
    
    if ($this->form_validation->run() == FALSE) 
    {
     //failed - reload page and re-populate fields with any error messages
    }
    else
    {
         // display form with success message
    }
    

    }

Normally this is simply done by 'loading a view' and passing the relevant data to repopulate the page, fields or display messages. But I have to use a 'redirect' back to the product specific page. Is there any good way to do this other than using session data to carry the validation errors..?

Thanks in advance, Michael

+1  A: 

Session data would be the simplest and most accurate/efficient way to send the data back to the form. You could use cookies as well, but it would be less secure (since cookie data is sent with every http request).

Your other option would be to store the details in the database and fetch them again after the redirect, but I feel like that would be adding unneeded complexity to your application.

For a really simple solution you could even just dump your POST/GET data into a SESSION variable then call it up quickly on the page being redirected to, almost making the redirect transparent from a logic point of view.

session_start();
$_SESSION['POST_DATA'] = $_POST;
tj111
Umm, that's what I thought. It's still a pretty round about way, but probably the simplest solution nonetheless. It would be handy if an array could be passed through a redirect...thanks tj111
Michael Bradley
A: 

To alleviate the issue you could just do client side validation using Javascript/jQuery. So, basically, instead of immediately going to the new page, the Submit action would just get redirected to your javascript function, which would decide if the form is valid. Then you could use jQuery to make things look pretty for invalid elements.

Peter
The problem with this is that client-side validation is easily by-passable. You should *always* validate data on the server, even if you pre-validate it on the client. Never trust data you don't control.
tj111
A: 

you can pass the variables along with the redirect as in a GET type request

eg

http:://www.example.co.nz/myform/?formerrors=missing fields

downside in the url will look a little ugly

i am not quite sure why you are adverse to storing the details in the session, as it is probably simpler and easier todo then this

bumperbox
Unless specifically configured not to, CodeIgniter will blast the $_GET variable. Something to keep in mind. http://codeigniter.com/user_guide/general/urls.html
GloryFish
+1  A: 

Take a look at the CodeIgniter manual on Sessions and see if 'FlashData' is of any use.

amr
Flashdata is a great suggestion. Easy to use, clean code. http://codeigniter.com/user_guide/libraries/sessions.html
GloryFish
+2  A: 

You may use the set_value('field_name') function to re-populate the form. Sample usage in view:

<input type="text name="name" value="<?php echo set_value('name'); ?>" />"

To display the errors from the form_validation, you may use the validation_errors() function. Sample usage in view:

<?php echo validation_errors(); ?>

Also, see link text for more info.

Randell
I think this is actually the most CI way of doing it but for some reason everyone, including me, always ends up using session methods.
stef
IMO, using the session methods end up with clutter in the code.
Randell
A: 

Hello,

See weakness of Codeigniter Form Validation library here.

Tahsin Hasan
A: 

and also see how you can solve this here

Tahsin Hasan