Welcome Guest, Not a member yet? Register   Sign In
Multiple Fields with a single callback using the form_validation library?
#2

[eluser]drewbee[/eluser]
It's funny you should mention this, because I do exactly this. It really helps keep things a lot more organized.

Rules Array:
Code:
// Controller public page
function edit_profile()
{
    // ... snip ...
        $rules =  array(
                    array('field'     => 'timezone',
                          'label'     => 'Timezone',
                          'rules'     => 'required|callback__edit_profile[timezone]'
                      ),  
                    array('field'     => 'birth_month',
                          'label'     => 'Birth Month',
                          'rules'     => 'required|callback__edit_profile[birth_month]'
                      ),
                    array('field'     => 'birth_day',
                          'label'     => 'Birth Day',
                          'rules'     => 'required|callback__edit_profile[birth_day]'
                      ),
                    array('field'     => 'birth_year',
                          'label'     => 'Birth Year',
                          'rules'     => 'required|callback__edit_profile[birth_year]'
                      ),
                    array('field'    => 'display_age',
                          'label'    => 'Display Age',
                          'rules'    => 'callback__edit_profile[display_age]'
                      ),
                    array('field'    => 'country',
                          'label'    => 'Country',
                          'rules'    => 'callback__edit_profile[country]'
                      ),
                    array('field'    => 'state',
                          'label'    => 'State/Province',
                          'rules'    => 'callback__edit_profile[state]'
                      )
                );
                
        $this->form_validation->set_rules($rules);
    // ...snip. ...
}

When you are setting rules, callbacks allow you to pass a value to the function as the second parameter (which I have setup as $which). All you need to do is simply enclose what you are trying to do in brackets [ ]

Then my edit profile callback:
Code:
function _edit_profile($value = '', $which = '')
    {
        switch($which)
        {
            
            case 'timezone':
                if (!array_key_exists($value, $this->lookups['timezone']))
                {

                    $this->form_validation->set_message('_edit_profile', 'Invalid Timezone Selected');
                    return FALSE;
                }
            break;
            
            case 'birth_month':
                if (!array_key_exists($value, $this->lookups['birth_month']))
                {
                    $this->form_validation->set_message('_edit_profile', 'Invalid Month Selected');
                    return FALSE;
                }            
            break;
            
            case 'birth_day':
                if (!array_key_exists($value, $this->lookups['birth_day']))
                {
                    $this->form_validation->set_message('_edit_profile', 'Invalid Day Selected');
                    return FALSE;                    
                }
            break;
            
            case 'birth_year':
                if (!array_key_exists($value, $this->lookups['birth_year']))
                {
                    $this->form_validation->set_message('_edit_profile', 'Please enter a valid year ex. xxxx');
                    return FALSE;                    
                }            
            break;
            
            case 'display_age':
                if (!in_array($value, $this->lookups['display_age']))
                {
                    $this->form_validation->set_message('_edit_profile', 'Invalid value passed');
                    return FALSE;
                }
            break;
            
            case 'country':
                if (!array_key_exists($value, $this->lookups['countries']))
                {
                    $this->form_validation->set_message('_edit_profile', 'Invalid Country Selected');
                    return FALSE;                    
                }        
            break;
            
            case 'state':
                if (!array_key_exists($value, $this->lookups['states']))
                {
                    $this->form_validation->set_message('_edit_profile', 'Invalid State Selected');
                    return FALSE;                    
                }                
            break;
            
        }

        return TRUE;
    }

Hope this helps. Some people may raise the question of needing to pass a value and which validation to check. Simply use delimitors EX: set_rules> callback__edit_profile[state:32] and do a split in the callback.


Messages In This Thread
Multiple Fields with a single callback using the form_validation library? - by El Forum - 03-19-2009, 09:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB