-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started today, implemented some validations
- Loading branch information
1 parent
4dc64fa
commit cf111dd
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import re | ||
|
||
class Form(object) | ||
|
||
def build(self): | ||
|
||
def validate(self): | ||
|
||
def fields(self): | ||
raise NotImplemented() | ||
|
||
def validations(self): | ||
raise NotImplemented() | ||
|
||
|
||
# ---------------------------------------- | ||
|
||
class Validation(object) | ||
def __init__(self, name, message, attr): | ||
self.name = name | ||
self.message = message | ||
self.attr = attr | ||
|
||
def validate(self): | ||
raise NotImplemented() | ||
|
||
class MinLength(Validation) | ||
def __init__(self, min, message): | ||
self.min = min | ||
super(MinLength, self).__init__("MinLength", message, | ||
{"ng-minlength" : min}) | ||
|
||
def validate(self, value): | ||
return len(value) >= min | ||
|
||
class MaxLength(Validation) | ||
def __init__(self, max, message): | ||
self.max = max | ||
super(MaxLength, self).__init__("MaxLength", message, | ||
{"ng-maxlength" : max}) | ||
|
||
def validate(self, value): | ||
return len(value) =< max | ||
|
||
class Required(Validation) | ||
def __init__(self, field, message): | ||
self.field = field | ||
super(Required, self).__init__("Required", message, {"required"}) | ||
|
||
def validate(self, field): | ||
return len(field)>0 | ||
|
||
class Email(Validation) | ||
def __init__(self, email, message): | ||
self.email = email | ||
super(Email, self).__init__("Email", message, {}) | ||
|
||
def validate(self, email): | ||
if not re.match("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", email): | ||
return False | ||
else: | ||
return True | ||
|