Welcome Guest, Not a member yet? Register   Sign In
Validation won't work
#1

[eluser]IamPrototype[/eluser]
I load the library validation in my controller like this.

Code:
function Overview()
    {
        parent::Controller();
        
        $this->load->library('validation');
    }

This is my submit_comment function which is placed in the Overview Controller.

Code:
function submit_comment()
    {    
        $rules['author']     = 'required|min_length[5]|max_length[12]|xss_clean|trim|addslashes';
        $rules['body']         = 'required|min_length[5]|max_length[150]|xss_clean|trim|addslashes';
        
        $this->validation->set_rules($rules);
            
        if($this->validation->run() == FALSE)
        {
        // false
            redirect('overview/comments/' . $this->input->post('headline') . '/' . $this->input->post('entry_id'));
        }
        else
        {
        // true
            redirect('overview/comments/' . $this->input->post('headline') . '/' . $this->input->post('entry_id'));
            
            $comment_details = array(
                                    'author' => $this->input->post('author', TRUE),
                                    'body' => $this->input->post('body', TRUE),
                                    'date' => time(),
                                    'entry_id' => $this->input->post('entry_id', TRUE)
                                    );
        
            $this->db->insert('comments', $comment_details);
        }
    }

My form looks like this:

Code:
<?=$this->validation->error_string?>

<?=form_open('overview/submit_comment')?>

<?=form_hidden('entry_id', $this->uri->segment(4))?>
<?=form_hidden('headline', $this->uri->segment(3))?>

<p>Name:</p>
<p>&lt;input type="text" name="author" size="15" /&gt;&lt;/p>

<p>Comment:</p>
<p>&lt;textarea name="body" cols="40"&gt;&lt;/textarea></p>

<p>&lt;input type="submit" value="Submit Comment!" /&gt;&lt;/p>
&lt;/form&gt;

It just returns false whenever I try. I could use some help... I've tried different stuff, but it didn't work well. Also I would to have the error message displayed on the form page, but it won't.. (I mean so the guest can see what he/she is doing wrong, too many chars etc.)

** ...Yes, I've seen that I use the XSS cleaner twice, my bad.
#2

[eluser]xwero[/eluser]
You are working with the old validation library, is there a reason not to use the new library?

I believe Micheal Wales mentioned this before but he didn't write why it returned false all the time.
#3

[eluser]IamPrototype[/eluser]
Old??? I'm using the newest version of CI?
#4

[eluser]Ivan A. Zenteno[/eluser]
maybe you need $this->validation->set_fields($array_fields)
#5

[eluser]IamPrototype[/eluser]
[quote author="Ivan A. Zenteno" date="1232439139"]maybe you need $this->validation->set_fields($array_fields)[/quote]

Instead of rules, or what?
#6

[eluser]xwero[/eluser]
the old validation library : Validation.php, the new validation library : Form_validation.php.
Old adding of rules
Code:
$rules['author']     = 'required|min_length[5]|max_length[12]|xss_clean|trim|addslashes';
$rules['body']         = 'required|min_length[5]|max_length[150]|xss_clean|trim|addslashes';
        
$this->validation->set_rules($rules);
New adding of rules
Code:
$this->validation->set_rules('author','Author','required|min_length[5]|max_length[12]|xss_clean|trim|addslashes');
$this->validation->set_rules('body','Body','required|min_length[5]|max_length[150]|xss_clean|trim|addslashes');
#7

[eluser]IamPrototype[/eluser]
I tried the new way of adding rules, and it won't work, check my demo on http://habbofreak.kliboo.net/ (click "Comments" and try adding some comments). It doesn't turn into false. I can click "Submit Comment!" without any input value and it submits the comment without data.

# submit_comment function placed in my controller
Code:
function submit_comment()
    {    
        $this->validation->set_rules('author','Author','required|min_length[5]|max_length[12]|xss_clean|trim|addslashes');
        $this->validation->set_rules('body','Body','required|min_length[5]|max_length[150]|xss_clean|trim|addslashes');
            
        if($this->validation->run() == FALSE)
        {
        // false
            redirect('overview/comments/' . $this->input->post('headline') . '/' . $this->input->post('entry_id'));
        }
        else
        {
        // true
            $comment_details = array(
                                    'author' => $this->input->post('author', TRUE),
                                    'body' => $this->input->post('body', TRUE),
                                    'date' => time(),
                                    'entry_id' => $this->input->post('entry_id', TRUE)
                                    );
        
            $this->db->insert('comments', $comment_details);
        
            redirect('overview/comments/' . $this->input->post('headline') . '/' . $this->input->post('entry_id'));
        }
    }

# html form placed in my view
Code:
&lt;?=$this->validation->error_string?&gt;

&lt;?=form_open('overview/submit_comment')?&gt;

&lt;?=form_hidden('entry_id', $this->uri->segment(4))?&gt;
&lt;?=form_hidden('headline', $this->uri->segment(3))?&gt;

<p>Name:</p>
<p>&lt;input type="text" name="author" /&gt;&lt;/p>

<p>Comment:</p>
<p>&lt;textarea name="body" cols="40"&gt;&lt;/textarea></p>

<p>&lt;input type="submit" value="Submit Comment!" /&gt;&lt;/p>
&lt;/form&gt;
#8

[eluser]xwero[/eluser]
the new validation way also means loading the new validation library.
Code:
$this->load->library('form_validation');
$this->form_validation->set_rules('author','Author','required|min_length[5]|max_length[12]|xss_clean|trim|addslashes');
        $this->form_validation->set_rules('body','Body','required|min_length[5]|max_length[150]|xss_clean|trim|addslashes');
            
        if($this->form_validation->run() == FALSE)
#9

[eluser]IamPrototype[/eluser]
Won't work Sad Keeps getting false now.
#10

[eluser]xwero[/eluser]
You could try to reduce the rules to check of they are the problem.




Theme © iAndrew 2016 - Forum software by © MyBB