[Twisted-web] maxlength input with formless
Tommi Virtanen
tv at twistedmatrix.com
Sun Dec 5 01:05:43 MST 2004
Andrea Arcangeli wrote:
>How can I define a maxlength for my input fields in a form? Do I need to
>modify the formless module or is there a trick?
>
>
from formless import annotate
class MaxLenString(annotate.String):
maxlen = 100
def __init__(self, *args, **kwargs):
if 'maxlen' in kwargs:
self.maxlen = kwargs['maxlen']
del kwargs['maxlen']
super(MaxLenString, self).__init__(*args, **kwargs)
def coerce(self, val, configurable):
r = super(MaxLenString, self).coerce(val, configurable)
if len(r) > self.maxlen:
raise annotate.InputError, "%r is too long, maximum is %d." %
(val, self.maxlen)
return r
>Or can I use the css to set the maxlength based on the id? (I tried
>that, but I couldn't find a way to do it with the css yet ;)
>
>
I think that while you can use CSS to set the size of the input box,
there is
no CSS way to control the maximum number of characters one can input.
You can always override the renderer. I wish there was a nice way to not
reimplement
the whole function..
from nevow import flat
from formless import iformless
class MaxLenStringRenderer(BaseInputRenderer):
def input(self, context, slot, data, name, value):
if data.typedValue.getAttribute('hidden'):
T="hidden"
else:
T="text"
return slot[
input(id=keyToXMLID(context.key), type=T, name=name,
value=value, maxlength=self.maxlen,
_class='freeform-input-%s' % T)]
flat.registerFlattener(MaxLenStringRenderer, MaxLenString,
iformless.ITypedRenderer)
More information about the Twisted-web
mailing list