HTML select Tag Title using Codeigniter 3

时间:2016-04-04 17:20:46

标签: php html codeigniter

I'm using Codeigniter 3.
The countries select box is an optional field on a form:

<select name="countries">
    <option>Choose a Country</option>
    <option value="en">England</option>
    <option value="fr">France</option>
    <option value="de">Germany</option>
</select>

I have the following line for the countries field validation:

$this->form_validation->set_rules('year',  'Year',    'trim|integer');

When I submit the form with the 1st option (Choose a Country), even without a value defined the validator sees something like this:

echo '<pre>';
print_r($this->input->post('year'));
echo '</pre>';

>> Choose a Country

.. when I am expecting something like false or null.
How can I make CI ignore the value when there isn't one selected?

Thanks in advance!

1 个答案:

答案 0 :(得分:0)

首先,您粘贴错误的行,我认为它应该是cbIniValues而不是countries

你可以通过以下方式实现:

  1. 为默认选择提供可以捕获的值。并在值等于year
  2. 时进行处理
    none
    1. 更改仅允许列表中的选项的验证规则。如果列表中的值不返回,则会出错。
    2. <select name="countries">
          <option value="none">Choose a Country</option>
          <option value="en">England</option>
          <option value="fr">France</option>
          <option value="de">Germany</option>
      </select>