Welcome Guest, Not a member yet? Register   Sign In
Why does the validation class do this?
#1

[eluser]adamp1[/eluser]
Code:
function set_radio($field = '', $value = '')
    {
        if ($field == '' OR $value == '' OR  ! isset($_POST[$field]))
        {
            return '';
        }
            
        if ($_POST[$field] == $value)
        {
            return ' checked="checked"';
        }
    }

OK so this is the basic function to reset a radio button after a form has been submitted. What I want to ask is why does the function get the value from $_POST? Why not $this->{$field}??

Surely that would keep in with everything with the class. And also make outputting form data simpler.

Since I know many people use the hack of setting values in the validation class, so they don't have to then have ugly if statements just to either output a form return value or the value from the DB. But using the above method you can't do this for radio/checkboxs.
#2

[eluser]xwero[/eluser]
The validation library only takes care of the values that exist after a post request.

Maybe instead of the having these methods in the validation library maybe they should be in the form helper.
Code:
function form_checkbox($data = '', $value = '', $checked = TRUE, $extra = '')
    {
        $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
    
        if(isset($_POST[$defaults['name']]))
        {
            $checked = $data['checked'];
        }
        elseif (is_array($data) AND array_key_exists('checked', $data))
        {
            $checked = $data['checked'];
        
            if ($checked == FALSE)
            {
                unset($data['checked']);
            }
            else
            {
                $data['checked'] = 'checked';
            }
        }
    
        if ($checked == TRUE)
            $defaults['checked'] = 'checked';
        else
            unset($defaults['checked']);

        return "<input ".parse_form_attributes($data, $defaults).$extra." />\n";
    }

edit : changed code
#3

[eluser]adamp1[/eluser]
That would be better I think, seems odd having them in the validation library.




Theme © iAndrew 2016 - Forum software by © MyBB