CodeIgniter Forums

Full Version: Form validation of drop down list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]foysal[/eluser]
Hi all

How can i validate a drop down list in CI??


I am using form_dropdown() in my view.




Foysal

El Forum

[eluser]Piter[/eluser]
Is that it?

Code:
<?php echo form_dropdown('product_tax', $array, set_value('product_tax'), 'style="width:100px;"'); ?>

El Forum

[eluser]foysal[/eluser]
No, form_error('') is not working for that specific drop down list. But working for text boxes.


Thanks for reply.


Foysal

El Forum

[eluser]Konfine[/eluser]
I find that when I want to use say a selectbox as a required form item that you have to set a callback function.

You can read more about callback functions on the userguide.

El Forum

[eluser]LuckyFella73[/eluser]
- form_error('fieldname') works for dropdown fields as well
- you don't need callback functions in generall to set a "requiered" rule for dropdowns

@foysal
Please post you view code (at least the part where your dropdown is generated)
and the controller part where you set the rule for that dropdown.

El Forum

[eluser]Konfine[/eluser]
Would be interesting to see your code then LuckyFella73 as when using 'required' within the form_validation config file it never works unless I set a callback function to handle it.

I don't like use CI's form_dropdown function, I use my own:

Code:
function generate_dropdown_example($data = array(), $current = 0)
{
    $drop = '<select id="name" name="name" class="formitem">';
    $drop .= '<option value="all">please select</option>';
    
    foreach( $data as $row )
    {
        $s = ( $row['identifier'] == $current ) ? ' selected="selected"' : '';
        $drop .= '<option value="' . $row['identifier'] . '"' . $s . '>' . $row['visible_val'] . '</option>';
    }
    
    $drop .= '</select>';
    
    return $drop;
}

If you try using that code with the form validation library it never works...

El Forum

[eluser]LuckyFella73[/eluser]
Just set an option with value ='' as default row.

According to your example:
Code:
function generate_dropdown_example($data = array(), $current = 0)
{
    $drop = '<select id="name" name="name" class="formitem">';
    $drop .= '<option value="">please select</option>'; // removed value here
    
    foreach( $data as $row )
    {
        $s = ( $row['identifier'] == $current ) ? ' selected="selected"' : '';
        $drop .= '<option value="' . $row['identifier'] . '"' . $s . '>' . $row['visible_val'] . '</option>';
    }
    
    $drop .= '</select>';
    
    return $drop;
}

Now you can set the rule like usual:
Code:
$this->form_validation->set_rules('name', 'Dropdown', 'required');

El Forum

[eluser]Konfine[/eluser]
Interesting, thanks for the tip.

El Forum

[eluser]LuckyFella73[/eluser]
You are welcome!

El Forum

[eluser]foysal[/eluser]
Thanks all for reply.


So far validation problem is not solved. my view is as below

Code:
<tr><td id="td_left_event"><b>&lt;?php echo form_label('Event Time', 'event_time'); ?&gt;</b></td>
        <td id="td_right_event_1">&lt;?php echo form_dropdown('event_time', $data_for_event_time, $js); ?&gt;</td>
        <td>&lt;?php echo form_error('event_time')?&gt;</td>
</tr>


my code of controller is as below

Code:
$this->form_validation->set_rules('event_time', 'Event Time', 'required');


I am working with calender class. I fall a new problem also. I printed a calender in my view, the dates are clickable. If I click an individual date it will take me to another page. I would like to get the date at that page, but could not do that so far.



foysal