Welcome Guest, Not a member yet? Register   Sign In
Re-populating form and hack
#1

[eluser]dhenoer[/eluser]
Re-populating the form after form_validation returning false, make our application seems more smart, because all value are restored again, and ready to fix.

The problem arise when we 'edit' a set of data, so the input values are gotten from database. I mean how to display their initial value. I found a simple method that I think a hack to inject initial data.

In the controller, I create a method like this:

Code:
function edit($id){
    $this->load->model('testmodel');
    $this->load->library('form_validation');
    $this->load->helper('form');

    if ($this->input->post('submit')) {

        $this->form_validation->set_rules('ID','Identity','required');
        $this->form_validation->set_rules('Name','Name','required|min_length[5]');

        if ($this->form_validation->run()){
            $data['propID'] = $this->input->post('ID');
            $data['propName'] = $this->input->post('Name');
            $this->testmodel->update($data, $id);
    
            die('success!');
        }
    
    } else {
        $data = $this->testmodel->getByID($id);
    }

    $this->load->view('test/edit', @$data); //i have to use @ to hide notice warning
}

and code of the view as follows:
Code:
<h1>Edit</h1>

&lt;?= form_open($this->uri->uri_string()) ?&gt;

Id: &lt;?= form_input('ID', set_value('ID', @$propID)) ?&gt;<br/>
Name: &lt;?= form_input('Name', set_value('Name', @$propName)) ?&gt;<br/>
&lt;?= form_submit('submit', 'Submit!') ?&gt;
&lt;?= form_close()?&gt;

What we do is: prepend '@' before $data to send to view from controller.
And prepend '@' before each fieldnames as default value in your view.
This method hides warning notice at that line, but your application still runs.
#2

[eluser]Flemming[/eluser]
:zip:
#3

[eluser]dhenoer[/eluser]
yes, set_value() help me to set default as initial value.
but when we submit the form, and if form_validation returns false, we back to views again. then to re-populate the form, 2nd parameter of set_value() actually doesn't be needed anymore. so, because of 2nd paramater doesn't be set, set_value() will show notice warning...
#4

[eluser]Flemming[/eluser]
I think this post may help you (look at the example in post#6 for how to structure your controller)

http://ellislab.com/forums/viewthread/135367/
#5

[eluser]dhenoer[/eluser]
Thanks Flemming,
I have read your reference. But I think my structure is similar to yours. The difference is your method only receives submitted form, and mine handles both displaying initial value and submitted. I've tried to restructure like yours, as here:

Code:
//display inital value
    function edit($id) {
        $data = $this->testmodel->getByID($id);
        $this->load->view('test/edit', $data);
    }

    //handle submitted form
    function handleSubmit()
    {
        $this->form_validation->set_rules('ID','Identity','required');
        $this->form_validation->set_rules('Name','Name','required|min_length[5]');

        if ($this->form_validation->run()){
            //save
            $data['propID'] = $this->input->post('ID');
            $data['propName'] = $this->input->post('Name');
            $this->testmodel->update($data, $id);
            //success!
            redirect('to_other_page');
        }
        else
        {
            //validation failed
            //I do not pass $data, no needed anymore, because set_value should re-populate them
            $this->load->view('test/edit');
        }
    }

Remember my view still contains set_value('inputName', $defaultValueComesFromDatabase). So variable $defaultValueComesFromDatabase is not set when the view loaded by handleSubmit method. Of course I could rewrite handleSubmit like the following to make my view runs without warning:
Code:
function handleSubmit()
    {
      //form_validation->set_rules goes here..

      if ($this->form_validation->run()){

            //save and then redirect to another page

      }
      else {

        //validation failed
        //makes array for each field I used, to be passed to the view
        $data['propID']   = '';
        $data['propName'] = '';

        $this->load->view('test/edit', $data);
      }

This method will re-populate variables in the view manually, so set_value function seems useless... Sad

I've learnt Ajax, but I'm not sure if validation process in javascript could contains large & complex rules generated dynamically come from database..
#6

[eluser]Flemming[/eluser]
Hi, just a very quick reply (sorry) ... I havent read all of your code in detail but WHY are you trying to use 2 different methods? You don't need a separate method for 'edit' and 'handle submit'

My simple example will do both. All in ONE method!
#7

[eluser]dhenoer[/eluser]
Oh, I see now. You are right and thank you..
I'v tested, that we do not need if ($this->input->post('submit')) to detect if the submitted data are coming.. Hmm.. In my mind before, we have to protect validation process only to be done when submitted exist..

Ok, but my question have still unsolved yet. I revised the code..
Code:
function edit($id){
  
        $this->form_validation->set_rules('ID','Identity','required');
        $this->form_validation->set_rules('Name','Name','required|min_length[5]');

        if ($this->form_validation->run()){
            $d['propID'] = $this->input->post('ID');
            $d['propName'] = $this->input->post('Name');
            $this->testmodel->update($d, $id);
            //success!;
            redirect('to_another_page');
        }

        //$data????
        $this->load->view('test/edit', $data);
}

The question is, how to get $data? $data is needed to become initial value of my form.
If $data is gotten from database, they will show again when the validation runs fail. The form would be repopulated from database again, NOT the last input by users...
#8

[eluser]Flemming[/eluser]
Quote:The question is, how to get $data? $data is needed to become initial value of my form.
If $data is gotten from database, they will show again when the validation runs fail. The form would be repopulated from database again, NOT the last input by users…

You get your $data from the database and use it as the second parameter in set_value() - this is the initial/default value but it is over-written if the form is submitted ... with ... the $post data!

:-)
#9

[eluser]dhenoer[/eluser]
Oh no :red: ....
You are right. My problem solved! Thank you..
Actually set_value() displays the last input. When the last input undefined, $data is shown...




Theme © iAndrew 2016 - Forum software by © MyBB