Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_get_validation_object() returns always FALSE #598

Closed
tomas-vccp opened this issue Oct 19, 2011 · 1 comment
Closed

_get_validation_object() returns always FALSE #598

tomas-vccp opened this issue Oct 19, 2011 · 1 comment

Comments

@tomas-vccp
Copy link

Version: CodeIgniter 2.0.3
System: PHP Version 5.3.2-1ubuntu4.9
File: ./system/helpers/form_helper.php
Affected Code:

function &_get_validation_object()
{
    $CI =& get_instance();

    // We set this as a variable since we're returning by reference
    $return = FALSE;

    if ( ! isset($CI->load->_ci_classes) OR  ! isset($CI->load->_ci_classes['form_validation']))
    {
        return $return;
    }

    $object = $CI->load->_ci_classes['form_validation'];

    if ( ! isset($CI->$object) OR ! is_object($CI->$object))
    {
        return $return;
    }

    return $CI->$object;
}

the _get_validation_object() function always returns FALSE as the first if condition fails (because _ci_classes is a protected field of the Loader class). Also,

$object = $CI->load->_ci_classes['form_validation'];

throws an error as, again, _ci_classes is a protected field.

Here is a suggested fix

function &_get_validation_object()
{
    $CI =& get_instance();

    // We set this as a variable since we're returning by reference
    $return = FALSE;

    if ( !$CI->load->is_loaded('form_validation'))
    {
        return $return;
    }

    // this may need to be changed to a more suitable name
    $object = 'form_validation';

    if ( ! isset($CI->$object) OR ! is_object($CI->$object))
    {
        return $return;
    }

    return $CI->$object;
}

The fix uses the is_loaded method the Loader class already provides - The Law of Demeter prohibits access such as $CI->load->_ci_classes and although CI uses it extensively here is an example why it shouldn't!

@narfbg
Copy link
Contributor

narfbg commented Mar 1, 2012

This function has been changed in CI 2.1, can you confirm if it's OK in there?

@narfbg narfbg closed this as completed Jun 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants