• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form validation - validation error before form submit

I'm using the form helper and form validation, but I get differences on loading the page between my webserver at home (php5) and my hosted webserver php4.

Here's a snippet from my controller:
function index()
        // load form helper
        // form validation
        $rules['dname'] = "trim|required|min_length[5]|max_length[255]|xss_clean";
        $rules['description'] = "trim|required|min_length[5]|max_length[1024]|xss_clean";
        $this->validation->set_error_delimiters('<p class="frm_valid_err">', '</p>');

        $fields['dname'] = 'DName';
        $fields['description'] = 'Description';

        if ($this->validation->run() == FALSE)
            $page_data['page_title'] = 'View A';
            $page_data['page_title'] = 'View B';
Here's a snippet from my view
    echo $this->validation->error_string;
    $form_attributes = array('id' => 'form_id');
    $form_hidden = array('user_name' => getUnamefromUid($user), 'user_id' => $user);
    // form submits to self i.e. function index()
    echo form_open('',$form_attributes,$form_hidden);
    if ($this->validation->dname)
        $dname_value = $this->validation->dname;
        $dname_value = 'Type something here';
    if ($this->validation->description)
        $dscr_value = $this->validation->description;
        $dscr_value = 'Type something here';
    $text_name = array(
                  'name' => 'dname',
                  'id' => 'dname',
                  'value' => $dname_value,
                  'rows' => '2',
                  'cols' => '60',
                  'style' => 'width:100%',
    $text_descr = array(
                  'name' => 'description',
                  'id' => 'description',
                  'value' => $dscr_value,
                  'rows' => '3',
                  'cols' => '60',
                  'style' => 'width:100%',
    echo form_textarea($text_name);
    echo form_textarea($text_descr);
    echo '<br />'.form_submit('', 'Submit!');
    echo form_close();

There's another view (View B) which just echos "Success!".

On my webserver at home, I get the behaviour I expect.

On the hosted box, I get a "must have a value" validation error for both of the fields when I load View A, before I've even submitted the form.

Is this likely to be something to do with the version of php, my code, or the fact that the page is being rendered by facebook.

Okay, I've solved this. I'm not exactly sure why, but the problem is a facebook thing.
Whenever one does a POST, fb adds certain things to the POST array. I was already aware of this, because before using a
I strip the fb stuff:
function unset_fbpost($uset)
        if (isset($uset['fb_sig_time']))
        return $uset;

Anyway, when using ONE view and posting to itself as in my previous posting, the POST array seems to trigger the validation on it's initial load because of the facebook stuff.
A combination of CI and facebook = weirdness!

So I solved it, by having an initial view which shows the form, but without the validation and which posts to another function.
This other function can then handle the validation as per my previous post.
Seems to work.

Hope this saves someone several hours!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.