加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 没有实际表单的CodeIgniter Form_Validation

发布时间:2020-12-13 17:20:33 所属栏目:PHP教程 来源:网络整理
导读:我想做的很简单(我希望): 我正在尝试使用Form_Validation验证POST数据(或通过变量提供的任何数据). 问题是要运行我/我们需要做的实际验证 if ($this-form_validation-run() == FALSE){ //validation did not pass}else{ //validation did pass} 我的规则是
我想做的很简单(我希望):
我正在尝试使用Form_Validation验证POST数据(或通过变量提供的任何数据).
问题是要运行我/我们需要做的实际验证

if ($this->form_validation->run() == FALSE){
 //validation did not pass
}else{
 //validation did pass
}

我的规则是这样的:

$this->form_validation->set_rules('key','','trim|required'); //simplified

所以,我显然是通过AJAX向这个脚本发送数据

echo $_POST['key']; //prints valid value that is sent from AJAX

如何使用form_validation验证$_POST [‘key’]?

AJAX代码

$("#key-inputs .short,#key-inputs .long").change(function () {
        var key = $(this).val();
        var id = $(this).attr('name');
        var table = $(this).attr('class');
        var file = $('span .file').val();
        var data = 'NULL';
        $.post(file + "/edit",{
                        id:id,key:key,file:file,table:table},function(code,textStatus) {
                        data = code;
        });
        $(this).ajaxStop(function(){
            if (data == 3) $(this).parent().fadeOut('slow',function() {}); //delete
            if (data == 2) $(this).fadeOut(200).fadeIn(200); //update
            if (data == 5) alert('not valid'); //update parameter is not valid input (depends on set_value rules)
            //writeConsole(previous.val().outerHTML);
        });
    });

PHP代码//简化

if (!$this->input->is_ajax_request()) redirect('lang/translate/keys/'.$param);
          //do work here
         if ($key == "") {
          //delete key
            $this->lang_translate_model->DeleteKey($id,$table);
            echo 3;
            return TRUE;
         }
         $this->form_validation->set_rules('key','key','trim|required|max_length[56]|xss_clean|unique_file[lang_key:'.$param.']');
         if ($this->form_validation->run() == FALSE){
          //validation did not pass
          echo 5;
        }

所以我有一些(生成的)输入,如果..set_rules通过,我想在DB中进行更新.
如果输入为空它会删除自己(这很好)

TRUE / FALSE表是here

if ($this->form_validation->required($key)){
          //validation did not pass
          echo 5;
}

如果有参数传递,例如min_length [2];

$this->form_validation->min_length($key,'2');

解决方法

请参阅 this.另请参阅该页面上的注释.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读