Welcome Guest, Not a member yet? Register   Sign In
Problems with form validation/form helper and re-populate
#1

[eluser]albertone01[/eluser]
Hello this is my controller:

Code:
function index()
{
$this->load->helper('form');    
$this->load->library('form_validation');    
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('surname', 'Surname', 'required');

if(empty($_REQUEST['submit'])){
$data['error'] = '';

$data['def_name']        = 'Example';
$data['def_surname']    = '';

$this->load->view('myview', $data);        

} else {

   if ($this->form_validation->run() == FALSE){
      $this->load->view('myview');        
   } else {
      echo 'ok';
   }                    
}                                    

}

and this is my view:

Code:
<?php echo form_open('/Myview/index/'); ?>
<?php echo form_input('name', set_value('name', $def_name)); ?>
<?php echo form_input('surname', set_value('surname', $def_surname)); ?>
<?php echo form_submit('submit', 'Send'); ?>
...

The first time it's all right, i can see correctly the values "Example" in the name input.
If I submit the form i get this error over all my input form:
Code:
Severity: Notice
Message: Undefined variable: def_surname

why??? help Sad
#2

[eluser]Dam1an[/eluser]
You need to pass the $data rray into the view the second time as well
like this
Code:
if ($this->form_validation->run() == FALSE){
      $this->load->view('myview', $data);        
   } else {
      echo 'ok';
   }
#3

[eluser]albertone01[/eluser]
But in this way i have to put all post data into $data var...
I would to repopulate the form in this way:
http://ellislab.com/codeigniter/user-gui...latingform

...
#4

[eluser]Dam1an[/eluser]
Right, now I'm with you (not thinking straight today)

Even if it doesn't need the second value, it still looks for it, changing the view to this fixes the problem
Code:
<?php echo form_open('test/index'); ?>
<?php echo form_input('name', set_value('name', isset($def_name) ? $def_name : 'nothing')); ?>
<?php echo form_input('surname', set_value('surname', isset($def_surname) ? $def_surname : 'nothing')); ?>
<?php echo form_submit('submit', 'Send'); ?>

another solution would be to always initialise these to nothing in the controller and pass them in
#5

[eluser]albertone01[/eluser]
Ok, now i don't have the error ....
But I have not the posted value, I have 'noting' or '' ...


Tongue
#6

[eluser]Dam1an[/eluser]
You're just one problem after another Tongue
Out of curiosity, why do you need to set the default as a variable? Won't it always be the same? So it could be hard coded
#7

[eluser]albertone01[/eluser]
'Couse I need to purpose to users some data, but they are free to change them...

Ex.
The user open the form:
- name*: ''
- surname*: ''
- date*: 25/05/2009

Change:
- name* = 'Alberto'
- surname*: ''
- date*: 28/06/2009

I check that che surname is empty

Return to the form with the new values...

Smile




Theme © iAndrew 2016 - Forum software by © MyBB