CodeIgniter Forums
Form Helper Label - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Helper Label (/showthread.php?tid=49012)



Form Helper Label - El Forum - 02-05-2012

[eluser]Perkin5[/eluser]
I want to give a label the class of 'required' so I can do:

Code:
$data = array(
'class'=>'required'
);
echo form_label('First Name','',$data);

but for a single attribute, it doesn't seem worth using an array. So I tried:

Code:
echo form_label('First Name','','class="required"');

but it doesn't work. A simlar approach works with other form helper elements so why doesn't this work? Or how can it be made to work?


Form Helper Label - El Forum - 02-05-2012

[eluser]CroNiX[/eluser]
Extend the form_helper with your own form_label() function. Looking at the code, it seems pretty easy to do what you are wanting there.


Form Helper Label - El Forum - 02-05-2012

[eluser]Perkin5[/eluser]
Gulp! That would be a first for me...


Form Helper Label - El Forum - 02-05-2012

[eluser]CroNiX[/eluser]
Read the Extending Helpers section. It's pretty easy (as is extending just about everything in CI).


Form Helper Label - El Forum - 02-05-2012

[eluser]Perkin5[/eluser]
OK - thanks


Form Helper Label - El Forum - 02-05-2012

[eluser]InsiteFX[/eluser]
Just p[ass it the css class name in $str
Saveas: ./application/helpers/MY_form_helper.php
Code:
// ------------------------------------------------------------------------

/**
* Form Label Str Tag
*
* @access public
* @param string The text to appear onscreen
* @param string The id the label applies to
* @param string Additional class str
* @return string
*/
if ( ! function_exists('form_label_str'))
{
    function form_label_str($label_text = '', $id = '', $str = '')
    {
        $label = '<label class="'.$str.'"';

        if ($id != '')
        {
            $label .= " for=\"$id\"";
        }

        $label .= ">$label_text</label>";

        return $label;
    }
}