用于jquery和php验证的函数相同

用于jquery和php验证的函数相同,php,jquery,validation,jquery-validate,codeigniter-2,Php,Jquery,Validation,Jquery Validate,Codeigniter 2,所以我有这个问题。我正在进行服务器端验证和jquery验证 在服务器端验证中,我要做的是使用codeigniter的form_验证库,更具体地说: $this->form_validation->set_rules('documentn', 'Passport number', 'required|min_length[7]|max_length[20]|is_natural|callback_checkDocAndUser'); 需要返回true或false 我有这个用户编辑表单

所以我有这个问题。我正在进行服务器端验证和jquery验证

在服务器端验证中,我要做的是使用codeigniter的form_验证库,更具体地说:

$this->form_validation->set_rules('documentn', 'Passport number', 'required|min_length[7]|max_length[20]|is_natural|callback_checkDocAndUser');
需要返回true或false

我有这个用户编辑表单,用于更改用户数据。但是有一些限制。。。存储在数据库中的用户具有唯一的passport号码。如果这个护照号码错了,我需要能够更改它。。。但数据库中不应重复护照号码

这是从callback_checkDocAndUser调用的php函数:

public function checkDocAndUser(){
    if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
        $dn = UserManager::getInstance()->checkUserDocument($_POST['documentn'],$_POST['id']);
        if ($dn) {
            //passport belongs to the user
            echo "true";
            // return true;
        }else{
            //does the passport entered belong to another user?
            $exists = UserManager::getInstance()->getByDocument($_POST['documentn']);
            if (!$exists) {
                //passport belongs to another user
                echo "true";
                // return true;
            }else{
                                    //passport number is free to use
                echo "false";
                // return false;
            }
        }
    }
}
正如你所看到的,我在函数中加入了一些“echo”。这是因为我想对jQuery验证使用相同的函数(它需要echo,不适用于“return”)


那么,我如何在这两种验证中使用相同的函数。。。?有没有办法让jquery函数接收返回..或者让codeigniter函数接收回音?

验证函数总是需要返回一个
布尔值

Put an exit at the end of your function.

EDIT:

If you want to use the same set of validations for both client and server side validation, divide your call to two functions, one which handles client and the another which handles server. check the following code:
In you jquery function call url - admin/validate_form_client

function validate_form_client(){
   $op = $this->checkDocAndUser($_POST['documentn'],$_POST['id']);
   echo $op;
 exit;

}

function validate_form_server(){
   if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
    return $this->checkDocAndUser($_POST['documentn'],$_POST['id']);
}
}



public function checkDocAndUser($documentn,$id){

        $dn = UserManager::getInstance()->checkUserDocument($documentn,$id);
        if ($dn) {
            //id belongs to the user

            return true;
        }else{
            //does the id entered belong to another user?
            $exists = UserManager::getInstance()->getByDocument($documentn);
            if (!$exists) {
                // id number belongs to another user
                return "true";

            }else{
                                    //id number is free to use
                return "false";

            }
        }
    }
}

Note: the given function names are just for example. please follow standard practice in the variable and function naming conventions.

在控制器中,尝试检索验证方法的返回值,并在那里回显“true”或“false”。

我在CodeIgniter项目中也做了同样的事情,解决方案非常简单。下面的答案一般命名为只需要添加验证逻辑的地方

在验证代码不重复的地方,以及CodeIgniter结构之后,就有了这个答案

模型对CodeIgniter(服务器端)验证和jQuery验证(客户端)
远程
进行实际验证

// file name 'models/demo_model.php'

class Demo_model extends CI_Model {

    public function check_demo($params)
    {

        // insert your validation logic here...
        // the entirety of your validation logic, check the DB, etc.

        // if it passes validation
        return TRUE;

        // if it fails validation
        return FALSE;

    }

}
控制器仅由客户端
远程
调用,用于jQuery验证

// file name 'controllers/demo.php'

class Demo extends CI_Controller {

    public function remote_demo($params = FALSE)
    {

        // call the Model to do the actual validation
        $valid = $this->demo_model->check_demo($params);

        if ($valid)  
        {
            echo 'true';  // passes validation
        }
        else         
        {
            echo 'false'; // fails validation
        }

    }

}
仅用于CodeIgniter的服务器端验证

// file name 'libraries/MY_Form_validation.php'

class MY_Form_validation extends CI_Form_validation {

    public function __construct()
    {
        parent::__construct();
        $this->ci =& get_instance();
    }

    public function demo_check($params)
    {

        $this->ci->form_validation->set_message('demo_check', 'The %s is incorrect.');  

        // call the Model to do the actual validation
        return $this->ci->demo_model->check_demo($params);

    }

}
按照组织方式,您将有一个位于中心位置的验证函数与CodeIgniter模型驻留在一起。我选择模型作为中心位置,因为代码主要与数据库交互

以下两种验证使用相同的模型函数

  • 服务器端CodeIgniter验证:从
    MY_Form_validation
    调用模型,模型将
    根据CodeIgniter验证规则将
    返回
    FALSE
    到其他CodeIgniter控制器

  • 客户端jQuery验证
    远程
    :从控制器调用模型,模型将
    返回控制器。然后,控制器功能将基于模型的布尔响应
    echo
    true
    false


对不起,我想我不明白你的意思。。。如果我输入echo“true”或echo“false”,则它不适用于服务器端验证,但它适用于jquery验证在检查验证是否失败的地方,请自己尝试回显“true”或“false”。你永远不应该通过验证方法进行回音。对不起…还是不明白。你能把代码放在你的控制器中验证输入吗?哦,你是说查询的结果吗?是的,马上告诉我你指的是哪一个函数?在checkDocAndUser函数的末尾。一个示例代码片段将非常有助于解释你的答案。啊,你想做什么?
id
是函数还是
$('#id').val()的值?我猜是最后一个。那么为什么不干脆
id:$('#id').val()
?我稍后会检查这是否有效,但现在我更感兴趣的是我文章的真正问题。但是谢谢你的观察;)不清楚你想做什么。什么是
文档?它是某个更大物体的一部分。那个物体是什么?它是如何使用的?您正在使用
$.ajax()
?或者可能是
$.post()
?或者别的什么?documentn是护照号码(或用户id)。。。我正在使用远程规则..这是一个自定义规则,可以通过ajax检查数据库中是否存在特定数据。请注意,我提到函数工作正常并给出预期结果,但只有在我使用echo for js file并返回codeigniter的回调函数时,我的意思是
documentn
只是一个更大语句的片段。向我们展示整个语句。
if(passs)返回true;如果(失败)返回true?@nl-x,那是个打字错误。。。谢谢你让我知道。
// file name 'libraries/MY_Form_validation.php'

class MY_Form_validation extends CI_Form_validation {

    public function __construct()
    {
        parent::__construct();
        $this->ci =& get_instance();
    }

    public function demo_check($params)
    {

        $this->ci->form_validation->set_message('demo_check', 'The %s is incorrect.');  

        // call the Model to do the actual validation
        return $this->ci->demo_model->check_demo($params);

    }

}