jQuery验证插件远程规则修改php函数值



我有一个"编辑用户表单",以便在需要时更改用户数据。其中一个字段是id号。除了Codeigniter的表单验证库,我还使用了jQuery验证插件。

我有这个php函数在我的管理检查是否为用户输入的id已经存在于数据库上。如果id与用户存储在数据库中的id匹配,则没问题。但是当尝试输入另一个用户的id时,就会出现错误。如果试图输入一个id,它没有存储在数据库中,那么它应该是ok的,不应该显示错误。

我为此构建了一个php函数,它工作得很好。问题是当做jquery函数调用php函数。

视图如下:

<div class="span6">
  <div class="control-group">
    <label class="control-label">N&uacute;mero<span class="required">*</span></label>
      <div class="controls">
        <input type="text" name="documentn" id="documentn" class="m-wrap span8" value="{$frontuser->document}" required/>
            <span class="help-block"></span>
      </div>
  </div>
</div>
下面是jquery函数:
documentn: {
  required: true,
  minlength: 7,
  maxlength: 20,
  remote: {
  url: '/admin/checkDocAndUser',
  type: 'POST',
   },  
 },

下面是php函数:

public function checkDocAndUser(){
    if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
        $n = $_POST['documentn'];
        $id = $_POST['id'];
        $dn = UserManager::getInstance()->checkUserDocument($n,$id);
        if ($dn) {
            //id belongs to the user
            echo "true";
        }else{
            //does the id entered belongs to another user?
            $exists = UserManager::getInstance()->getByDocument($_POST['documentn']);
            if (!$exists) {
                // number entered belongs to another user
                echo "false";
            }
        }
    }
}

所以给定php函数工作时,我不使用js验证文件…为什么jquery函数不工作?

我已经尝试改变远程的规则url: admin/checkDocAndUser,/checkDocAndUser, checkDocAndUser,但不工作。

编辑和工作

考虑了@Sparky所说的,现在我发送用户id

documentn: {
  required: true,
  minlength: 7,
  maxlength: 20,
  remote: {
    url: '/admin/checkDocAndUser',
    type: 'POST',
    data: {
     id: function(){
     var dn = $('#id').val();
     return dn;
    }                                
   } 
  }
},
public function checkDocAndUser(){
    if ((isset($_POST['documentn'])) && (isset($_POST['id']))) {
        $dn = UserManager::getInstance()->checkUserDocument($_POST['documentn'],$_POST['id']);
        if ($dn) {
            //id belongs to the user
            echo "true";
        }else{
            //does the id entered belong to another user?
            $exists = UserManager::getInstance()->getByDocument($_POST['documentn']);
            if (!$exists) {
                // number entered belongs to another user
                echo "true";
            }else{
                echo "false";
            }
        }
    }
}

1)验证页面上是否包含jQuery验证插件。它需要包含在jQuery库之后。

2)在您的PHP函数中,您似乎正在寻找两个 POST输入:if ((isset($_POST['documentn'])) && (isset($_POST['id'])))。然而,默认情况下,remote方法只发送来自您正在验证的字段documentn的数据。如果您需要发送来自另一个字段的值,那么您需要使用remote中的data选项来配置它…

documentn: {
    required: true,
    // minlength: 7,     // <- can be replaced with rangelength rule
    // maxlength: 20,    // <- can be replaced with rangelength rule
    rangelength: [7,20], // <- can be used in place of two rules
    remote: {            // <- by default, only posts data from 'documentn' field       
        url: '/admin/checkDocAndUser',
        type: 'POST',
        data: {          // <- also post data from another field(s)
            anotherfield: function() {  // <- post name, $_POST['anotherfield']
                return $( "#another_field" ).val();
            }
        }
    }  
},

3)我不确定你的checkDocAndUser函数的逻辑。当它不属于其他用户并且它存在时会发生什么?在这种情况下,你没有重复任何东西。当验证应该通过时,remote方法需要true;当验证应该失败时,falseundefinednull。如果它回显一个JSON字符串,那么它将失败,并且该字符串将成为错误消息。检查你的逻辑是否按照文档的要求运行。

参见docs: http://jqueryvalidation.org/remote-method/

最新更新