【问题标题】:Form validation on radio buttons Codeigniter单选按钮上的表单验证 Codeigniter
【发布时间】:2017-03-16 23:08:39
【问题描述】:

我正在尝试为 Codeigniter 中的 html 表单上的单选按钮添加验证。该表单预先填充了来自数据库的数据,包括已上传文档的列表(如果项目没有关联的文档,此字段可能为空)。用户可以上传新文档,将其添加到现有文档或添加新文档删除当前文档。

所以我有一个包含组织文档名称的文本字段和一个新的文件上传字段:

 <label for="orgdocs">Documents</label>
 <input type="text" id="orgdocs" readonly="readonly" value="<?php echo $fetched_row['pdocs']; ?>"  />
 <input type="file" id="newdocs" name="newdocs[]" multiple="multiple" />

和单选按钮:(忽略对西班牙名字的不良尝试)

 <label for="mas"><b>Añada mas</b></label>
<input type="radio" name="docsacion" style="margin-right: 0" <?php if (isset($docsacion) && $docsacion=="mas") echo "checked";?> value="mas" title="Add another document to existing docs"><br />
<label for="otra"><b>Borrar y añada otra</b></label>
<input type="radio" name="docsacion" style="margin-right: 0" <?php if (isset($docsacion) && $docsacion=="otra") echo "checked";?> value="otra" title="Remove all current docs and add new">

我只想添加验证。如果选择了新文档(newdocs 不为空),则需要 dosacion。 我试过了:

if(isset($_FILES['newdocs']['name']) && (!empty($_FILES['newdocs']['name'])))
{$this->form_validation->set_rules('docascion','Documentation upload', 'required');}

但是即使 newdocs 字段为空并且我不知道为什么,这也会产生错误!?

【问题讨论】:

  • 尝试 if ( !$this->upload->do_upload('newdocs',FALSE)){ //验证 }

标签: codeigniter validation radio-button forms


【解决方案1】:

试试这个

if(!empty($_FILES['newdocs']['name'])){
   $this->form_validation->set_rules('docascion','Documentation upload', 'required');
}

【讨论】:

  • 谢谢,但这完全一样。从逻辑上讲,它应该可以工作(我认为可以),但它不允许在 Newdocs 字段为空且未选择任何 docsacion 的情况下提交??
  • 这里没有什么是强制文件字段为非空的。此代码仅在选择文件时为您的radio 按钮设置验证。如果那里没有文件选择,则跳过验证
  • 但即使没有选择文件,提交时也会出现验证错误。 (即不跳过验证)
猜你喜欢
  • 2020-02-02
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 2018-07-13
相关资源
最近更新 更多