Part of the EllisLab Network
   
 
Why does the validation class do this?
Posted: 02 March 2008 05:29 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  880
Joined  09-01-2007
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.

 Signature 

Kaydoo - A day in the life of a developer
BackendPro Control Panel

Profile
 
 
Posted: 02 March 2008 06:17 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

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.

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

Profile
 
 
Posted: 02 March 2008 06:19 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  880
Joined  09-01-2007

That would be better I think, seems odd having them in the validation library.

 Signature 

Kaydoo - A day in the life of a developer
BackendPro Control Panel

Profile