Welcome Guest, Not a member yet? Register   Sign In
Form validation of drop down list
#1

[eluser]foysal[/eluser]
Hi all

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


I am using form_dropdown() in my view.




Foysal
#2

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

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

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


Thanks for reply.


Foysal
#4

[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.
#5

[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.
#6

[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...
#7

[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');
#8

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

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB