CodeIgniter Form Validation
- by mmcglynn
I am trying to set up validation on a simple contact form that is created using the form helper. No validation at all occurs. What is wrong?
In the code below, the “good” keyword always shows, regardless of what is entered into the form, and the saved values set via set_value are never shown.
Controller
// Contact
function contact() {
$data['pageTitle'] = "Contact";
$data['bodyId'] = "contact";
$this->load->library('form_validation');
$config_rules = array ('email' => 'required','message' => 'required');
$this->form_validation->set_rules($config_rules);
if ($this->form_validation->run() == FALSE) {
echo "bad";
$data['include'] = "v_contact";
$this->load->view('v_template',$data);
} else {
echo "good";
$data['include'] = "v_contact";
$this->load->view('v_template',$data);
}
}
View
echo validation_errors();
echo form_open('events/contact');
// name
echo form_label('Name', 'name');
$data = array (
'name' => 'name',
'id' => 'name',
'maxlength' => '64',
'size' => '40',
'value' => set_value('name')
);
echo form_input($data) . "\n<br />";
// email address
echo form_label('Email Address', 'email');
$data = array (
'name' => 'email',
'id' => 'email',
'maxlength' => '64',
'size' => '40',
'value' => set_value('email')
);
echo form_input($data) . "\n<br />";
// message
echo form_label('Message', 'message');
$data = array (
'name' => 'message',
'id' => 'message',
'rows' => '8',
'cols' => '35',
'value' => set_value('message')
);
echo form_textarea($data) . "\n<br />";
echo form_submit('mysubmit', 'Send Message');
echo form_close();