【问题标题】:Same function for jquery and php validationjquery 和 php 验证的相同功能
【发布时间】:2014-07-23 00:33:38
【问题描述】:

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

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

$this->form_validation->set_rules('documentn', 'Passport number', 'required|min_length[7]|max_length[20]|is_natural|callback_checkDocAndUser');

需要返回真或返回假。

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

这是从 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;
            }
        }
    }
}

如您所见,我在函数中添加了一些“回声”。这是因为我想使用相同的函数进行 jQuery 验证(需要回显,不能使用“return”)。

       documentn: {
                    required: true,
                    minlength: 7,
                    maxlength: 20,
                    remote: {
                        url: '/admin/checkDocAndUser',
                        type: 'POST',
                        data: {
                            id: function(){
                                return $('#id').val();
                            }                                
                        } 
                    }
                },

那么我怎样才能对这两种验证使用相同的功能...?有没有办法让jquery函数接收return..或codeigniter的函数接收echo?

【问题讨论】:

  • 呃,你想做什么? id 是函数还是 $('#id').val() 的值?我猜是最后一个那么为什么不只是id: $('#id').val() 呢?
  • 我稍后会检查这是否有效,但现在我对我的帖子的真正问题更感兴趣。但是感谢您的观察;)
  • 不清楚您要做什么。 documentn 是什么?它是某个更大对象的成员。那个对象是什么以及它是如何被使用的?你在用$.ajax()吗?或者$.post()?还是别的什么?
  • documentn 是护照号码(或用户 ID)...我正在使用远程规则 ..这是通过 ajax 检查数据库中是否存在特定数据的自定义规则。请注意,我提到函数工作正常并给出预期结果,但前提是我对 js 文件使用 echo 并返回 codeigniter 的回调函数
  • 我的意思是documentn 只是一个更大的声明中的一个sn-p。向我们展示整个声明。

标签: php jquery validation jquery-validate codeigniter-2


【解决方案1】:
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.

【讨论】:

  • 你指的是哪个函数?
  • checkDocAndUser 函数结束时。
  • 示例代码 sn-p 将极大地帮助您解释答案。
【解决方案2】:

验证函数总是需要返回一个boolean 值。

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

【讨论】:

  • 对不起,我想我不明白你的意思......如果我输入 echo "true" 或 echo "false",它不适用于服务器端验证,但它确实为 jquery 验证工作
  • 在您检查验证是否失败的地方,尝试自己回显“真”或“假”。你永远不应该通过验证方法来回应。
  • 抱歉……还是没听懂。
  • 您可以将代码发布到您验证输入的控制器中吗?
  • 哦,你的意思是查询的结果?是的,马上
【解决方案3】:

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

此答案遵循the DRY principle,您的验证代码不重复,以及 CodeIgniter 结构。

这个 MODELboth CodeIgniter(服务器端)验证和 jQuery Validate(客户端)remote...

进行实际验证
// 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;

    }

}

CONTROLLER client-side remote 调用以进行 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
        }

    }

}

LIBRARY用于 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 调用模型,模型将 return TRUEFALSE 根据您的 CodeIgniter 回到您的其他 CodeIgniter 控制器验证规则。

  • 客户端jQuery验证remote:从控制器调用模型,模型将returnTRUEFALSE返回控制器。然后控制器函数将根据来自模型的布尔响应echotruefalse

【讨论】:

  • if (passes) return true; if (fails) return true; ?
  • @nl-x,这是一个错字...谢谢你告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多