【问题标题】:How to re-populate the form when CodeIgniter form validation detects error?CodeIgniter 表单验证检测到错误时如何重新填充表单?
【发布时间】:2014-07-17 12:53:08
【问题描述】:

我想在 CodeIgniter 表单验证检测到错误时填充我的所有输入字段,

这是我的输入表单

<form class="form-horizontal" action="<?php echo base_url() ?>admin.php/country/add_country" method="post" enctype="multipart/form-data">
                          <fieldset>
                            <legend><?php if(isset($msg)) echo $msg;?>
                            <?php echo validation_errors(); ?></legend>
                            <div class="control-group">
                              <label class="control-label" for="typeahead">Country Name</label>
                              <div class="controls">
                                <input type="text" name="title" class="span6 typeahead" id="title"  data-provide="typeahead" data-items="4" value="<?php echo set_value('title');?>">
                              </div>
                            </div>
                            <div class="control-group">
                              <label class="control-label" for="typeahead">ISO2 Code</label>
                              <div class="controls">
                                <input type="text" name="iso2" class="span6 typeahead" id="iso2"  data-provide="typeahead" data-items="4" value="<?php echo set_value('iso2');?>" >
                              </div>
                            </div>
                                                        <div class="control-group">
                              <label class="control-label" for="typeahead">ISO3 Code</label>
                              <div class="controls">
                                <input type="text" name="iso3" class="span6 typeahead" id="iso3"  data-provide="typeahead" data-items="4" value="<?php echo set_value('iso3');?>">
                              </div>
                            </div>

                            <div class="form-actions">
                              <button type="submit" class="btn btn-primary">Save changes</button>
                              <button type="reset" class="btn">Cancel</button>
                            </div>
                          </fieldset>
                        </form>

这是我的 setup_rules:

$this->form_validation->set_rules('title', 'Country Name', 'required|min_length[5]|alpha');

现在当我遇到错误时,表单只填充值标题字段,而不填充其他字段。

【问题讨论】:

  • 如果您在其他字段上应用验证,它也适用于其他字段
  • 我不想验证其他字段
  • 如果您不想在 POST 中设置值进行验证

标签: php codeigniter validation


【解决方案1】:

如果您在其他字段上应用验证,那么它也适用于其他字段。

试试这样的。

$this->form_validation->set_rules('title', 'Country Name', 'required|min_length[5]|alpha');
$this->form_validation->set_rules('iso2', 'ISO2 Code', 'min_length[5]|alpha');// any validation you want.
$this->form_validation->set_rules('iso3', 'ISO3 Code', 'min_length[5]|alpha');// any validation you want.

【讨论】:

    猜你喜欢
    • 2010-11-10
    • 2010-12-30
    • 1970-01-01
    • 2012-01-25
    • 2012-07-12
    • 2011-02-13
    • 2013-02-28
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多