Welcome Guest, Not a member yet? Register   Sign In
Form Validation > Preview button
#1

[eluser]drewbee[/eluser]
Hi All,

I have a form that has the option of 'preview' before actually posting.

It appears the form helper class doesn't honor submitted data unless an error actually occurs.

So how did I have to rig up a solution to get this working correctly, in a sense where the preview box will show up with the posted data, as well as original form with the data appearing again?

I had to set a random form error manually EX.

Code:
$this->form_validation->_error_array[] = 'asdfasdf';

causing run() to fail, re-displaying my form and populating it as needed.

Is their a better way to do this or is this quick little rig up option the only way? The original goal is to redisplay the form is run() fails OR the submit_preview is pushed.

Unfortunately, it is a no go if run() passes, and any other scenario is present because the data will not repopulate.

Looks like another function for the form_validation class
Code:
function force_form_repopulate()
{
    $this->_error_array[] = '';
}

Has anyone else encountered this and what they did to work around this? One of the biggest problems that could potentially be a problem is the call to show_errors(); I show my errors individually; but in a call to show_errors() all will show up.
#2

[eluser]JayTee[/eluser]
You can use the set_value() function to have a default value and just set that to the posted value:
Code:
<p>&lt;?= $this->input->post('formvalue') ?&gt;</p>
&lt;input type="text" name="formvalue" value="&lt;? set_value('formvalue',$this-&gt;input->post('formvalue') ?&gt;">
If there's no posted value, the default would be blank.
#3

[eluser]drewbee[/eluser]
Aye, that would be fine and dandy if set_value had a value to set. For some reason, the values all the set_xxx pull from only get populated on a FAILED run();

Weird huh?
#4

[eluser]drewbee[/eluser]
Interestingly enough, Codeigniter doesn't process the empty error and as such is never displayed in the validation_errors() function as long as it is set to an empty string thanks to this little part of the method error_string()
Code:
foreach ($this->_error_array as $val)
        {
            if ($val != '')
            {
                $str .= $prefix.$val.$suffix."\n";
            }
        }

With that being said, I have just added 1 new rigup to my validation class. haha.

It seems like so much of this comes sooo close to what I need, but just isn't quite their.
Code:
class CIEXT_Form_validation extends Form_validation
{
    var $allow_run = TRUE;

    function run($group = '')
    {
        
        if ($this->allow_run == FALSE)
        {
            $this->_error_array[] = '';    
        }


        return parent::run($group);
    }
}

I've got some other stuff in the run() so i didn't just pull this out just for this occasion; obviously snipped down.
#5

[eluser]JayTee[/eluser]
I've been looking at the internals of the set_value function and am having some difficulties figuring out why there's an error.

Basically, the form_validation library operates directly on the $_POST array, so the data *should* be there if the library has been instantiated.

Perplexing to say the least...
#6

[eluser]drewbee[/eluser]
Same here. Part of it is I am not completely understanding what the first line is doing though.

If I were to take a quick guess, I would say the following line is true in our case:

Code:
FALSE === ($OBJ =& _get_validation_object()

causing the following to be returned:

Code:
return form_prep($OBJ->set_value($field, $default));

Which, given the defaults passed in as parameters ($field & $default are both empty strings), that is what is returned. Silly, huh?
#7

[eluser]JayTee[/eluser]
[quote author="drewbee" date="1236071389"]Which, given the defaults passed in as parameters ($field & $default are both empty strings), that is what is returned. Silly, huh?[/quote]
It's almost as if it's intentional in this case.

Have you tried calling the set_data function directly just to see if there's something in there after the run() was successful?
Code:
$this->form_validation->set_data('field_name','test value')




Theme © iAndrew 2016 - Forum software by © MyBB