I am learning PHP5 and CodeIgniter. I am writing my first application using this framework. I have a problem with validation. Although $this->form_validation->run()===FALSE, I have empty results of validation_errors(), form_error() and set_value(). I don’t understand why.
I found a solution, but I don’t like it. I created library MY_Form_validation:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
private $ci;
private $vulgarWords;
public function __construct()
{
parent::__construct();
$this->ci =& get_instance();
$this->set_error_delimiters('<span class="atserver_error">', '</span>');
}
public function setRules(array $vulgarWords) {
$this->vulgarWords = $vulgarWords;
$rules = array(
array('client_name', 'your name', 'trim|alpha_dash|max_length[45]'),
array('client_email', 'your e-mail address', 'trim|email|validate_client_data'),
array('client_mobile1', 'mobile phone #1', 'trim|validate_mobile1'),
array('client_mobile2', 'mobile phone #2', 'trim|validate_mobile2'),
array('client_direction1', 'phone direction #1', 'trim|integer|exact_length[2]'),
array('client_phone1', 'phone #1', 'trim|validate_phone1'),
array('client_direction2', 'phone direction #2', 'trim|integer|exact_length[2]'),
array('client_phone2', 'phone #2', 'trim|validate_phone2'),
array('client_town', 'your town', 'trim|alpha_dash|max_length[25]'),
array('property_estate_type_id', 'type of real estate', 'required'),
array('property_town', 'property town name', 'trim|alpha_dash|max_length[25]'),
array('property_district', 'district name', 'trim|alpha_dash|max_length[25]'),
array('property_street', 'street name', 'trim|alpha_dash|max_length[30]'),
array('property_house_no', 'house no', 'trim|max_length[8]'),
array('property_flat_no', 'flat no', 'trim|alpa_numeric|max_length[5]'),
array('property_floor_no', 'which floor', 'trim|integer|max_length[5]'),
array('property_floors', 'how many floors', 'trim|integer|max_length[5]'),
array('property_area', 'area', 'trim|numeric|max_length[11]'),
array('property_rooms', 'number of rooms', 'trim|integer|max_length[10]'),
array('property_offices', 'number of offices', 'trim|integer|max_length[10]'),
array('property_year_built', 'year built', 'trim|integer|max_length[5]'),
array('property_remarks', 'additional remarks', 'max_length[' . MAX_CHARS_PROPERTY_REMARKS . ']|validate_property_remarks|xss_clean'),
array('property_pictures', 'pictures or videos', 'validate_property_pictures'),
array('offer_type_id', 'offer type', 'required'),
array('change_to', 'for what to change', 'max_length[' . MAX_CHARS_CHANGE_TO . ']|validate_change_to|xss_clean'),
array('remarks', 'remarks', 'max_length[' . MAX_CHARS_REMARKS . ']|validate_remarks|xss_clean'),
array('valid_from', 'date valid from', 'trim|date|validate_dates_valid'),
array('valid_to', 'date valid to', 'trim|date'),
array('price', 'offer price', 'trim|numeric|max_length[15]|validate_price'),
array('captcha', 'captcha', 'trim|required|validate_captcha')
);
foreach ($rules as $r) {
$this->set_rules($r[0], $r[1], $r[2]);
}
}
...
public function validate_dates_valid($d1) {
$d2 = trim($this->ci->input->post('valid_to'));
if (!empty($d1) && !empty($d2)) {
$d1 = new DateTime($d1);
$d2 = new DateTime($d2);
if ($d1 > $d2) {
$this->set_message('validate_dates_valid', 'Date valid-to cannot be sooner than date valid-from.');
return FALSE;
}
}
return TRUE;
}
public function run() {
$b = parent::run();
if ($b === FALSE) {
$e = $this->_error_array;
if (array_key_exists('property_pictures', $_FILES) &&
!empty($_FILES['property_pictures']['name']) &&
!empty($_FILES['property_pictures']['name'][0])) {
$e['property_pictures'] = 'Please add files again.';
}
$this->ci->session->set_flashdata('field_errors', $e);
}
return $b;
}
}
and I created helper:
<?php
function myFormError($fieldName) {
$ci =& get_instance();
$e = $ci->session->flashdata('field_errors');
if (isset($e[$fieldName])) {
return "<span class='atserver_error'>" . $e[$fieldName] . '</span>';
}
return '';
}
function myValue($fieldName) {
$ci =& get_instance();
if (isset($$fieldName)) {
return $$fieldName;
} elseif (!$ci->common->emptyField($fieldName)) {
return $ci->input->post($fieldName);
}
return FALSE;
}
It works fine but I still don’t understand why I have to use $this->_error_array in run(). I don’t like it. I would like to have form_error() and set_value() working fine.
Please help. Thanks in advance.