首页 \ 问答 \ JQuery自定义验证错误(JQuery custom validation error)

JQuery自定义验证错误(JQuery custom validation error)

我使用JQuery验证器面临以下问题:我有一个表单,我使用提供的默认验证规则,一切正常。 但是,只要我添加自定义验证,就会抛出以下错误:

  1. 在Chrome中:未捕获TypeError:无法调用未定义的方法'addMethod'
  2. 在IE8中:'validator'为null或不是对象
  3. FireFox:没有错误消息,但JQuery默认验证逻辑中断;

导致错误发生的代码是这个(它位于ready函数内):

$().validator.addMethod("defaultInvalid", 
   function(value, element) {   
    alert("validate");
   }
 );

如您所见,我已经评论了回调函数的大部分代码,以消除任何其他错误。 如果我发表评论,一切都会恢复正常。 但是,我想在文本框上使用此自定义规则,必须具有默认值“名字”,并且在提交时,不应允许用户将“名字”作为值发送。


I am facing the following issue with the JQuery validator: I have a form where I am using the default validation rules provided and everything works fine. Howoever, as soon as I add a custom validation, the following error is thrown:

  1. in Chrome: Uncaught TypeError: Cannot call method 'addMethod' of undefined
  2. in IE8: 'validator' is null or not an object
  3. FireFox: no error message, but the JQuery default validation logic breaks;

The piece of code which is causing the error to occur is this (it sits inside the ready function):

$().validator.addMethod("defaultInvalid", 
   function(value, element) {   
    alert("validate");
   }
 );

As you see I have commented most of the code of the callback function in order to eliminate any other error. if I comment it, everything goes back to normal. However, I would like to use this customized rule on a text box taht must have the default value of "First name" and on submit it should not allow the user to send "First name" as value.


原文:https://stackoverflow.com/questions/14626467
更新时间:2021-07-25 11:07

最满意答案

这不是很简单,但我能够解决它。 我从文件中导入了这个我的clean_sessions():

from importlib import import_module
from django.conf import settings

然后,在函数内部,我加载了SessionStore对象:

SessionStore = import_module(settings.SESSION_ENGINE).SessionStore

从那里,很容易删除会话,留下这样的方法:

def clean_sessions():
    stored_sessions = Session.objects.all()
    active_users = Request.objects.active_users(seconds=15)
    active_users_ids = [user.id for user in active_users]
    for session in stored_sessions:
        SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
        s = SessionStore(session_key=session.session_key)
        session_uid = session.get_decoded().get('_auth_user_id')
        if not session_uid:
            s.delete()
            continue
        if session_uid not in active_users_ids:
            ## some code ##
            s.delete()

从你正在使用的任何会话引擎加载正确的SessionStore非常重要,否则它将无法从两个地方(DB和缓存)中删除它。


It wasn't very straightforward but I was able to fix it. I imported this from the file I have my clean_sessions():

from importlib import import_module
from django.conf import settings

Then, inside the function, I loaded the SessionStore object:

SessionStore = import_module(settings.SESSION_ENGINE).SessionStore

From there, it was very easy to remove the sessions, leaving the method like this:

def clean_sessions():
    stored_sessions = Session.objects.all()
    active_users = Request.objects.active_users(seconds=15)
    active_users_ids = [user.id for user in active_users]
    for session in stored_sessions:
        SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
        s = SessionStore(session_key=session.session_key)
        session_uid = session.get_decoded().get('_auth_user_id')
        if not session_uid:
            s.delete()
            continue
        if session_uid not in active_users_ids:
            ## some code ##
            s.delete()

It's very important to load the right SessionStore from whatever session engine you're using, otherwise it will fail to remove it from both places (DB and cache).

相关问答

更多
  • 我目前使用django-redis作为Redis的缓存后端。 到目前为止,我还没有使用过django-redis-cache,但是让我决定使用django-redis的原因如下: 模块化客户端系统(可插拔客户端)。 一些可插拔的客户端开箱即用(碎片客户端,牧群客户端等) 主从支持默认客户端。 原始访问Redis客户端/连接池的设施(非常有用)。 更好地记录。 在django-redis文档网站上 ,您可以找到更多考虑它的理由。 根据我的经验,我可以告诉我的是,我对django-redis非常满意:)祝你好运 ...
  • Redis是存储会话的理想选择。 所有操作都在内存中执行,因此读取和写入将会很快。 第二个方面是持续的会话状态。 Redis为您提供了如何将会话状态保留到硬盘的灵活性。 您可以通过http://redis.io/topics/persistence了解更多信息,但在高层次,您可以选择这些选项 - 如果您无法承受丢失任何会话, appendfsync always在配置文件中设置appendfsync always 。 因此,Redis保证任何写入操作都保存到磁盘。 缺点是写操作会更慢。 如果你失去了大约1年 ...
  • Redis的这个Python模块在自述文件中有一个明确的用法示例: http : //github.com/andymccurdy/redis-py Redis被设计为RAM缓存。 它支持基本的GET和SET键,以及诸如字典的集合的存储。 您可以通过将其输出存储在Redis中来缓存RDBMS查询。 目标是加快您的Django网站。 在您需要速度之前,不要开始使用Redis或任何其他缓存 - 不要过早优化。 This Python module for Redis has a clear usage exam ...
  • 我正在使用Django 1.9和django-redis版本4.3 ,我已将我的缓存设置为使用RedisCache : CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.clien ...
  • 这不是很简单,但我能够解决它。 我从文件中导入了这个我的clean_sessions(): from importlib import import_module from django.conf import settings 然后,在函数内部,我加载了SessionStore对象: SessionStore = import_module(settings.SESSION_ENGINE).SessionStore 从那里,很容易删除会话,留下这样的方法: def clean_sessions(): ...
  • 我很久以前就找到了解决我所有问题的方法,因为有人投了票,我想他们可能需要一个答案,所以我在这里发布一个答案。 服务器/ server.js var redis = require("redis"); var session = require('express-session'); var redisStore = require('connect-redis')(session); app.use(session({ secret: 'hello', store: new redisS ...
  • 是的,它们可以用于会话和队列。 Laravel使用不同的redis对象来存储会话数据,缓存数据和队列数据。 您应该将队列命名为不同的名称。 此外,在redis中,延迟队列数据和直接队列数据之间存在分离。 Laravel会话扫描不会触及队列数据。 Yes they can be used for both sessions and queues. Laravel uses different redis objects for storing session data, cache data and queu ...
  • Redis是存储会话的快速而快捷的方式,而不是在SQL DB中添加它们(尝试监视查询会话表所花费的时间),如果您使用django-toolbar app之类的工具,这一点显而易见。 另一方面,唯一可能显示为问题的是会话数据持久性,Redis已经使用2种不同的方法处理,您可以根据需要选择,请参阅文档了解更多详情,以及制作群集或故障转移Redis服务器的选项。 我遇到了类似的情况,我需要将我的会话存储在NoSQL数据库中以获得速度和其他与项目相关的原因,这是因为我使用了django-redisession应用程 ...
  • django-redis只为你提供了一个redis 缓存后端 : django-redis是Django的BSD许可,全功能redis缓存/会话后端。 使用redis-py,你可以与redis服务器“对话”,它是一个python redis接口。 据我所知,问题是你想如何直接通过界面或使用django的缓存系统与redis进行交互。 如果您希望此数据“过期”或者您希望使用redis缓存其他实体,或者您希望以redis存储会话 - 请使用django-redis。 另外,直接使用redis-py或同时使用re ...
  • 是否有一个好的经验法则可以使用哪一个? 没有。 Cached_db似乎永远是一个更好的选择...... 没关系。 在某些情况下,有许多Django(和Apache)进程查询公共数据库。 mod_wsgi以这种方式允许很多可伸缩性。 缓存没有多大帮助,因为会话是在Apache(和Django)进程之间随机分布的。 是否可以同时使用,会话在浏览器关闭时到期并给出年龄? 不明白为什么不。 什么被认为是“不活跃”? 我假设你在开玩笑。 “活动”是 - 好 - 活动。 你懂。 发生在Django的东西。 Django ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。