Welcome Guest, Not a member yet? Register   Sign In
How to validate? Getting TRUE whenever I submit even if it is FALSE
#1

[eluser]IamPrototype[/eluser]
I am still getting TRUE even if it is FALSE. This is my code:

See the script online:
http://codeigniter.kliboo.net/

# welcome.php (my default controller)
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->load->library('form_validation');
    }
    
    function index()
    {
        redirect('welcome/add');
    }

    function _entry_form()
    {
        $form = array(
            'fields' => array(
                'id' => 'Entry ID',
                'title' => 'Title',
                'body' => 'Body Text',
                'author' => 'Author',
                'posted' => 'Posted Data',
                'slug' => 'URL',
                'allow_comments' => 'Allow Comments?',
                'categorie_id' => 'Categorie ID',
                'published' => 'Status',
            ),
            'rules' => array(
                'id' => 'required|numeric',
                'title' => 'trim|required|min_length[6]|max_length[22]',
                'body' => 'trim|required',
                'author' => 'trim|required',
                'posted' => 'required',
                'slug' => 'required',
                'allow_comments' => 'required|numeric',
                'categorie_id' => 'required|numeric',
                'published' => 'required|numeric',
            ),
            'values' => array(
                'id' => '',
                'title' => '',
                'body' => '',
                'author' => '',
                'posted' => '',
                'slug' => '',
                'allow_comments' => '',
                'categorie_id' => '',
                'published' => '',
            ),
        );
        
        return $form;
    }
    
    function add()
    {        
        $form = $this->_entry_form();
        $this->form_validation->set_rules($form['rules'], $form['fields']);
        
        $form['headline'] = 'Add an Entry';
        $form['action'] = site_url('welcome/add');
        $form['button'] = 'Add Entry';
        
        if ($this->form_validation->run() == FALSE)
        {
            $form['error'] = $this->form_validation->error_string();
            $this->load->view('admin/entry_form_view', $form);
        }
        else
        {
            print 'You have passed the validation!';
        }    
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

# entry_form_view.php (my form view)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

    <h1>&lt;?= $headline ?&gt;</h1>
    
    &lt;form method="post" action="&lt;?= $action ?&gt;"&gt;
    
    <p>&lt;?= $error ?&gt;</p>
    
    <p><label>&lt;?= $fields['title'] ?&gt;</label></p> <p>&lt;?= form_input('title', $values['title']) ?&gt;</p>
    
    <p><label>&lt;?= $fields['slug'] ?&gt;</label></p> <p>&lt;?= form_input('slug', $values['slug']) ?&gt;</p>
    
    <p><label>&lt;?= $fields['body'] ?&gt;</label></p> <p>&lt;?= form_textarea('body', $values['body']) ?&gt;</p>
    
    <p>&lt;?= form_submit('submit', $button) ?&gt;</p>
    
    &lt;/form&gt;
    
    <p>&lt;?= anchor('welcome', 'Cancel') ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;

I know I have not added every single field yet, so how can my validation be TRUE? Am I missing something or doing something wrong? Meh, I loved the way the old validation was created - it was easy Tongue

I think it is something with this line and my form array:

Code:
$this->form_validation->set_rules($form['rules'], $form['fields']);

Do I have to re-build my array in another way or? Also this line seems wrong to me:

Code:
$form['error'] = $this->form_validation->error_string();

I mean, is it the right function name for the error? I hope somebody might help me ASAP. Smile




Theme © iAndrew 2016 - Forum software by © MyBB