I think I have a similiar issue as you are having.
If you do not pass all your form fields through a $this->form_validation->set_rules() and then the $this->form_validation->run() functionality it can make the reloading of the fields back in the form that is displaying errors RATHER UNPREDICTABLE.
The set_value( ‘fieldname’ ) function is suggested to be used.
This function lives in system/helpers/form_helper.php
What is happening is :-
If you just use form_helper.php and not Form_validation then set_value() gets the value of ‘field’ from the $_POST array and if it does not exists returns ‘’.
If you are also using the Form_validation class then the set_value() method
defers to the CLASS.
If the class is instantiated the value of ‘field’ is expected to be in the _field_data array of the Form_validation object.
BUT, if you have not attempted to validate one or more of you fields they wont exists in the _field_data array so no data is returned.
I have made the following changes to the set_value() function by creating a application/helpers/form_helpers.php file and putting the following code into it.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Form Value
*
* Grabs a value from the POST array for the specified field so you can
* re-populate an input field or textarea. If Form Validation
* is active it retrieves the info from the validation class
*
****************************************************************************
*
* Amended: RiggsFolly
* Version: 1.7.3
* Reason: If you use the form_validation class but do not validate a field
* on the form for some reason i.e. a checkbox which is not present
* in the $_POST array if the checkbox is not selected.
* When you come to try to reload the value on a form using set_value()
* the field does not exist in the form_validation->_field_date array.
*
* BY DEFAULT this function deferres to the form_validation->_field_date array
* if the form_validation object has been instantiated regardless of wether
* all the fields have been validated. This function therefore returns
* NOTHING as the field has not been passed through the validator and
* does not exists.
*
* FIX: If the validation_object exists check that the field exists in the
* _field_date array and if not use the $_POST array instead.
*
* @access public
* @param string
* @return mixed
*/
if ( ! function_exists('set_value'))
{
function set_value($field = '', $default = '')
{
log_message( 'debug', 'RUNNING MY NEW set_value' );
if (FALSE === ($OBJ =& _get_validation_object()))
{
if ( ! isset($_POST[$field]))
{
return $default;
}
return form_prep($_POST[$field], $field);
}
// Form_validation has been instantiated and we assume used.
// Check the field exists in the _field_date array.
if ( isset( $OBJ->_field_data[$field] ) )
{
return form_prep($OBJ->set_value($field, $default), $field);
}
else
{
if ( ! isset($_POST[$field]))
{
return $default;
}
return form_prep($_POST[$field], $field);
}
}
}
// ------------------------------------------------------------------------
/* End of file MY_form_helper.php */
/* Location: ./application/helpers/MY_form_helper.php */
I would be very interested to hear from someone in the CORE team to say whether this is a silly idea or not.
It seems to work for me so that I can use set_value(‘field’) in my views without having to change some of the reloads to $this->input->post(‘field’) if I am not validating a field.
I suppose the other option would be to create a ‘novalidation’ Rule which would just make a value available in the objects->_field_data array?
COMMENTS PLEASE