Codeigniter:下拉验证set_rules [英] Codeigniter: dropdown validation set_rules

查看:84
本文介绍了Codeigniter:下拉验证set_rules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何确保用户选择了博士,先生,女士,女士,当他们提交表格时,如果称呼为空白,它将为set_rules()返回一条错误消息。

may i know how do i ensure that the user have selected, Dr, Mr, Ms, Mdm and when they submit the form if the salutation is given as blank it will return an error message for the set_rules().

代码:

echo "<p>Salutation: ";
    $salutationOptions = array(
                  ''  => '',
                  'Dr'  => 'Dr',
                  'Mr'    => 'Mr',
                  'Ms'   => 'Ms',
                  'Mdm' => 'Mdm',
                );
    echo form_dropdown('salutation', $salutationOptions, '');
    echo "</p>";


推荐答案

在视图文件中,您可以进行客户端操作使用以下方法进行验证:

In the view file, you can do a client side validation using this:

echo "<p>Salutation: ";
 $salutationOptions = array(
                  ''  => '',
                  'Dr'  => 'Dr',
                  'Mr'    => 'Mr',
                  'Ms'   => 'Ms',
                  'Mdm' => 'Mdm',
                );
    echo form_dropdown('salutation', $salutationOptions, '', 'required="required"');
    echo "</p>";

当用户尝试提交而不选择下拉菜单时,将给他们一个错误,要求他们

When the user tries to submit without choosing form the dropdown, it will give them an error saying they should choose from the dropdown.

如果要在服务器端使用它,可以执行以下操作:

If you want it on the server side, you can do something like this:

$this->form_validation->set_rules('salutation', 'Salutation', 'required')
if($this->form_validation->run()){
    /*user has selected. use the data now*/
}else{
    /*user has not sleected data, throw error back*/
}

这篇关于Codeigniter:下拉验证set_rules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆