示例#1
0
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())
示例#2
0
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()
示例#3
0
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'
    ]
示例#4
0
文件: forms.py 项目: moinwiki/moin
        """
        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.
示例#5
0
class LoginForm(Form):
    username = String.using(label='Username', validators=[Present()])
    password = String.using(label='Password', validators=[Present()])
示例#6
0
class EditLessonForm(Form):
    lesson_name = String.using(label='Lesson Name', validators=[Present()])
    text = String.using(label='Text', validators=[Present()])
示例#7
0
    class MyForm(Form):
        x = Integer.using(validators=[Present()])

        d2 = Dict.of(Integer.named('x2').using(validators=[Present()]))
示例#8
0
 class MyForm(Form):
     x = Integer.using(validators=[Present()])
示例#9
0
class ThirtySomething(Age):
    validators = (Present(), Age.IsNumber(), Age.ValidAge(minage=30,
                                                          maxage=39))
示例#10
0
class AddEditClassForm(Form):
    name = "addClass"
    classname = String.using(label='Class Name', validators=[Present()])
    description = String.using(label='Description', validators=[Present()])