Skip to content

Commit

Permalink
Finished validation function
Browse files Browse the repository at this point in the history
  • Loading branch information
gomezjdaniel committed Jan 30, 2013
1 parent 084b1ce commit 3cdccd2
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions ngforms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import re

import webapp2
from webapp2_extras import json


class Form(object):
field_values = {}

def build(self):
fields = ''.join([f.build(self) for f in self.fields])
Expand All @@ -12,7 +16,21 @@ def build(self):
""" % fields

def validate(self):
pass
request = webapp2.get_request()
data = json.decode(request.body)

for f in self.fields:
vals = self.validations[f.id]

try:
value = data[f.id]
except KeyError:
value = ''

field_values[f.id] = value
for val in vals:
if not val.validate(self):
request.abort(403)

@property
def fields(self):
Expand All @@ -22,8 +40,8 @@ def fields(self):
def validations(self):
raise NotImplemented()

def field(self, name):
return ""
def field(self, id):
return field_values[id]


class Validation(object):
Expand Down Expand Up @@ -71,7 +89,7 @@ def __init__(self, message):
super(Email, self).__init__("email", message, {})

def validate(self, form):
return not re.match("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$",
return not re.match(r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$',
form.field(self.input)) is None


Expand Down Expand Up @@ -159,22 +177,3 @@ def build(self, form):
input = '<input%s>' % ''.join(input)

return tmpl % input

"""
class EditAccountForm(Form):
@property
def validations(self):
return {
"name": [Required("oops"), MinLength(4, "ops")],
}
@property
def fields(self):
return [
InputField(id="name", placeholder="test@example.com", type="email",
cls=["input-xlarge"], name="Nombre"),
]
print EditAccountForm().build()
"""

0 comments on commit 3cdccd2

Please sign in to comment.