Welcome Guest, Not a member yet? Register   Sign In
How to preset my form fields and use the Validation class
#1

[eluser]TWP Marketing[/eluser]
[edit]
Ok, let me simplify the question.
How do I pass field values to a form and use the validation library.

view:
Code:
...
<input type='text' name='name' value="<?php echo set_value('name','default name'); ?>" />
...
This will only set a default value BEFORE the form is submitted.
I've tried to pass my field value from the db:
Code:
...
<input type='text' name='name' value="<?php echo set_value('name',$current_name); ?>" />
...
This fails to display, although I can see the value; $current_name IS available in the view.

I've been searching the forum, but find no discussion about the most recent version of the form_validation library. CI 2.0.1
Help?
#2

[eluser]TWP Marketing[/eluser]
[bump] I've simplified the question, but have had no success finding an answer...
#3

[eluser]InsiteFX[/eluser]
A simple way to do this would be to use session user_data
Code:
<input id="name" type="text" name="name" maxlength="255" value="<?php echo set_value('name', $this->session->userdata('name_value')); ?>" />

InsiteFX
#4

[eluser]TWP Marketing[/eluser]
Insitefx, I see what you suggest, but don't see any material difference between a session var and a var passed to the view from the controller. Note that I am trying to implement the form_validation class:

controller:
Code:
...
$view_data['contact']['name'] = 'Charley Smith';
$this->load->library('form_validation');

$this->form_validation->set_rules('name','Name or Company','trim|required|xss_clean');

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

   $this->load->view('contact_entry_form', $view_data);

}else{

   $view_data['advisory_msg'] = 'Thank you! Your Message has been received';

   $this->load->view('advisory', $view_data);

}
...
view:
Code:
...
<?php
$attributes = array(
'type' => 'text',
'name' => 'name',
'size' => '50',
'value' => set_value('name', $contact['name'])
);
echo form_input($attibutes);
?>
This works and dislays 'Charley Smith', telling me that the value of contact['name'] does exist.
'Name: <input type="text" name="name" size="50" value="<?php echo $contact['name'];?>" />
...
The form helper (form_input()) outputs valid html but does NOT place the name 'Charley Smith' in the field.

So, How do I pass or preset the content of the form_validation field 'name'? Prior to CI 2, we had a function: set_field('name','some_name'), but that function is gone now, What replaces it?
#5

[eluser]InsiteFX[/eluser]
Try this.
When passing view data you use like $name not $view_data['name']

Controller:
Code:
...
$view_data['name'] = 'Charley Smith';
$this->load->vars($view_data);

$this->load->library('form_validation');

$this->form_validation->set_rules('name','Name or Company','trim|required|xss_clean');

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

   $this->load->view('contact_entry_form');

}else{

   $view_data['advisory_msg'] = 'Thank you! Your Message has been received';
   $this->load->vars($view_data);

   $this->load->view('advisory');

}
...

View:
Code:
...
<?php
$attributes = array(
'type' => 'text',
'name' => 'name',
'size' => '50',
'value' => set_value('name', $name)
);
echo form_input($attibutes);
?>
This works and dislays 'Charley Smith', telling me that the value of contact['name'] does exist.
'Name: <input type="text" name="name" size="50" value="<?php echo $contact['name'];?>" />
...

InsiteFX
#6

[eluser]TWP Marketing[/eluser]
No luck, The form field displays blank, but the var: $name, does contain 'Charley Smith'.
The form validation function: set_value('name',$name') does not pass the default value in the second parameter.
Code:
This works:
<input type="text" name="name" size="50" value="<?php echo $contact['name'];?>" />

These do not:
<?php
$attributes = array(
'type' => 'text',
'name' => 'name',
'size' => '50',
'value' => set_value('name', $name)
);
echo form_input($attributes)."\n";
...
<input type="text" name="name" size="50" value="<?php echo set_value('name', $name); ?>" />
?>
#7

[eluser]toopay[/eluser]
try set array variable directly in your controller...
#8

[eluser]TWP Marketing[/eluser]
toopay, which array variable are you referring to?
#9

[eluser]InsiteFX[/eluser]
Try this
Code:
<input type="text" name="name" value="<?php echo set_value('name', $name);?>" size="50" />

InsiteFX
#10

[eluser]toopay[/eluser]
try send array var directly from your controller, like this...
Code:
// in your controller
....
$data['input_name'] = array(
'type' => 'text',
'name' => 'name',
'size' => '50',
'value' => set_value('name', $your_vars)
);
$this->load->view('your_form_view',$data);
....

// In your view
....
echo form_input($input_name)."\n";




Theme © iAndrew 2016 - Forum software by © MyBB