zoukankan      html  css  js  c++  java
  • django form组件自定义验证

    1.自定义正则表达式

    password = django_fields.RegexField(
            '^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$\%^&*()])[0-9a-zA-Z!@#$\%^&*()]{8,32}$',
            min_length=12,
            max_length=32,
            error_messages={'required': '密码不能为空.',
                            'invalid': '密码必须包含数字,字母、特殊字符',
                            'min_length': "密码长度不能小于8个字符",
                            'max_length': "密码长度不能大于32个字符"}
        )

    2.clean_%s

        def clean_email(self):
            if 'email' in self.cleaned_data:
                if models.UserInfo.objects.filter(email=self.cleaned_data['email']) is None:
                    raise ValidationError(message='邮箱已经存在')

    在类中自定义以clean_%s的方法(以上是验证邮箱是否已经存在)

    3.clean

        def clean(self):
    
            if 'password' in self.cleaned_data and 'confirm_pwd' in self.cleaned_data:
                v1 = self.cleaned_data['password']
                v2 = self.cleaned_data['confirm_pwd']
                print(v1,v2)
                if v1 == v2:
                    pass
                else:
                    from django.core.exceptions import ValidationError
                    raise ValidationError('密码输入不一致')
            else:
                print('格式错误')

    联合验证,错误信息在form.errors["__all__"],templates中{{ result.form.non_field_errors.0}}

  • 相关阅读:
    左划删除
    UILabel 添加图片
    Swift-11-委托模式
    Swift-11-协议(Protocols)
    Swift-10--错误处理
    Swift-09-可空链式调用(Optional Chaining)
    Swift-08-闭包引起的循环强引用
    Swift-07-析构器deinit
    Swift-06-闭包
    【转】HTML5标签使用的常见误区
  • 原文地址:https://www.cnblogs.com/shuzhixia/p/11681606.html
Copyright © 2011-2022 走看看