Welcome Guest, Not a member yet? Register   Sign In
Confused how form dropdown is being re-populated
#1

[eluser]eger[/eluser]
I am trying out the new form validation in 1.7 and am having a hard time grasping something. I am using the form helper to create some dropdowns. The data for these dropdowns is an array generated by my SQL data. I couldn't figure out how to re-populate a dropdown when using the form helper. However it seems to be repopulating automatically and I am not sure why as I can't find any information about it in the manual.

In my controller I use only:
Code:
$view_data['field_host'] = form_dropdown('host', $options_host);

I have a rule that says 'host' is only required. When I submit the form with another field that has an error, it correctly selects the dropdown item. Yet I have no set_input or set_select.

Is the form helper automatically re-populating dropdowns? I would like to know why this is working when I did not setup any re-populating.. very confusing.

EDIT:

I also seem to be having an issue with an input also:

Code:
$view_data['field_port'] = form_input('port', set_value('port', 80));

This displays '80' for the value of the input field when loading the form. But it does not repopulate after submitting with an error. I have also tried using $this->form_validation->set_value('port', 80). I did read that these functions were for use in view files. I am doing this all in the controller. Will all this ONLY work in the views?
#2

[eluser]Sumon[/eluser]
[quote author="eger" date="1226464305"]I did read that these functions were for use in view files. I am doing this all in the controller. Will all this ONLY work in the views?[/quote]
you can call helper function from anywhere(view or controller).
for me i use text boxes as
Code:
//$default['email'] = any value. i set it by form_validation
<input type="text" name="email" value="<?php echo set_value('email', $default['email']); ?>" size="50" />
whereas
text input works fine with repopulating. but about checkbox and select i am really hopeless. i also need a good example.
#3

[eluser]eger[/eluser]
From the view this code seems to work
Code:
form_input('port', set_value('port', '80'));
It repopulates the field and sets it to '80'. But when I use the same in the controller and send it to the view as a variable it does not repopulate. Though it does set the field to 80 on first load.

What is more puzzling is that the dropdowns I pass to the view as a variable are auto populating and I am not even trying any set_value or set_dropdown... Confused about this one also.
#4

[eluser]Sumon[/eluser]
would you please post your code of view and controller (only input box portion)
#5

[eluser]eger[/eluser]
Sure, here is my method in the controller
Code:
function request($trackId = '')
    {
        $playlist = $this->playlist->get();
        $playlisResult = $playlist->result();
        
        foreach ($playlisResult as $track) {
            $optionsTrack[$track->ID] = "$track->artist - $track->title";
        }

        $this->form_validation->set_rules('ID', 'Track ID', 'trim|required|integer');
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('error', 'Cause Error', 'trim|required');
        
        $view['form_open'] = form_open('playlist/request');
        $view['input_track'] = form_dropdown('ID', $optionsTrack);
        $view['input_name'] = form_input('name', set_value('name', 'Your Name'));
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('reqform', $view);
        }
        else
        {
            $this->load->view('reqform_success');
        }
    }

Here is the reqform.php view
Code:
<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?= $form_open ?>

<h5>Track</h5>
&lt;?= $input_track ?&gt;

<h5>Name</h5>
&lt;?= $input_name ?&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

In this example, the error field always fails (cause it does not exist). It keeps the correct selected track field from the dropdown on error and displays 'Your Name' in the name field on first load.

- The name field is populated with 'Your Name' on load (expected)
- The name field is not repopulated on error (not expected)
- The track dropdown IS repopulated on error (not expected)

Hope this makes some sense. Not sure why this happens.
#6

[eluser]Sumon[/eluser]
give it a try
Code:
if ($this->form_validation->run() == FALSE)
{
   $view = $_POST;
   $this->load->view('reqform', $view);
}




Theme © iAndrew 2016 - Forum software by © MyBB