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

#1
[eluser]jahboite[/eluser]
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:
Code:
function index()
    {
        // load form helper
        $this->load->helper('form');
        $this->load->library('validation');
        
        // 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>');
        $this->validation->set_rules($rules);

        $fields['dname'] = 'DName';
        $fields['description'] = 'Description';
        $this->validation->set_fields($fields);

        if ($this->validation->run() == FALSE)
        {
            $page_data['page_title'] = 'View A';
            $this->load->view('a_vw',$page_data);
        }
        else
        {
            
            $page_data['page_title'] = 'View B';
            $this->load->view('b_vw',$page_data);
        }
    }
Here's a snippet from my view
Code:
&lt;?php
    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;
    }
    else
    {
        $dname_value = 'Type something here';
    }
    
    if ($this->validation->description)
    {
        $dscr_value = $this->validation->description;
    }
    else
    {
        $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();

?&gt;
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.

#2
[eluser]jahboite[/eluser]
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
Code:
$this->db->insert('table_name',$_POST);
I strip the fb stuff:
Code:
function unset_fbpost($uset)
    {        
        if (isset($uset['fb_sig_time']))
        {
            unset($uset['fb_sig_time']);
            unset($uset['fb_sig_added']);
            unset($uset['fb_sig_user']);
            unset($uset['fb_sig_profile_update_time']);
            unset($uset['fb_sig_session_key']);
            unset($uset['fb_sig_expires']);
            unset($uset['fb_sig_api_key']);
            unset($uset['fb_sig_in_canvas']);
            unset($uset['fb_sig_friends']);
            unset($uset['fb_sig']);
        }
        
        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-2019 MyBB Group.