[Cubicweb] On-the-fly modification of forms

St├ęphane Bugat stephane.bugat at free.fr
Wed Aug 22 11:17:46 CEST 2012


> > Following the example given in
> > http://docs.cubicweb.org/devweb/edition/examples.html, I'd like to
> > modify the default FormView for the AirportSearch entity. First,
> > there is some slight differences with the given example derived
> > from ChangeStateFormView: for entity edition it seems that I have
> > to overwrite the EditionFormView class of
> > cubicweb.web.views.editforms, is that correct?
> 
> yep

Thanks for confirmation

> 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')

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)

Thans a lot



More information about the Cubicweb mailing list