air_wang 发表于 2009-3-25 20:29:51

form_validation的callback问题

本帖最后由 air_wang 于 2009-3-25 21:43 编辑

view下的form.php中:
<input type="text" name="idno" id="idno" value="<?php echo set_value('idno'); ?>" size="20" maxlength="30" />
controller中:
$this->load->library('form_validation');
$this->form_validation->set_rules('idno', '身份证号码', 'callback_id_check');
if ($this->form_validation->run() == FALSE)
{
   $this->load->view('form');
}
else
{
   $this->load->view('formsuccess');
}
}
    function id_check(){
    if($str = '123'){
    $this->form_validation->set_message('id_check', 'The %s field can not be the word "123"');
    return false;
    }else{
    return true;
    }
    }
我在“idno”的文本框中输入“123”,没有反应,好像没有调用函数id_check。
单位马上就要开始招生了,我初学想用CI做招生系统,没想到在用户注册这里就卡壳了,请教各位,以上代码哪里有错。

moorland 发表于 2009-3-25 21:46:23

RE: form_validation的callback问题

view下的代码应该写成:

<?php
$attributes = array('name' => 'testform');
echo form_open('home/valid/',$attributes); //home 是一个controller的名字,你根据你的程序修改
?>
<input type="text" name="idno" id="idno" value="<?php echo set_value('idno'); ?>" size="20" maxlength="30" />
   <div id="mes_erreur"><?php echo form_error('idno');?></div> 这里是为了显示错误信息的
</p>
<p>
    <label>
      <input type="submit" name="send" id="send" value="Envoyer" />
      
    </label>
      </form>
</p>

Controller下的代码:

$this->load->helper('form');$this->load->library('form_validation');
这两行你加在适当的位置 

function valid(){ $this->form_validation->set_rules('idno', 'Idno', 'required|callback_idno_check');if ($this->form_validation->run() == FALSE){ $this->load->view('form');}else{$this->load->view('formsuccess');}


} function idno_check($str){    if($str == '123'){    $this->form_validation->set_message('idno_check', 'The %s field can not be the word "123"');    return false;}else{   return true;}
}

air_wang 发表于 2009-3-25 22:19:09

我也是完全这么做的啊,输入"123"以后页面直接跳到formsuccess去了,没有输出想要的错误提示,还请2楼指教。

Hex 发表于 2009-3-25 22:27:24


function idno_check(){
改成
function idno_check($str){
试试

必须传递一个参数给回调函数。

air_wang 发表于 2009-3-25 22:38:31

谢谢两位,问题依旧啊,我的代码如下:
        function reg(){
        $this->load->helper('form');
        $this->load->library('form_validation');
       
            
        $this->form_validation->set_rules('idno', 'idno', 'required|callback_idno_check');
        if ($this->form_validation->run($rules) == FALSE){
                $this->load->view('reg.html');
        }
        else{
                $this->load->view('regsuccess.html');
        }
       function idno_check($str)
{
if ($str == 'test')
{
   $this->form_validation->set_message('idno_check', 'The %s field can not be the word "test"');
   return FALSE;
}
else
{
   return TRUE;
}
}
        }

air_wang 发表于 2009-3-25 22:42:39

不知各位在实际项目中有没有成功使用过form_validation的callback,我现在想extends类CI_Form_validation和form_validation_lang.php,但是还是不清楚callback的工作原理,仍然没有成功。

air_wang 发表于 2009-3-25 22:45:08

顺便贴上我的扩展代码:
MY_Form_validation类
class MY_Form_validation extends CI_Form_validation{
        public function MY_Form_validation(){
                parent::CI_Form_validation();
        }
        public function idno_check($str)
       {
          if ($str == 'test')
          {
           return FALSE;
          }
          else
          {
           return TRUE;
          }
       }
}
form_validation_lang.php
$lang['idno_check']        = "The %s field can not be the word 'test'"

air_wang 发表于 2009-3-25 22:56:56

问题基本解决,虽然算不上圆满,解决方法就是使用扩展类,刚才不行的原因是我吧rule还是写成了callback_idno_check了,忘记去掉callback_了,就是不知道为什么照着手册的方法不能成功,希望老大指教!

moorland 发表于 2009-3-25 23:05:34

你不应该把你的function idno_check($str)
放在function reg() 里边啊

手册上的没有什么错误,不用扩展类一样可以做到
我刚刚给你的代码是经过测试的,你要仔细看看红色的部分

forme的开头必须写成这样才能得到想要的结果
<?php
$attributes = array('name' => 'testform');
echo form_open('home/valid/',$attributes); //home 是一个controller的名字,你根据你的程序修改
?>

moorland 发表于 2009-3-25 23:07:11

我也是完全这么做的啊,输入"123"以后页面直接跳到formsuccess去了,没有输出想要的错误提示,还请2楼指教。
air_wang 发表于 2009-3-25 22:19 http://codeigniter.org.cn/forums/images/common/back.gif

<form action=",,,,"method="post">改成
<?php
$attributes = array('name' => 'testform');
echo form_open('home/valid/',$attributes); //home 是一个controller的名字,你根据你的程序修改
?>
页: [1] 2 3
查看完整版本: form_validation的callback问题