class AgentForm(formencode.Schema): allow_extra_fields = True extension = formencode.All(UniqueAgent(), validators.NotEmpty())
class PbxBlacklistedForm(formencode.Schema): allow_extra_fields = True cid_number = formencode.All(validators.NotEmpty(),\ validators.MinLength(10),\ validators.String(not_empty=True),\ validators.MaxLength(10))
class QueueForm(formencode.Schema): allow_extra_fields = True name = formencode.All(UniqueQueue(), validators.NotEmpty()) strategy = validators.String(not_empty=True)
def test_config_option_with_validator(): v = fev.NotEmpty() opt = app.ConfigOption('test1', str, None, validator=v) assert_equal(opt.validate('val'), 'val') assert_raises(fev.Invalid, opt.validate, None) assert_raises(fev.Invalid, opt.validate, '')
class fields(WidgetsList): """This WidgetsList is just a container.""" title = TextField(validator=validators.NotEmpty()) year = TextField(size=4, validator=validators.Int())
class ShortURLAdminController(DefaultAdminController): shorturl_validators = All( validators.NotEmpty(), validators.Regex( r'^[-_a-zA-Z0-9]+$', messages={ 'invalid': 'must include only letters, numbers, dashes and underscores.' })) @expose() def index(self, **kw): redirect(request.referer or '/') @without_trailing_slash @expose('json:') def remove(self, shorturl, **kw): require_access(self.app, 'update') ShortUrl.query.remove({ 'app_config_id': self.app.config._id, 'short_name': shorturl }) return dict(status='ok') @expose('jinja:forgeshorturl:templates/form.html') @validate( dict(full_url=All(validators.URL(add_http=True), validators.NotEmpty()), short_url=shorturl_validators)) def add(self, short_url='', full_url='', description='', private='off', update=False, **kw): if update: require_access(self.app, 'update') else: require_access(self.app, 'create') if request.method == 'POST': if c.form_errors: error_msg = 'Error: ' for msg in list(c.form_errors): names = {'short_url': 'Short url', 'full_url': 'Full URL'} error_msg += '%s: %s ' % (names[msg], c.form_errors[msg]) flash(error_msg, 'error') redirect(request.referer or '/') shorturl = ShortUrl.query.find({ 'app_config_id': self.app.config._id, 'short_name': short_url }).first() if shorturl is not None: if not update: flash('Short url %s already exists' % short_url, 'error') redirect(request.referer or '/') else: msg = ('update short url %s from %s to %s' % (short_url, shorturl.full_url, full_url)) flash("Short url updated") else: shorturl = ShortUrl() shorturl.created = datetime.utcnow() shorturl.app_config_id = self.app.config._id msg = 'create short url %s for %s' % (short_url, full_url) flash("Short url created") shorturl.short_name = short_url shorturl.full_url = full_url shorturl.description = description shorturl.create_user = c.user._id shorturl.private = private == 'on' shorturl.last_updated = datetime.utcnow() M.AuditLog.log(msg) redirect(request.referer or '/') return dict(app=self.app, url_len=len(ShortUrl.build_short_url(c.app, short_name='')))
import tw2.core as twc import tw2.forms as twf from formencode.compound import All from formencode import validators name_validator = All(validators.NotEmpty(), validators.UnicodeString(), validators.Regex(r'[A-Za-z]')) phone_validator = All(validators.NotEmpty(), validators.UnicodeString(), validators.Regex(r'[0-9]')) class SubmitForm(twf.Form): class child(twf.TableLayout): nome = twf.TextField(size=15, validator=name_validator) telefono = twf.TextField(size=15, validator=phone_validator) submit = twf.SubmitButton(value='Submit') action = '/save'