我在完成视图后编写了我的控制器代码:
关于选择菜单的一些问题:
如何验证选择菜单 - 我不希望他们能够选择“请选择”
我还可以使用$this->form_validation->set_rules('','','required');
如何将正确答案发送到控制器?
<label for="hostingRequired">Hosting Required:</label>
<select name="hostingRequired">
<option value="pleaseSelect"> Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<label for="domainRequired">Domain Registration: </label>
<select name="domainRequired">
<option value="pleaseSelect">Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div id="domainToBeReged">
<label for="domainToBeReged">Domain:</label><input name="domainToBeReged" type="text" placeholder="http://www." />
<label for="domainToBeReged0">Domain:</label><input name="domainToBeReged0" type="text" placeholder="http://www." />
</div>
答案 0 :(得分:1)
您可以执行以下操作
然后使用以下验证规则
$this->form_validation->set_rules('','','required|callback_is_default');
function is_default($array)
{
foreach($array as $element)
{
if($element == 'pleaseSelect')
{
return FALSE;
}
}
return TRUE;
}
答案 1 :(得分:0)
如果您没有找到所需的验证规则,请扩展验证库并编写您自己的规则。 Look example如何做到这一点。
答案 2 :(得分:0)
在验证规则中添加:
$this->form_validation->set_rules('hostingRequired','Hosting Required','required|callback__check_select_yes_or_no');
$this->form_validation->set_rules('domainRequired','Domain registration','required|callback__check_select_yes_or_no');
此外,将此附加方法作为验证规则回调添加到控制器:
function _check_select_yes_or_no($str)
{
if ($str != 'yes' && $str != 'no')
{
$this->form_validation->set_message('_check_select_yes_or_no', 'Please select yes or no for the %s field');
return FALSE;
}
else
{
return TRUE;
}
}
这适用于“是”或“否”选项 - 如果您想添加更多调整回调,或者如果用户选择“请选择”,则将检查反转为失败。
或者,更好的方法是使用单选按钮“是”和“否”,并在表单加载时没有“选中”。
<fieldset><legend> Hosting required:</legend>
<label><input type="radio" name="hostingRequired" value="yes">Yes</label><br />
<label><input type="radio" name="hostingRequired" value="no">No</label>
</fieldset>
答案 3 :(得分:0)
正确的做法如下:
所以这里是代码
<label for="hostingRequired">Hosting Required:</label>
<select name="hostingRequired">
<option value=""> Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<label for="domainRequired">Domain Registration: </label>
<select name="domainRequired">
<option value="">Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<div id="domainToBeReged">
<label for="domainToBeReged">Domain:</label><input name="domainToBeReged" type="text" placeholder="http://www." />
<label for="domainToBeReged0">Domain:</label><input name="domainToBeReged0" type="text" placeholder="http://www." />
</div>
。
$this->form_validation->set_rules('hostingRequired','HostingRequired','required');
$this->form_validation->set_rules('domainRequired','DomainRequired','required');