Re: Formulator nice-to-have features

Josef Meile <[email protected]> Fri, 24 Nov 2006 16:41:53 +0100
Newsgroups gmane.comp.web.zope.formulator.devel
Message-ID <[email protected]>
> After having worked with Formulator, I needed some features that it
> doesn't have that perhaps would be nice to include in the official
> release.
> 
> 1) i18n_domain for each field: [...]
> 
> 2) group property for each field: [...]
> 
> 3) Dynamically setting properties while rendering a form. [...]
I forgot another feature:
4) Introduce a render_label method in order to distinguish required
fields in a rendered form. This is how I use it:

<table tal:define="reqMark python:'<b class=\'color1\'>*&nbsp;;</b>'">
   <tr tal:define="userName python:form.get_field('UserName')"
     <td>
       <div tal:replace="structure 
python:userName.render_label(here,reqMark)"/>:
     </td>
     <td>
       <div tal:replace="structure 
python:userName.render_from_request(request)"/>
       </td>
   </tr>
</table>


So, this will render as:
<table>
   <tr>
     <td>
       <b class="color1">*&nbsp;;</b>User:
     </td>
     <td>
       <input type="text" name="UserName">
     </td>
   </tr>
</table>

So, you won't have to use a "tal:condition" directive to check wheter or
not the field is required. Now the implementation details:

from zope.i18n import translate as _
def render_label(self, context, reqMark = u'(*)&nbsp;'):
     fieldTitle = self.get_value('title')
     if self.is_required():
         #Here you have to translate the fieldTitle MessageId; otherwise,
         #you will get the reqMark plush the original message
         context = getattr(context,'REQUEST',context)
         fieldTitle = _(fieldTitle, context = context)
         fieldTitle = reqMark + fieldTitle

     return fieldTitle