• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prepopulating form data

#1
[eluser]macleodjb[/eluser]
This is probably a lame question but i want to make sure that i do this correctly with code igniter. I want to pre-populate form data using data from my sql database. I have validation rules set up inside my controller and it will populate the fields when the validation is triggered. How can i intermingle the two without having a duplicate string placed in the value of a field?

#2
[eluser]TheFuzzy0ne[/eluser]
This is how I usually do it:
Code:
class Homepage extends Controller {

...

function add_something()
{

    // We need the session class for flashdata and url helper
    // for redirects.
    $this->load->library('session');
    $this->load->helper('url');

    // Load the validation class. This will allow you to use
    // set_value() in your view. Use the second parameter
    // of set_value() to populate the form with the data
    // from the database for the first time.
    $this->load->library('form_validation');

    // See if the form is being submitted
    if ($this->input->post('submit'))
    {
        // Run the validation (set up the rules first)
        if ($this->form_validation->run() === TRUE)
        {
            // set a flashdata message
            $this->session->set_flashdata('message', 'Entry successfully added!');
            
            // Redirect to another page
            redirect('/some_controller');
        }
    }
    // Set an array to feed into the view
    $data['form_data'] = $this->some_model->getFormData();

    // Load the default view
    $this->load->view('default_view', $data);
}

}

Basically, the controller will keep loading so long as the validation fails. When the validation is successful, the user is redirected, which prevents them accidentally resubmitting the form when using the back button, or refreshing a page.

Ceveat: This method relies on cookies to keep the user posted, although it's possible to do it without the need for cookies easily enough.


I hope this helps.

#3
[eluser]ajcolburn[/eluser]
This method is great for prepopulating an update record form. I have tried to modify it to allow for the creation of new records, but the system throws a PHP error if the set_value() default variable is not defined. The variable is not defined because if a record for the given ID is not returned by the model then my controller assumes a new record is being created. Basically I'm trying to write the simplest/shortest bit of code to add and edit records with one controller and one view.

Any help would be much appreciated...

Regards,
Alex

#4
[eluser]TheFuzzy0ne[/eluser]
If we could see your code it would really help.

#5
[eluser]JayTee[/eluser]
[quote author="ajcolburn" date="1236055161"]Basically I'm trying to write the simplest/shortest bit of code to add and edit records with one controller and one view.

Any help would be much appreciated...

Regards,
Alex[/quote]
I've had issues with this since I started using CI - but there's just no "easy" way to have a form that can be used for both adding AND editing. I just use 2 forms; 1 for adding, the other for editing.

I wish I could be more help. The nice thing is how much quicker/cleaner the CI 1.7 code is for creating these forms.

#6
[eluser]TheFuzzy0ne[/eluser]
Here's some untested HTML which might give you some inspiration, but as JayTee said, it's not really an "easy" way:
Code:
<html>
    <head>
        <title><?php echo (isset($id)) ? "Edit" : "Add" ; ?>User</title>
    </head>
    <body>
        <form action="/user/<?php echo ($id) ? 'edit' : 'add'; ?>" method="post">
            <input type="type" name="username" value="<?php echo (isset($username)) ? $username : ''; ?>" />
            <?php if (isset($id)): ?>
            <input type="hidden" name="id" value="<?php echo $id; ?>" />
            <?php endif; ?>
        </form>
    </body>
</html>

Basically, the rule is, if an id is specified, then the user obviously exists, therefore we must be editing. If not, we're adding. Simple, huh?

#7
[eluser]The Wizard[/eluser]
I've made a tutorial on this topic, search for 6 minutes tutorial and you shall find what you are looking for.


(do i sound like a dungeon master?Smile)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.