没有实际表单的CodeIgniter Form_Validation

时间:2022-10-06 16:42:16

what I am trying to do is pretty simple (I hope):
I am trying to validate POST data (or any data provided via variable) with Form_Validation.
The problem is that to run actual validation I/we need to do

我想要做的很简单(我希望):我正在尝试使用Form_Validation验证POST数据(或通过变量提供的任何数据)。问题是要运行我/我们需要做的实际验证

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

my set of rules are like this:

我的规则是这样的:

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

so, I am sending data via AJAX to this script obviously

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

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

How can I validate $_POST['key'] using form_validation?

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

AJAX code

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 code //simplified

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;
        }

so I have some (generated) inputs, on change I want to make update in DB only if ..set_rules passes.
if input is empty it deletes itself (this works fine)

SOLUTION
TRUE/FALSE table is here

所以我有一些(生成的)输入,如果..set_rules通过,我想在DB中进行更新。如果输入为空则删除自身(这很好)解决方案TRUE / FALSE表在这里

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

if there is parameter to pass for example min_length[2];

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

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

2 个解决方案

#1


2  

See this. Also check out the note below on that page.

看到这个。另请查看该页面上的注释。

#2


3  

It depends on the version of codeigniter that you are using

这取决于您使用的codeigniter的版本

codeigniter 3.0 :

codeigniter 3.0:

you can use set_data method to validate your custom data

您可以使用set_data方法验证自定义数据

$data = array(
        'username' => 'johndoe',
        'password' => 'mypassword',
        'passconf' => 'mypassword'
);

$this->form_validation->set_data($data);

codeigniter 2.X :

codeigniter 2.X:

set $_POST values manually

手动设置$ _POST值

   $_POST'username']  = 'johndoe';
   $_POST['password'] ='mypassword';
   $_POST['passconf'] = 'mypassword';

Common :

常见:

for the common part thankfully they both still use form_validation->run() and form_validation->set_rules(<SPECIFIC ATTRIBUTE RULES>)

为了共同的部分,谢天谢地他们仍然使用form_validation-> run()和form_validation-> set_rules(

#1


2  

See this. Also check out the note below on that page.

看到这个。另请查看该页面上的注释。

#2


3  

It depends on the version of codeigniter that you are using

这取决于您使用的codeigniter的版本

codeigniter 3.0 :

codeigniter 3.0:

you can use set_data method to validate your custom data

您可以使用set_data方法验证自定义数据

$data = array(
        'username' => 'johndoe',
        'password' => 'mypassword',
        'passconf' => 'mypassword'
);

$this->form_validation->set_data($data);

codeigniter 2.X :

codeigniter 2.X:

set $_POST values manually

手动设置$ _POST值

   $_POST'username']  = 'johndoe';
   $_POST['password'] ='mypassword';
   $_POST['passconf'] = 'mypassword';

Common :

常见:

for the common part thankfully they both still use form_validation->run() and form_validation->set_rules(<SPECIFIC ATTRIBUTE RULES>)

为了共同的部分,谢天谢地他们仍然使用form_validation-> run()和form_validation-> set_rules(