[Cubicweb] On-the-fly modification of forms

Sylvain Thénault sylvain.thenault at logilab.fr
Wed Aug 22 14:15:25 CEST 2012


On 22 août 14:03, Stéphane Bugat wrote:
> 
> > Huum, that's because you also have to override
> > editforms.CreationFormView.
> > 
> >   class AirportSearchCreationFormView(AirportSearchEditionFormView):
> >     __select__ = CreationFormView.__select__ &
> >     specified_etype_implements('AirportSearch')
> > 
> > should be enough.
> 
> Well, unfortunately it is not... Even with this mod I still get a 'normal' creation form view and my boolean field is not added at all.

Weird. Much probably a selection pb. Could you send us code for your class hierarchy + __select__?

> BTW, the 'form.append' method did not work, but I discovered some better methods like insert_field_before, so I modified my init_form like this:
> 
>     def init_form(self, form, entity):
>         oneway = ff.BooleanField(
>                 label=_('Search type: '),
>                 choices=(u'single airport', u'trip'),)
>         form.insert_field_before(oneway, u'airport_a_eid', 'subject')
>         airport_a_field = form.field_by_name(u'airport_a_eid', 'subject')
>         airport_a_field.widget = fw.LazyRestrictedAutoCompletionWidget(
>                 autocomplete_initfunc=get_airports)

right, didn't checked this but a form fields should be "edited" using
append_field, insert_field_before, insert_field_after, remove_field.

> As mentioned, the boolean field is not shown in the creation form. However, I did create an AirportSearch object and tried to modify it afterwards, but I just got an error: "TypeError: sequence item 1: expected string or Unicode, NoneType found" for "/usr/lib/python2.6/dist-packages/cubicweb/web/views/forms.py, line 244, function build_context".
> 
> Any idea about that?

Yep. That's because when creating a field then adding it to a form as you're
doing it, you must provide name='something' to the field's constructor. I'll
add an assertion for this trying to make it clearer.

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