Welcome Guest, Not a member yet? Register   Sign In
Form repopulate on error and edit mode
#1

[eluser]John5788[/eluser]
Hello, I just updated to CI 2.0.2 and I am running into some problems.

My form uses the form helpers, so I have a bunch of code that looks like this

Code:
<?php echo form_input("x", set_value("x", $input_x)); ?>
<?php echo form_input("y", set_value("y", $input_y)); ?>
<?php echo form_input("y", set_value("z", $input_z)); ?>

I am using the first parameter of set_value() for form validation error. The second parameter is default value on edit mode. I found this technique on the forum a long time ago.

I am trying to share the same form view so I don't have to duplicate code and this method worked well until 2.0.2. PHP ERROR notices are thrown because $input_x does not exist until I call the controller function in charge of populating $input_x, $input_y, $input_z. I am not sure why it didn't happen in the past, but I am curious how to get around this?

I know I can just add an if statement such as

Code:
<?php
if(isset($input_x))
{
    echo form_input("x", $input_x);
}
else
{
    echo form_input("x", set_value("x"));
}
?>

but it just looks too verbose. Any other approaches?

TLDR: I need some way to share same form view on add and edit. Solutions?
#2

[eluser]InsiteFX[/eluser]
Did you try this?
Code:
class some_class extends CI_Controller {

    public $input_x;
    public $input_y;
    public $input_z;

}

InsiteFX
#3

[eluser]John5788[/eluser]
[quote author="InsiteFX" date="1303905978"]Did you try this?
Code:
class some_class extends CI_Controller {

    public $input_x;
    public $input_y;
    public $input_z;

}

InsiteFX[/quote]

Sorry, I am not sure how that is supposed to help?

The problem is on the view side. The example code I posted up

Code:
<?php echo form_input("x", set_value("x", $input_x)); ?>
<?php echo form_input("y", set_value("y", $input_y)); ?>
<?php echo form_input("y", set_value("z", $input_z)); ?>

is on my view.

The controller populates $input_x, $input_y, and $input_z in my edit() function correctly. It's just that the edit() and add() functions both share the same form view, but $input_x, $input_y, $input_z does not exist in add().
#4

[eluser]InsiteFX[/eluser]
Because you are defining the variables before they are used!
#5

[eluser]John5788[/eluser]
[quote author="InsiteFX" date="1303906580"]Because you are defining the variables before they are used![/quote]

I tried your idea but it did not work.

I did find that if I populate $input_x, $input_y, $input_z with blank strings in my add() function, it will not throw the php error. But then my controller just looks silly with a bunch of blank values. I suppose I will use this until something better comes up.
#6

[eluser]toopay[/eluser]
What about this..
Code:
<?php
echo form_input("x", isset($input_x) ? $input_x : set_value("x"));
?>
#7

[eluser]John5788[/eluser]
[quote author="toopay" date="1303907052"]What about this..
Code:
<?php
echo form_input("x", isset($input_x) ? $input_x : set_value("x"));
?>
[/quote]

ive spent too much time away from PHP. forgot about ternary statements. thanks.




Theme © iAndrew 2016 - Forum software by © MyBB