Welcome Guest, Not a member yet? Register   Sign In
Simple question (I hope) about form_validation
#11

[eluser]Unknown[/eluser]
Hello,

This is a custom source patch for a validation 'bug' in CodeIgniter 2.0.3.

The bug:
All the set_* function from the form_helper is set to default unless the specified field (_POST parameter name) is set in a validation set.
The documentation does not explicitly state that the field needs to be specified in a validation set, so I consider this to be a bug.

What we want:
- The field should be remembered and set accordingly and should be indifferent as to wether the parameter has been put into a validation set.
- The field should be set to default if it isn't in the _POST collection.
- The field should be validated if the field is in a validation set.

The fix:
Code:
set_value function:
Replace:
if (FALSE === ($OBJ =& _get_validation_object())) {
With:
$OBJ =& _get_validation_object();
if (!$OBJ || ($OBJ && !$OBJ->contains_field($field)))

Code:
set_select function:
Replace:
if ($OBJ === FALSE)
With:
if (!$OBJ || ($OBJ && !$OBJ->contains_field($field)))

Code:
set_checkbox function:
Replace:
if ($OBJ === FALSE)
With:
if (!$OBJ || ($OBJ && !$OBJ->contains_field($field)))

Code:
set_radio function:
Replace:
if ($OBJ === FALSE)
With:
if (!$OBJ || ($OBJ && !$OBJ->contains_field($field)))
Create a new file in "codeigniter/application/libraries/" named "MY_Form_validation.php"
Content of the file:
Code:
class MY_Form_validation extends CI_Form_validation {

function __construct($config = array()) {
  
  parent::__construct($config);    

}    
function contains_field($field) {
  
  return isset($this->_field_data[$field]);

}
}

Make sure you're autoloading libraries prefixed with "MY_", if I recall correctly this is default behaviour in Codeigniter 2.0.3

Your set_* function should now work as expected.



#12

[eluser]moonbeetle[/eluser]
Tried and tested this patch for the set_checkbox() behavior and I can confirm that this works.

@gobblegobble Thanks for contributing!




Theme © iAndrew 2016 - Forum software by © MyBB