How to validate? Getting TRUE whenever I submit even if it is FALSE - El Forum - 01-23-2009
[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">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<h1><?= $headline ?></h1>
<form method="post" action="<?= $action ?>">
<p><?= $error ?></p>
<p><label><?= $fields['title'] ?></label></p> <p><?= form_input('title', $values['title']) ?></p>
<p><label><?= $fields['slug'] ?></label></p> <p><?= form_input('slug', $values['slug']) ?></p>
<p><label><?= $fields['body'] ?></label></p> <p><?= form_textarea('body', $values['body']) ?></p>
<p><?= form_submit('submit', $button) ?></p>
</form>
<p><?= anchor('welcome', 'Cancel') ?></p>
</body>
</html>
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 
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.
|