def EmailValidator(node, value): """ Validator which succeeds if the email does not exist. Can be used for the email input field in a sign up form. """ # validate email format Email()(node, value) if IsReservedUserName(value): err = _( u"Email '${name}' already in use. Please choose a different email.", mapping={'name': value}) raise Invalid(node, err) # lookup email in database r = node.widget.form.context.root() u = r.Select(pool_type=u"user", parameter={u"email": value}, fields=[u"id", u"name", u"email"], max=2, operators={u"email": u"="}) if not u: u = r.Select(pool_type=u"user", parameter={u"name": value}, fields=[u"id", u"name", u"email"], max=2, operators={u"name": u"="}) if u: # check if its the current user ctx = node.widget.form.context if len(u) == 1 and ctx.id == u[0][0]: return err = _( u"Email '${name}' already in use. Please choose a different email.", mapping={'name': value}) raise Invalid(node, err)
def AcceptValidator(node, value): """ Validator which succeeds if the checkbox is ticked (true). """ if not value == True: err = _(u"Please accept the terms and conditions.") raise Invalid(node, err)
def test_validate_fails_widgeterror_and_schemaerror(self): from nive.components.reform.schema import Invalid fields = { 'name': 'Name', 'title': 'Title', } widget_invalid = Invalid(None, None, dict(fields)) schema_invalid = Invalid(None, None) schema = DummySchema(schema_invalid) field = self._makeOne(schema) field.widget = DummyWidget(exc=widget_invalid) e = validation_failure_exc(field.validate, fields) self.assertEqual(field.widget.error, schema_invalid) self.assertEqual(e.cstruct, dict(fields)) self.assertEqual(e.field, field) self.assertEqual(e.error, schema_invalid)
def OldPwValidator(node, value): """ Validator which succeeds if the current password matches. """ user = node.widget.form.view.User(sessionuser=False) if not user.Authenticate(value): err = _(u"The old password does not match.") raise Invalid(node, err)
def handle_error(self, field, error): msgs = [] if error.msg: field.error = error else: for e in error.children: msgs.append('%s' % e) field.error = Invalid(field.schema, '\n'.join(msgs))
def deserialize(self, field, pstruct, formstruct=None): error = None result = {} if pstruct is null: pstruct = {} for num, subfield in enumerate(field.children): name = subfield.name subval = pstruct.get(name, null) try: result[name] = subfield.deserialize(subval, pstruct) except Invalid, e: result[name] = e.value if error is None: error = Invalid(field.schema, value=result) error.add(e, num)
def deserialize(self, field, pstruct, formstruct=None): if pstruct in (null, self.placeholder): return null value = pstruct confirm = formstruct.get('%s-confirm' % (field.name,)) or '' setattr(field, '%s-confirm' % (field.name,), confirm) if (value or confirm) and (value != confirm): raise Invalid(field.schema, self.mismatch_message, value) if not value: return null return value
def PasswordValidator(node, value): """ Validator which succeeds if the username does not exist. Can be used for the name input field in a sign up form. """ Length(min=5, max=30)(node, value) chars = ''.join(set(value)) if len(chars) < 5: err = _( u"Password is too simple. It should have at least 5 different characters." ) raise Invalid(node, err)
def deserialize(self, field, pstruct, formstruct=None): if pstruct is null: return null if not pstruct.strip(): return null try: infile = StringIO.StringIO(pstruct) reader = csv.reader(infile) row = reader.next() except Exception, e: field.unparseable = pstruct raise Invalid(field.schema, str(e))
def RootnameValidator(node, value): """ Makes sure the new name does not exist. """ # lookup name in database app = node.widget.form.context.app for root in app.GetAllRootConfs(): if root.id == value: # check if its the context if app.root(root.id) != node.widget.form.context: err = _( u"'${name}' already in use. Please choose a different name.", mapping={'name': value}) raise Invalid(node, err)
def UsernameValidator(node, value): """ Validator which succeeds if the username does not exist. Can be used for the name input field in a sign up form. """ # lookup name in database r = node.widget.form.context.root() u = r.LookupUser(name=value, activeOnly=0) if u: # check if its the current user ctx = node.widget.form.context if len(u)==1 and ctx.id == u[0][0]: return err = _(u"Username '${name}' already in use. Please choose a different name.", mapping={'name':value}) raise Invalid(node, err)
def EmailValidator(node, value): """ Validator which succeeds if the email does not exist. Can be used for the email input field in a sign up form. """ # validate email format Email()(node, value) # lookup email in database r = node.widget.form.context.root() u = r.LookupUser(email=value, activeOnly=0) if u: # check if its the current user ctx = node.widget.form.context if len(u)==1 and ctx.id == u[0][0]: return err = _(u"Email '${name}' already in use. Please use the login form if you already have a account.", mapping={'name':value}) raise Invalid(node, err)
def deserialize(self, field, pstruct, formstruct=None): if formstruct and 'year' in formstruct and 'month' in formstruct and 'day' in formstruct: year = formstruct['year'].strip() month = formstruct['month'].strip() day = formstruct['day'].strip() if (not year and not month and not day): return null if self.assume_y2k and len(year) == 2: year = '20' + year result = '-'.join([year, month, day]) if (not year or not month or not day): raise Invalid(field.schema, _('Incomplete date'), result) elif pstruct is null: return null else: result = pstruct return result