【问题标题】:Codeigniter input type text array validationCodeigniter 输入类型文本数组验证
【发布时间】:2015-06-14 07:55:18
【问题描述】:

HTML 查看表单

<div class="form-group">
            <div class="col-lg-4">
                <input type="text" name="keywords[]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[]'); ?>" placeholder="Tag or Keyword" />
            </div>
            <div class="col-lg-4">
                    <input type="text" name="keywords[]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[]'); ?>" placeholder="Tag or Keyword" />
            </div>
            <div class="col-lg-4">
                <input type="text" name="keywords[]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[]'); ?>" placeholder="Tag or Keyword" />
           </div>
          <div class="col-lg-4">
                <input type="text" name="keywords[]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[]'); ?>" placeholder="Tag or Keyword" />
        </div>
          <div class="col-lg-4">
                <input type="text" name="keywords[]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[]'); ?>" placeholder="Tag or Keyword" />
            </div>
            <div class="col-lg-4">
                <input type="text" name="keywords[]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[]'); ?>" placeholder="Tag or Keyword" />
            </div>
        </div>

在我的控制器中

$this->form_validation->set_rules('keywords[]', 'Keyword or Tags', 'xss_clean|alpha_dash');

实际问题, 以上所有关键字数组的输入文本都不是强制性的(控制器中没有应用必需的规则),但如果用户输入的不是alpha_dash,Codeigniter 应该只显示该字段的验证错误。它不应该显示空白字段的错误。

如果我写一个回调,我该如何管理return FALSE and error message for particular text box

我怎样才能做到这一点?有什么建议么? 谢谢

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我认为通过为关键字添加索引可以做到这一点。但您需要为每种输入类型指定单独的验证。

    <div class="form-group">
                <div class="col-lg-4">
                    <input type="text" name="keywords[0]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[0]'); ?>" placeholder="Tag or Keyword" />
                </div>
                <div class="col-lg-4">
                        <input type="text" name="keywords[1]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[1]'); ?>" placeholder="Tag or Keyword" />
                </div>
                <div class="col-lg-4">
                    <input type="text" name="keywords[2]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[2]'); ?>" placeholder="Tag or Keyword" />
               </div>
              <div class="col-lg-4">
                    <input type="text" name="keywords[3]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[3]'); ?>" placeholder="Tag or Keyword" />
            </div>
              <div class="col-lg-4">
                    <input type="text" name="keywords[4]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[4]'); ?>" placeholder="Tag or Keyword" />
                </div>
                <div class="col-lg-4">
                    <input type="text" name="keywords[5]" class="form-control input-sm" tagchecker="alphanumeric" value="<?php echo set_value('keywords[5]'); ?>" placeholder="Tag or Keyword" />
                </div>
            </div>
    

    在控制器中。

    $this->form_validation->set_rules('keywords[0]', 'Keyword or Tags', 'xss_clean|alpha_dash');
    $this->form_validation->set_rules('keywords[1]', 'Keyword or Tags', 'xss_clean|alpha_dash');
    $this->form_validation->set_rules('keywords[2]', 'Keyword or Tags', 'xss_clean|alpha_dash');
    $this->form_validation->set_rules('keywords[3]', 'Keyword or Tags', 'xss_clean|alpha_dash');
    $this->form_validation->set_rules('keywords[4]', 'Keyword or Tags', 'xss_clean|alpha_dash');
    $this->form_validation->set_rules('keywords[5]', 'Keyword or Tags', 'xss_clean|alpha_dash');
    

    我认为它会解决您的问题。

    【讨论】:

      猜你喜欢
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2013-01-25
      • 2020-09-01
      • 1970-01-01
      • 2012-01-02
      相关资源
      最近更新 更多