Part of the EllisLab Network
   
 
Why does the validation class do this?
Posted: 02 March 2008 04:29 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  774
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 05:17 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
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 05:19 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  774
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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120592 Total Logged-in Users: 56
Total Topics: 126626 Total Anonymous Users: 2
Total Replies: 665667 Total Guests: 452
Total Posts: 792293    
Members ( View Memberlist )