Welcome Guest, Not a member yet? Register   Sign In
Displaying labels in HTML from $config
#1

[eluser]B___[/eluser]
Hello:

This may have been already covered or may be ridiculously obvious, so I apologize in advance for either case.

I am experimenting with the new Form Validation class (which I love so far). Is there an easy way to display the labels that are defined in the $config array when setting rules? I had thought of just passing the $config to the view, but then wouldn't know how to access the label value by specifying the field value.

ie: given the following:

$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
));

how would one access the label Username by specifying the field username.

I didn't see this ability anywhere in the manual, but thought it wouldn't be too hard to write a helper function to do this.

Any suggestions? Thanks in advance!
#2

[eluser]James Gifford[/eluser]
I too would like to be able to output the value for the label as set in the config. I propose something like a set_label helper function similar to the set_value function.

This is how I'd like it to work:

Controller:
Code:
$this->form_validation->set_rules(array(
   array('field' => 'username', 'label' => $this->lang->line('username'), 'rules' => 'required'),
   array('field' => 'password', 'label' => $this->lang->line('password'), 'rules' => 'required'),

View:
Code:
<?= form_open('login') ?>
<p>&lt;?= form_label(set_label('username'), 'username') ?&gt;
&lt;?= form_input('username', set_value('username'))</p>

<p>&lt;?= form_label(set_label('password'), 'password') ?&gt;
&lt;?= form_password('password') ?&gt;</p>
&lt;/form&gt;

Or something like that.
#3

[eluser]James Gifford[/eluser]
I modified the form_helper.php and Form_validation.php files to add the set_label function. Its pretty much the same as the set_value function.

form_helper:
Code:
/**
* Set Label
*
* If Form Validation is active it retrieves the info from the validation class
*
* @access    public
* @param    string
* @return    mixed
*/
if ( ! function_exists('set_label'))
{
    function set_label($field = '', $default = '')
    {
        if (FALSE === ($OBJ =& _get_validation_object()))
        {
            return '';
        }

        return form_prep($OBJ->set_label($field, $default));
    }
}

form_validation:
Code:
/**
* Get the value for a form label
*
* Permits you to populate a form label with the value set in form_validation
* or, if that value doesn't exist, with the default
*
* @access    public
* @param    string    the field name
* @param    string
* @return    void
*/    
function set_label($field = '', $default = '')
{
     if ( ! isset($this->_field_data[$field]))
     {
         return $default;
     }
    
     return $this->_translate_fieldname($this->_field_data[$field]['label']);
}

EDIT: I should mention that I've also modified Form_validation.php by removing the first few lines of the set_rules() method which ignores the rules of there is no post data.

Of course, if there's an easier way to get the label value that would be even better.
#4

[eluser]B___[/eluser]
Awesome thanks! I will try this out! Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB