class Age(Integer): class IsNumber(Converted): incorrect = u'%(label)s is not a valid number.' class ValidAge(Validator): minage = 1 maxage = 150 too_young = u'%(label)s must be at least %(minage)s.' too_old = u'%(label)s may not be larger than %(maxage)s' at_min = '%(label)s is at the minimum age.' at_max = '%(label)s is at the maximum age.' def validate(self, element, state): age = element.value if age < self.minage: return self.note_error(element, state, 'too_young') elif age == self.minage: return self.note_warning(element, state, 'at_min') elif age == self.maxage: return self.note_warning(element, state, 'at_max') elif age > self.maxage: return self.note_error(element, state, 'too_old') return True validators = (Present(), IsNumber(), ValidAge())
def test_validator_validated(): sentinel = [] def listener(**kw): sentinel.append(kw) signals.validator_validated.connect(listener) schema = String.using(validators=[Present(), Converted(), NoLongerThan(5)]) el = schema() assert not el.validate() eq_(sentinel, [ dict(sender=schema.validators[0], element=el, state=None, result=False) ]) del sentinel[:] el = schema(value='abcd') assert el.validate() assert len(sentinel) == 3 assert sentinel[-1]['result'] del sentinel[:] el = schema('squiznart') assert not el.validate() assert len(sentinel) == 3 assert not sentinel[-1]['result'] s2 = String.using(optional=False) del sentinel[:] el = s2() assert not el.validate() eq_(sentinel, [dict(sender=NotEmpty, element=el, state=None, result=False)]) del sentinel[:] el = s2('squiznart') assert el.validate() eq_(sentinel, [dict(sender=NotEmpty, element=el, state=None, result=True)]) del listener del sentinel[:] el = schema('squiznart') assert not el.validate() assert not sentinel signals.validator_validated._clear_state()
class SignupForm(Form): username = String.using(label='Username', validators=[Present()]) password = String.using(label='Password', validators=[ Present(), LengthBetween(5, 25), ValuesEqual('.', '../confirmPassword') ]) confirmPassword = String.using(label='Confirm Password', validators=[Present()]) email = String.using(label='Email', validators=[Present(), IsEmail()]) firstname = String.using(label='First Name', validators=[Present()]) lastname = String.using(label='Last Name', validators=[Present()]) output_schema = [ 'username', 'password', 'confirmPassword', 'email', 'firstname', 'lastname' ]
""" if sort_by is not None: choice_specs = sorted(choice_specs, key=itemgetter(sort_by)) else: choice_specs = list(choice_specs) return cls.valued(*[e[0] for e in choice_specs]).with_properties( choice_specs=choice_specs) Text = String.with_properties(widget=WIDGET_TEXT) MultilineText = String.with_properties(widget=WIDGET_MULTILINE_TEXT) OptionalText = Text.using(optional=True) RequiredText = Text.validated_by(Present()) OptionalMultilineText = MultilineText.using(optional=True) RequiredMultilineText = MultilineText.validated_by(Present()) class NameNotValidError(ValueError): """ The name is not valid. """ def validate_name(meta, itemid): """ Common validation code for new, renamed and reverted items.
class LoginForm(Form): username = String.using(label='Username', validators=[Present()]) password = String.using(label='Password', validators=[Present()])
class EditLessonForm(Form): lesson_name = String.using(label='Lesson Name', validators=[Present()]) text = String.using(label='Text', validators=[Present()])
class MyForm(Form): x = Integer.using(validators=[Present()]) d2 = Dict.of(Integer.named('x2').using(validators=[Present()]))
class MyForm(Form): x = Integer.using(validators=[Present()])
class ThirtySomething(Age): validators = (Present(), Age.IsNumber(), Age.ValidAge(minage=30, maxage=39))
class AddEditClassForm(Form): name = "addClass" classname = String.using(label='Class Name', validators=[Present()]) description = String.using(label='Description', validators=[Present()])