[Cubicweb] On-the-fly modification of forms

Sylvain Thénault sylvain.thenault at logilab.fr
Wed Aug 22 11:33:00 CEST 2012


On 22 août 11:17, Stéphane Bugat wrote:
> > The EditionFormView class provides the init_form method for this
> > purpose (given
> > the selected form as argument, does nothing by default). So you can
> > override
> > it and add desired fields there, that should do the trick. At some
> > point, we
> > shall probably unify those get_form / init_form mecanism (my
> > preference going
> > to get_form which is the most flexible).
> 
> I've tried that by adding a simple BooleanField in init_form()... However this field is not displayed when adding an AirportSearch entity... Do I have to register it in some way? My FormView selection is done like this:
>     __select__ = EditionFormView.__select__ & is_instance('AirportSearch')

Huum, that's because you also have to override editforms.CreationFormView.

  class AirportSearchCreationFormView(AirportSearchEditionFormView):
    __select__ = CreationFormView.__select__ & specified_etype_implements('AirportSearch')

should be enough.

> Another question: is it also possible to modify the widget of one specific predefined field in the init_form method? I saw that it is possible to retrieve a field of the form by its name and role using field_by_name()::
> 
>     def init_form(self, form, entity):
>         oneway = ff.BooleanField(
>                 label=_('Search type: '),
>                 choices=(u'single airport', u'trip'),)
>         form.append(oneway)
>         airport_a_field = form.field_by_name(u'airport_a_eid', 'subject')
>         airport_a_field.widget = fw.LazyRestrictedAutoCompletionWidget(
>                 autocomplete_initfunc=get_airports)

yes, that should be fine.
-- 
Sylvain Thénault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (09.54.03.55.76)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
CubicWeb, the semantic web framework:    http://www.cubicweb.org



More information about the Cubicweb mailing list