dongmuyuan3046 2016-06-03 12:39
浏览 54

Form_validation始终显示错误

I'm using Codeigniter, and I'm trying to make form_validation work.

Yesterday I started to check some of the forms of my website, using the codeigniter default rules, and for some, using my owns with callbacks. I tested every forms yesterday and it was working, and today, I worked on some other forms, which aren't working, and I wanted to test the one from yesterday and they now don't work too, and I don't understand why.

One of my form :

<form id="form_group_data" name="form_group_data"  method="post" class="form-horizontal" action="<?php echo site_url('cms/cms/add')?>">
        <p>
        <div class="form-body">
           <div class="form-group">
              <label class="col-md-2 control-label" for="cms"><?php echo lang('nomcms'); ?><span class="required"> * </span></label>   
              <div class="col-md-4">
                 <input id="cms" class="form-control" type="text" rows="5" name="cms" required="required" value="">
              </div>
           </div>
           <div class="form-group">
              <label class="col-md-2 control-label" for="telephone"><?php echo lang('phone'); ?><span class="required"> * </span></label>   
              <div class="col-md-4">
                 <input id="telephone" class="form-control" placeholder="<?php echo lang('exemple_number'); ?>" type="text" rows="5" name="telephone" required="required" value="">
              </div>
           </div>
           <div class="form-group">
              <label class="col-md-2 control-label" for="email"><?php echo lang('email'); ?><span class="required"> * </span></label>   
              <div class="col-md-4">
                 <input id="email" class="form-control" type="text" rows="5" name="email" required="required" value="">
              </div>
           </div>
           <div class="form-group">
              <label class="col-md-2 control-label" for="id_localite"><?php echo lang('localite'); ?><span class="required"> * </span></label>   
              <div class="col-md-4">
                 <?php echo form_dropdown('id_localite', $id_localite, null,'class="form-control" required="required"')?>
              </div>
           </div>
           <div class="form-group">
              <label class="col-md-2 control-label" for="id_langue"><?php echo lang('langue'); ?><span class="required"> * </span></label>   
              <div class="col-md-4">
                 <?php echo form_dropdown('id_langue', $id_langue, null,'class="form-control" required="required"')?>
              </div>
           </div>
           <div class="form-group">
              <label class="control-label col-md-2"><?php echo lang('active'); ?></label>
              <div class="col-md-9">
                 <input type="checkbox" checked="checked" name="actif" class="make-switch" data-on-text="&nbsp;<?php echo lang('actif'); ?>&nbsp;&nbsp;" data-off-text="&nbsp;<?php echo lang('inactif'); ?>&nbsp;" data-on-color="info" data-off-color="danger"> 
              </div>
           </div>
        </div>
        <input type="submit" value="<?php echo lang('add'); ?>" class="btn blue"/>
        <button type="button" class="btn default" onclick="window.location='<?php echo site_url('cms/cms')?>'"><?php echo lang('cancel'); ?></button>
  </div>
  </p>
  </form>  

The form validation :

$this->form_validation->set_rules('cms', 'cms', 'trim|required|xss_clean|callback_customAlphaNum');
        $this->form_validation->set_rules('telephone', 'telephone', 'trim|required|xss_clean|callback_customNatural|exact_length[12]');
        $this->form_validation->set_rules('email', 'email', 'trim|required|xss_clean|valid_email');
        $this->form_validation->set_rules('id_localite', 'id_localite', 'trim|required|xss_clean');
        $this->form_validation->set_rules('id_langue', 'id_langue', 'trim|required|xss_clean');

        // callback function
        function customAlphaNum($str) 
        {
            if ( !preg_match('/^[a-z0-9 -]+$/i',$str) )
            {
                return false;
            }
        }
        function customNatural($str) 
        {
            if ( !preg_match('/^[0-9+]+$/i',$str) )
            {
                return false;
            }
        }

        // custom error message
        $this->form_validation->set_message('customAlphaNum', lang('form_validation_customAlphaNum'));
        // custom error message
        $this->form_validation->set_message('customNatural', lang('form_validation_customNatural'));

if ($this->form_validation->run() == FALSE)
        {
            $this->insert();
        }
        else
        {

        ...

        }

Now whatever I write in my fields triggers the error, even if it follows the rule I created. Actually, it triggers every callback I created, but not the default rule from codeigniter (like valid_email, required, etc.) so it is a problem with my callbacks, but yesterday I had no issue. Does anyone know what could cause this issue ?

  • 写回答

1条回答 默认 最新

  • douba9020 2016-06-04 00:45
    关注

    Is it this part:

    if ($this->form_validation->run() == FALSE)
            {
                $this->insert();
            }
    

    You are saying - if the form validation was FALSE then do the insert. Thats probably the opposite of what you want. AND @elddenmedio is correct add return true to your callbacks. see example: http://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料