Welcome Guest, Not a member yet? Register   Sign In
Using the validation class with itihe form helper
#1

[eluser]phester[/eluser]
Hello All,
I am trying to figure out how to use the validation class to repopulate a select menu generated with the form helper.

Using the validation class with the form class for most input fields is simple:
Code:
form_input(array('name' => 'email', 'value' => $this->validation->email));

However, it's not so simple for a select menu.

This is what the user guide for the validation class has for pre-setting the select menu:
Code:
//From the validation class
<select name="myselect">
<option value="one" &lt;?= $this->validation->set_select('myselect', 'one'); ?&gt; >One</option>
<option value="two" &lt;?= $this->validation->set_select('myselect', 'two'); ?&gt; >Three</option>
<option value="three" &lt;?= $this->validation->set_select('myselect', 'three'); ?&gt; >Three</option>
</select>

And here is what the user guide for the form helper has for pre-setting the select menu:
Code:
//From the form helper
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

echo form_dropdown('shirts', $options, 'large');  //This would select "large"

I will need to know which "value" to select to pass it to the form helper. Any ideas on how to work around that? I hope I described this situation clearly. Any help is greatly appreciated;
#2

[eluser]Armchair Samurai[/eluser]
Code:
echo form_dropdown('foo', $bar, $this->validation->foo);
#3

[eluser]smith[/eluser]
@座頭市2:
Unfortunately it doesn't work that way whit select and $_POST['field'] array Sad I am having the same problem myself, will post working example so that we can talk about it and maybe find a solution Sad
this problem has stopped me from development and i am desperet in finding the working solution Sad
#4

[eluser]Armchair Samurai[/eluser]
Actually, works perfectly... I'm using this very solution on a variety of sites with no issues.

There is nothing special about a single element selected from select field - it posts its data like any other form field. Try doing a var_dump or print_r on submitted $_POST data - it looks like everything else.
#5

[eluser]smith[/eluser]
here is an example of my problem. it is attached, together with mysql test tables dumps.

http://rapidshare.com/files/62239050/test.rar.html

$this->validation->test_array is not visible to view when editing Sad
updated set_select is inside MY_Validation.php (updated set_select can work with _POST arrays)

form is populated when using insert (your_host/test_post/test_insert)
but form is NOT correctly populated when editing, even though all the variables exist inside a model

i am using print_r at the end of model script so that you can see that variables exist but inside view they are not visible. Please, if anyone wants to test, i will be very grateful...

Thank you

P.S. I was extracting files from my existing project, so please excuse me for using too many view files...
#6

[eluser]phester[/eluser]
[quote author="座頭市2" date="1192279642"]Actually, works perfectly... I'm using this very solution on a variety of sites with no issues.

There is nothing special about a single element selected from select field - it posts its data like any other form field. Try doing a var_dump or print_r on submitted $_POST data - it looks like everything else.[/quote]

Can you give us a code sample of how you do that? Thanks,
#7

[eluser]Armchair Samurai[/eluser]
@phester
Code:
/* IN THE CONTROLLER */

function generic_validation()
{
    $this->load->library('validation');
    $this->load->helper('form');

    $rules['test'] = 'required|numeric';
    
    $fields['test'] = 'Dropdown';

    $this->validation->set_rules($rules);
    $this->validation->set_fields($fields);

    if (!$this->validation->run()) {
          $this->load->view('form');
    } else {
         /* PROCESS THE RESULTS AS NEEDED */
    }
}
Code:
&lt;!-- IN THE VIEW --&gt;

&lt;?php

echo form_open('your_controller_name/generic_validation');

echo $this->validation->error_string;

$arr = array(
     'null' => 'Please choose a number',
     '1' => 'One',
     '2' => 'Two',
     '3' => 'Three',
     '4' => 'Four',
     '5' => 'Five'
);

echo form_dropdown('test', $arr, $this->validation->test);

echo form_close();

?&gt;

@Smith
I'm kind of pressed for time at the moment, so I didn't have a chance to take a good look at things, but I noticed that in test_post.php, while you're loading the validation library, you're not actually calling the validation functions anywhere (i.e. setting the rules or $this->validation->run()). Did I miss something?
#8

[eluser]smith[/eluser]
Not a problem, thank you for your time Smile

$this->validation->run() is inside a form model, entire validation is inside a model, any additional callback is inside MY_Validation.php

so, in your controller all you have to do is call a form: $this->data['form'] = $this->form_model->form_name.
#9

[eluser]patbert[/eluser]
I have a related question.

The form I have created has values already filled in (call it $value) when it is first displayed. When the validation class fails the submission and I redisplay the form I have to do a check to see if $this->validation->value is set and if so make $value equal to it.

There are a lot of form elements so I have to write dozens of little if statements to check them all. Is there a better way to do this that I am not thinking of?
#10

[eluser]smith[/eluser]
use hidden field value inside your form, name it for example is_posted.
is_posted is set only if form is posted, so you can display $this->validation->value from your database or from $_POST depending on is_posted value.




Theme © iAndrew 2016 - Forum software by © MyBB