• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation in autoload PHP 4 (Fix suggested)

#11
[eluser]karloff[/eluser]
code is there, i'll have a test with my code and see what i can find. cheers

#12
[eluser]karloff[/eluser]
hey guys, i've been trying to mess about and get the code working, but with no success.

I have the same code as stated in the wiki (and above) but it doesn't seem to work, anyone know why?

#13
[eluser]Pascal Kriete[/eluser]
karloff, are you using Seppo's sample code, or do you have an implementation specific to your application?

If you have your own code, could you give us the smallest example possible needed to reproduce your problem please.

#14
[eluser]karloff[/eluser]
i am using seppo's code. want me to post the controller and view? would it help?

#15
[eluser]Pascal Kriete[/eluser]
A reduced version, as little code as possible, but yes please.

#16
[eluser]karloff[/eluser]
controller
Code:
function comments ()
    {
        
    $data['title'] = $this->uri->segment(3);
    $this->db->where('url_title', $this->uri->segment(3));
        $data['entriesquery'] = $this->db->get('entries');


        $this->db->where('entry_id', $this->uri->segment(3));
        $data['commentquery'] = $this->db->get('comments');

        
    $rules['author'] = "trim|required";
        $rules['email'] = "trim|required|valid_email|max_length[50]";
        $rules['body'] = "trim|required|htmlentities|max_length[2000]";
        
    $this->validation->set_rules($rules);

        $fields['author']    = 'Author';
        $fields['body']        = 'body';
        $fields['email']    = 'Email Address';

    $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('<p class="error">', '</p>');

        if ($this->validation->run() == FALSE) {
        $this->load->vars(array(
            'data' => $data,
         ));
        $this->load->view('comment_view', $data);
            
        } else {
                      
        $this->db->insert('comments', $_POST);
        redirect('blog/comments/'.$_POST['entry_id']);
        }



    }
function comments_insert ()
    {
    $rules['author'] = "trim|required";
        $rules['email'] = "trim|required|valid_email|max_length[50]";
        $rules['body'] = "trim|required|htmlentities|max_length[2000]";
        
    $this->validation->set_rules($rules);

        $fields['author']    = 'Author';
        $fields['body']        = 'body';
        $fields['email']    = 'Email Address';

    $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('<p class="error">', '</p>');

        if ($this->validation->run() == FALSE) {
        
        $this->load->view('comment_view', $data);
            
        } else {

                          
            $this->db->insert('comments', $_POST);
        
        redirect('blog/comments/'.$_POST['entry_id']);
        }
    }
view
Code:
&lt;?=form_open('blog/comments/' .$this->uri->segment(3));?&gt;
    
&lt;?php     echo $this->validation->error_string; ?&gt;
    
    <fieldset>

        <ul>
            <li><label>Comments (required):</label></li>
            
            <li>&lt;? $data = array(
                                'name'    => 'body',
            
                                );
                                echo form_textarea($data);
                             ?&gt;</li>
            <li><label>Name (required):</label></li>
            <li>&lt;?=form_input('author', $row->author); ?&gt;</li>
            <li><label>URL:</label></li>
            <li>&lt;input type="text" value="http://" name="url" /&gt;&lt;/li>            

            <li><label>Email (not published, required):</label></li>
            <li>&lt;input type="text" value="" name="email" /&gt;&lt;/li>            

            <li>&lt;input type="submit" value="Submit Comment" class="submit" /&gt;&lt;/li>
                
        
        </ul>
        &lt;?=form_hidden('entry_id', $this->uri->segment(3));?&gt;
        
    </fieldset>
    &lt;/form&gt;

#17
[eluser]Pascal Kriete[/eluser]
I can't reproduce, but I also had to remove the db specific stuff. Let's go back another step and start with something even simpler.

I've attached a zip of a basic controller and view, with only one required field. My test setup (php 4.4.7) is autoloading the validation library. Can you try running that very simple controller and tell us what errors, if any, come up.

Once we get that up to speed we can start reconstructing your original code.

#18
[eluser]karloff[/eluser]
thanks inparo, but i managed to get my server upgraded to php5 therefore the bug is goneSmile

#19
[eluser]lostsock[/eluser]
Just thought I should point out to PHP 4 users that you cannot load the validation library in the class constructor. You have to load it specifically in the function or via autoload.

The following will fail in PHP 4
Code:
class Test extends Controller {
    function Test() {
        parent::Controller();
        // LOADING THE validation CLASS FROM THE CONTROLLER WILL FAIL IN PHP 4
        $this->load->library('validation');
    }
    
    function index() {
        // Prep Validation
        $rules['author']    = "trim|required";
        $this->validation->set_rules($rules);

        $fields['author']    = 'Author';
        $this->validation->set_fields($fields);
        
        if (empty($_POST)) {
            $this->validation->author = "Testing";
        }
        
        // Run validation
        if ($this->validation->run() == FALSE) {
            $this->load->view('admin/category');
        } else {
            die('woohoo!');
        }
    }
}
To make it work you must either add it to the autoload array or call $this->load->library('validation'); from within the method itself
Code:
function index() {
    $this->load->library('validation');
    // ETC...
}

Hope this saves someone else a couple of hours work Wink

#20
[eluser]Derek Jones[/eluser]
What version of CI are you using, lostsock, and what version of PHP? You shouldn't have any problems there if you're on the latest (or even a recent) version, as the bug was fixed long ago.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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