Welcome Guest, Not a member yet? Register   Sign In
new validation library autocomplete field
#1

[eluser]Bogdan Tanase[/eluser]
Hi guys,

I'm trying to write a form using the new validation library, and I'm a little confused on how I should implement an "edit" feature. I use the same view for add and edit and two functions in my controller: add and edit.

In the old library in the edit function I was using the following code to autocomplete edited fields when edit function was first called:

Code:
// the validation rules and fields are already set
// now we need to retrieve the existing data from DB

$current_item=$this->my_table->get_one($item_id); // a model function that returns current item
                                                  // details

$this->validation->name=$current_item->name;
$this->validation->age= $current_item->age;

// ... do rest of stuff

in my view I had something like this:

Code:
<?=form_input('name', $this->validation->name)?>
<?=form_input('age', $this->validation->age)?>

<!-- ... -->

now, I understand that, with the new library, in the view I set the values using set_value, but how do I set them in the controller, to be used then by the set_value function?

Thanks,
Bogdan
#2

[eluser]Bogdan Tanase[/eluser]
Later edit: I looked again. this does soemthing else Sad

After looking through the form_validation.php file it looks like the member function set_value should do the trick:

Code:
$this->form_validation->set_value('name', $current_item->name);

However, this doesn't work. What am I missing?
#3

[eluser]Volder[/eluser]
hello, have you found the solution to your problem?

Because I'm facing the same problem.

In previous version of CI 1.6.3 I used in my controller conditions like:

Code:
If ($this->validation->email)
{
   //...check in DB whether such an email is already existing one
}

but how to get the value from the input form to use inside controller now with 1.7.0?
set_value('email') is not working.
even simple
Code:
echo set_value('email');
inside controller is not producing any output, while the email is properly submitted in the form Sad(
#4

[eluser]Bogdan Tanase[/eluser]
Hi,

Regarding the problem in my initial post, in the new version you can no longer set the validation fields. I had to use another approach, by passing the variables in the edit function (controller) to the view, and then display it using set_value:

Code:
//controller
$data['name']=$name; //db value;

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


view code

Code:
//...
<?=form_input('name',set_value('name',$name))?>


But your problem is different. The code you posted should work.

Did you set the rules as described in the manual?

Code:
$this->form_validation->set_rules('email', 'Your email', 'required|valid_email');

//[...]

if ($this->form_validation->run())
{
  echo set_value('email');
}
#5

[eluser]Volder[/eluser]
Yes, I did it in a proper way.

BTW I found a thread with a question similar to mine.

The suggestion there was to use $this->input->post('field_name') instead.

It worked for me.
#6

[eluser]MeanStudios[/eluser]
This will not work:
Code:
echo set_value('email');
This will work:
Code:
echo $this->form_validation->set_value('email');




Theme © iAndrew 2016 - Forum software by © MyBB