EllisLab text mark
Advanced Search
     
Simple question (I hope) about form_validation
Posted: 06 January 2011 12:33 AM   [ Ignore ]
Joined: 2010-11-29
16 posts

Do I need to declare (with set_rules) each field on my form in order for it to repopulate of validation failure? 

It seems that fields not defined in set_rules do not repopulate on failure.

Thank you.

 
Posted: 06 January 2011 12:42 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-03-11
30 posts

Yes.

See: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#repopulatingform

“Don’t forget to include each. field name in the set_value() functions!”

 
Posted: 06 January 2011 12:48 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-11-29
16 posts

Thanks, I did read that, but it isn’t clear if it each field name has to be set in the set_rules.  It only refers to set_value.  Thank you.

 
Posted: 03 February 2011 12:20 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-18
8 posts

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

 
Posted: 03 February 2011 01:21 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-18
8 posts

Actually the more I think about this the more I think I was wrong to try and make set_value() work regardless of whether a field has been validated.

Actually
if the field is validated use set_value( ‘field’ ).

if field is not validated use $this->input->post( ‘field’ )

Then when the next guy comes to maintain the code it is self documenting.


So I think I am going to remove the code I created as I would also need to make the same changes to all the functions provided to help in this area like set_select(), set_checkbox(), set_radio() in order to make the whole process predictabe as I originally tried to do.

 
Posted: 03 February 2011 02:54 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-01-26
183 posts

does form validation supports checking if the data already existed in db? BTW, im new to CI and i’m currently working on my registration module.

 Signature 

My First CI website:
DCS - Practicum Placement System

keep moving forward and trust in the Lord’s way!

 
Posted: 03 February 2011 05:40 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-18
8 posts

No I am afraid not. You have to code that yourself.

 
Posted: 04 February 2011 11:38 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-01-26
183 posts

thanks RiggsFolly,

found some site to extend Form validation and that’s what I used. CI rocks ^_^.

 Signature 

My First CI website:
DCS - Practicum Placement System

keep moving forward and trust in the Lord’s way!

 
Posted: 04 February 2011 11:50 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-18
8 posts

Well let everybody know what you used. It may be useful to all of us.

 
Posted: 17 August 2012 09:45 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2012-05-23
26 posts
RiggsFolly - 03 February 2011 01:21 PM

Actually the more I think about this the more I think I was wrong to try and make set_value() work regardless of whether a field has been validated.

Actually
if the field is validated use set_value( ‘field’ ).

if field is not validated use $this->input->post( ‘field’ )

Then when the next guy comes to maintain the code it is self documenting.


So I think I am going to remove the code I created as I would also need to make the same changes to all the functions provided to help in this area like set_select(), set_checkbox(), set_radio() in order to make the whole process predictabe as I originally tried to do.

Hi, I reckon this is the best way to validate a field even if it is rule-less. Your idea to improve the core into the set_value() function is good but I guess that when some codeigniter framework update is coming this is gonna overwrite the whole job you did so… maybe redeclare everything in the controller. What you think about?

Shella

 
Posted: 31 August 2012 07:18 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2012-08-31
1 posts

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:

set_value function:
Replace
if (
FALSE === ($OBJ =& _get_validation_object())) {
With

$OBJ =& _get_validation_object();
if (!
$OBJ || ($OBJ && !$OBJ->contains_field($field))) 
set_select function:
Replace:
if (
$OBJ === FALSE)
With:
if (!
$OBJ || ($OBJ && !$OBJ->contains_field($field))) 
set_checkbox function:
Replace:
if (
$OBJ === FALSE)
With:
if (!
$OBJ || ($OBJ && !$OBJ->contains_field($field))) 
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:

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.

 

 

 
Posted: 07 October 2012 07:35 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-12
115 posts

Tried and tested this patch for the set_checkbox() behavior and I can confirm that this works.

@gobblegobble Thanks for contributing!

 Signature 

—-
Moonbeetle web design & website redesign