(2017-8-6 17:47:18) 表单验证 02
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web import io import re class Ipfile: REGULAR='.*' # 正则表达式 # regular="^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" def __init__(self,error_dict=None,required=True): #requried 表示不能为空 self.error_dict={} if error_dict: self.error_dict.update(error_dict) # 更新错误表 self.required=required #true #传入到对象当中 self.error=None self.is_valid=False self.value=None def validate(self,name,input_value): if not self.required : # 如果是not true就不能通过,是not false的时候就能,即 required = false #可以为空 print('self.required', self.required) # testttttttttttt self.is_valid=True self.value=input_value # 值封装在里面 # 这里应该有些问题 else: #否则就是 required = True if not input_value.strip(): if self.error_dict.get('required',None): # 如果能获取到值 # 找是否有填错误信息 self.error=self.error_dict['require'] # 就直接等于给的值 else: self.error = "%s is valid" % name else: ret = re.match(Ipfile.REGULAR, input_value) if ret: self.is_valid=True self.value=input_value else: ret=re.match(Ipfile.REGULAR,input_value) #跟给定正则匹配 print('ret', ret) # testttttttttttt if ret: #匹配成功 self.is_valid=True self.value=input_value #匹配成功就用用户输入的值赋值给类里面的value else: if self.error_dict.get('valid',None): # 如果有提供错误信息 self.error=self.error_dict['valid'] else: self.error="%s is valid" % name class Baseform(object): def __init__(self): pass # self.host = "(.*)" # self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" # self.port = '(\d+)' # self.phone = '^1[3|4|5|8][0-9]\d{8}$' def check_valid(self, handle): flag=True value_dict={} error_message_dict={} success_value_dict={} for key, regular in self.__dict__.items(): # regular 继承前是正则表达式 继承后是对象ipfile key = 'ip' print('regular', regular) # testttttttttttt input_value=handle.get_argument(key) #获取用户输入的值 print('input_value',input_value) #testttttttttttt regular.validate(key, input_value) # 执行ipfile if regular.is_valid: success_value_dict[key]=regular.value print('success_value_dict', success_value_dict) # testttttttttttt else: error_message_dict[key]= regular.error print('error_message_dict',error_message_dict) #testttttttttttt flag=False value_dict[key]=input_value return flag,success_value_dict,error_message_dict class Homeform(Baseform): def __init__(self): # self.ip=Ipfile(required=True) self.phone=Ipfile(required=True) class IndexHandler(tornado.web.RequestHandler): def get(self): # callback=self.get_argument('callback') # self.write('%s ([11,22,33]);' % callback) self.render('index.html') def post(self, *args, **kwargs): # obj=MainForm() # is_valid,value_dict=obj.check_valid(self) # if is_valid: # print(value_dict) pass class Homehandler(tornado.web.RequestHandler): def get(self): self.render('home.html') def post(self): obj=Homeform() is_valid,success_dict,error_dict=obj.check_valid(self) print('error_dict',error_dict) #testttttttttttt if is_valid: print('sccess',success_dict) else: print('error',error_dict) self.render('home.html') # print(is_valid,success_dict,error_dict) settings = { 'template_path': 'views', 'static_path': 'statics', 'static_url_prefix': '/statics/', } application = tornado.web.Application([ (r"/index", IndexHandler), (r"/home", Homehandler), ], **settings) if __name__ == "__main__": application.listen(8088) tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web import io import re class Phonefile: REGULAR='^1[3|4|5|8][0-9]\d{8}$' # 正则表达式 \d{8}代表9个数字 # regular="^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" def __init__(self,error_dict=None,required=True): #requried 表示不能为空 self.error_dict={} if error_dict: self.error_dict.update(error_dict) # 更新错误表 self.required=required #true #传入到对象当中 self.error=None self.is_valid=False self.value=None def validate(self,name,input_value): if not self.required : # 如果是not true就不能通过,是not false的时候就能,即 required = false #可以为空 print('self.required', self.required) # testttttttttttt self.is_valid=True self.value=input_value # 值封装在里面 else: #否则就是 required = True if not input_value.strip(): # input_value.strip() 如果不为空 if self.error_dict.get('required',None): # 如果能获取到值 # 找是否有填错误信息 self.error=self.error_dict['require'] # 就直接等于给的值 else: self.error = "%s is valid" % name else: ret = re.match(Phonefile.REGULAR, input_value) if ret: self.is_valid=True self.value=input_value else: ret=re.match(Phonefile.REGULAR,input_value) #跟给定正则匹配 print('ret', ret) # testttttttttttt if ret: #匹配成功 self.is_valid=True self.value=input_value #匹配成功就用用户输入的值赋值给类里面的value else: if self.error_dict.get('valid',None): # 如果有提供错误信息 self.error=self.error_dict['valid'] else: self.error="%s is valid" % name class Baseform(object): def __init__(self): pass # self.host = "(.*)" # self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" # self.port = '(\d+)' # self.phone = '^1[3|4|5|8][0-9]\d{8}$' def check_valid(self, handle): flag=True value_dict={} error_message_dict={} success_value_dict={} for key, regular in self.__dict__.items(): # regular 继承前是正则表达式 继承后是对象ipfile key = 'ip' print('regular', regular) # testttttttttttt input_value=handle.get_argument(key) #获取用户输入的值 print('input_value',input_value) #testttttttttttt regular.validate(key, input_value) # 执行ipfile if regular.is_valid: success_value_dict[key]=regular.value print('success_value_dict', success_value_dict) # testttttttttttt else: error_message_dict[key]= regular.error print('error_message_dict',error_message_dict) #testttttttttttt flag=False value_dict[key]=input_value return flag,success_value_dict,error_message_dict class Homeform(Baseform): def __init__(self): # self.ip=Ipfile(required=True) self.phone=Phonefile(required=True) class IndexHandler(tornado.web.RequestHandler): def get(self): # callback=self.get_argument('callback') # self.write('%s ([11,22,33]);' % callback) self.render('index.html') def post(self, *args, **kwargs): # obj=MainForm() # is_valid,value_dict=obj.check_valid(self) # if is_valid: # print(value_dict) pass class Homehandler(tornado.web.RequestHandler): def get(self): self.render('home.html') def post(self): obj=Homeform() is_valid,success_dict,error_dict=obj.check_valid(self) print('error_dict',error_dict) #testttttttttttt if is_valid: print('sccess',success_dict) else: print('error',error_dict) self.render('home.html') # print(is_valid,success_dict,error_dict) settings = { 'template_path': 'views', 'static_path': 'statics', 'static_url_prefix': '/statics/', } application = tornado.web.Application([ (r"/index", IndexHandler), (r"/home", Homehandler), ], **settings) if __name__ == "__main__": application.listen(8088) tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web import io import re class Phonefile: REGULAR = '^1[3|4|5|8][0-9]\d{8}$' # 正则表达式 \d{8}代表9个数字 # regular="^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" def __init__(self, error_dict=None, required=True): # requried 表示不能为空 self.error_dict = {} if error_dict: # 如果 用户给出了值 self.error_dict.update(error_dict) # 就更新表 self.required = required # true #传入到对象当中 self.error = None self.is_valid = False self.value = None def validate(self, name, input_value): if not self.required: # 可以为空 # 如果是not true就不能通过,是not false的时候就能,即 required = false self.is_valid = True # 表示匹配 self.value = input_value # 值封装在里面 else: # 否则就是不能为空 required = True if not input_value.strip(): # 用input_value.strip()判断是否为空 如果为空 if self.error_dict.get('required', None): # 如果给了值中有required # 找是否有填错误信息 self.error = self.error_dict['require'] # 就直接等于给的值 else: # 如果required获取不到值 self.error = "%s is valid" % name # 错误信息就用这个 else: # input_value.strip() 如果不为空 ret = re.match(Phonefile.REGULAR, input_value) # 不为空的话就进行匹配 if ret: # 如果匹配成功 self.is_valid = True # 就是正明是有效的 self.value = input_value # 值就等于用户输入的值 else: # 如果匹配失败 if self.error_dict.get('valid', None): # 如果有提供错误信息 self.error = self.error_dict['valid'] #把用户提供的错误信息加到 self.error_dict['valid']中 else: #没有错误信息的话就用默认的 self.error = "%s is valid" % name class Baseform(object): def __init__(self): pass # self.host = "(.*)" # self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" # self.port = '(\d+)' # self.phone = '^1[3|4|5|8][0-9]\d{8}$' def check_valid(self, handle): flag = True value_dict = {} error_message_dict = {} success_value_dict = {} for key, regular in self.__dict__.items(): # regular 继承前是正则表达式 继承后是对象ipfile key = 'ip' # self.__dict__.items() 能获取类中的所有self.xx=xxx 的键值对 input_value = handle.get_argument(key) # 获取用户输入的值 regular.validate(key, input_value) # 执行ipfile 中的validate方法 if regular.is_valid: #如果regular.is_valid为True success_value_dict[key] = regular.value #在成功里面写值 else: #若果regular.is_valid为False error_message_dict[key] = regular.error flag = False value_dict[key] = input_value return flag, success_value_dict, error_message_dict class Homeform(Baseform): def __init__(self): # self.ip=Ipfile(required=True) self.phone = Phonefile(required=True) # 继承Baseform 并且改动其init方法 class IndexHandler(tornado.web.RequestHandler): def get(self): # callback=self.get_argument('callback') # self.write('%s ([11,22,33]);' % callback) self.render('index.html') def post(self, *args, **kwargs): # obj=MainForm() # is_valid,value_dict=obj.check_valid(self) # if is_valid: # print(value_dict) pass class Homehandler(tornado.web.RequestHandler): def get(self): self.render('home.html') def post(self): obj = Homeform() is_valid, success_dict, error_dict = obj.check_valid(self) if is_valid: print('sccess', success_dict) else: print('error', error_dict) self.render('home.html') # print(is_valid,success_dict,error_dict) settings = { 'template_path': 'views', 'static_path': 'statics', 'static_url_prefix': '/statics/', } application = tornado.web.Application([ (r"/index", IndexHandler), (r"/home", Homehandler), ], **settings) if __name__ == "__main__": application.listen(8088) tornado.ioloop.IOLoop.instance().start()
(2017-8-10 09:37:16)
#!/usr/bin/env python # -*- coding:utf-8 -*- import tornado.ioloop import tornado.web import io import re class Phonefilled: REGULAR = '^1[3|4|5|8][0-9]\d{8}$' # 正则表达式 \d{8}代表9个数字 # regular="^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" def __init__(self, error_dict=None, required=True): # requried 表示不能为空 self.error_dict = {} if error_dict: # 如果 用户给出了值 self.error_dict.update(error_dict) # 就更新表 self.required = required # true #传入到对象当中 self.error = None self.is_valid = False self.value = None def validate(self, name, input_value): if not self.required: # 可以为空 # 如果是not true就不能通过,是not false的时候就能,即 required = false self.is_valid = True # 表示匹配 self.value = input_value # 值封装在里面 else: # 否则就是不能为空 required = True if not input_value.strip(): # 用input_value.strip()判断是否为空 如果为空 if self.error_dict.get('required', None): # 如果给了值中有required # 找是否有填错误信息 self.error = self.error_dict['require'] # 就直接等于给的值 else: # 如果required获取不到值 self.error = "%s is valid" % name # 错误信息就用这个 else: # input_value.strip() 如果不为空 ret = re.match(Phonefilled.REGULAR, input_value) # 不为空的话就进行匹配 if ret: # 如果匹配成功 self.is_valid = True # 就是正明是有效的 self.value = input_value # 值就等于用户输入的值 else: # 如果匹配失败 if self.error_dict.get('valid', None): # 如果有提供错误信息 self.error = self.error_dict['valid'] #把用户提供的错误信息加到 self.error_dict['valid']中 else: #没有错误信息的话就用默认的 self.error = "%s is valid" % name class Checkboxfilled(Phonefilled): #直接继承 __init__方法相同 def validate(self, name, input_value): if not self.required: self.is_valid=True # 如果 required是false 的话就直接验证有效 self.value=input_value #直接让用户输入的值 赋值给类的value else: if not input_value: #若果没有输入的值 # is_valid=false if self.error_dict.get('required',None): #如果用户写错误提示 self.error= self.error_dict['required'] #那就让错误提示为用户写的 else: #如果用户没写错误提示,就用默认的 self.error = "%s is valid" % name else: # 如果用户有输入值 self.is_valid =True self.value = input_value class Baseform(object): def __init__(self): pass # self.host = "(.*)" # self.ip = "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$" # self.port = '(\d+)' # self.phone = '^1[3|4|5|8][0-9]\d{8}$' def check_valid(self, handle): flag = True value_dict = {} error_message_dict = {} success_value_dict = {} for key, regular in self.__dict__.items(): # regular 继承前是正则表达式 继承后是对象ipfile key = 'ip' # self.__dict__.items() 能获取类中的所有self.xx=xxx 的键值对 if type(regular)==Checkboxfilled: input_value = handle.get_arguments(key) # 获取用户输入的值 else: input_value = handle.get_argument(key) # 获取用户输入的值 regular.validate(key, input_value) # 执行ipfile 中的validate方法 if regular.is_valid: #如果regular.is_valid为True success_value_dict[key] = regular.value #在成功里面写值 else: #若果regular.is_valid为False error_message_dict[key] = regular.error flag = False value_dict[key] = input_value return flag, success_value_dict, error_message_dict class Homeform(Baseform): def __init__(self): # self.ip=Ipfile(required=True) self.phone = Phonefilled(required=True) # 继承Baseform 并且改动其init方法 self.favor=Checkboxfilled(required=True) class IndexHandler(tornado.web.RequestHandler): def get(self): # callback=self.get_argument('callback') # self.write('%s ([11,22,33]);' % callback) self.render('index.html') def post(self, *args, **kwargs): # obj=MainForm() # is_valid,value_dict=obj.check_valid(self) # if is_valid: # print(value_dict) pass class Homehandler(tornado.web.RequestHandler): def get(self): self.render('home.html',error_dict='') def post(self): obj = Homeform() is_valid, success_dict, error_dict = obj.check_valid(self) # 获取错误信息 if is_valid: print('sccess', success_dict) else: print('error', error_dict) self.render('home.html',error_dict=error_dict) # print(is_valid,success_dict,error_dict) settings = { 'template_path': 'views', 'static_path': 'statics', 'static_url_prefix': '/statics/', } application = tornado.web.Application([ (r"/index", IndexHandler), (r"/home", Homehandler), ], **settings) if __name__ == "__main__": application.listen(8088) tornado.ioloop.IOLoop.instance().start()