[Erp5-dev] dynamic form fields - how to do?

Jérôme Perrin jerome at nexedi.com
Mon Mar 12 09:38:08 CET 2007


Le Vendredi 9 Mars 2007 11:56, Jacek Medrzycki a écrit :
> Hi
>
> I have the following problem:
>
> There is a field in the form, say "Recruitment type", which is a
> listfield with 2 values:
> External
> Internal
> (this is associated with recruitment_type property)
>
> Then, I have RelationStringField (for referral relation) that should,
> when clicked on the gear wheel:
> a) In Internal recruitment type, display a list of Persons,
> b) In External recruitment type, display a list of Announcements.
>
> It is easy to write a TALES expression in "Portal Type" field, that
> makes relation string field display only Persons or Announcements,
> according to recruitment_type. It works good after initial load of the
> form.
>
> But then, user can change the value on a Recruitment Type list field. In
> such a case, relation string, when clicked on gear wheel, would display
> wrong portal type.
> I've attached javascript method to the list field:
> onchange='form.submit()', so the form is reloaded when user changes
> value in list field.

This will be a very strange behaviour for the user. Only clicking on save 
button (or pressing enter) should save data.

> But then I've found out that my TALES expression is not re-evaluated
> after form is reloaded, so the releation string field is still
> associated with wrong portal type.

This is just because the TALES is evaluated on the current version of the 
object, but the object is not modified yet.

> The question is - what should I do to make TALES re-evaluate after form
> is reloaded? Definitely there is such a way, 'couse TALESes are
> evaluated after user click gear wheel and then click "Update relation".
> I need similar behaviour for list field.

You could make it work by looking the value in REQUEST, but I would suggest a 
different approach, because even if you can restrict the portal type of 
referal relation based on recruitment_type property, the user can later 
change recruitement_type and your data is inconsistant. Trying to keep data 
consistent at the form level (eg. using required field or TALES) is not 
always possible.

In your case, you should allow the user to set a referer to both an 
Anouncement or a Person, and check the consistency by raising a 
ValidationFailed when the user pass a workflow transition (eg. 
validate_action).

You could also make different portal types (Internal Recruitement and External 
Recruitement) that use different forms.

Jérome



More information about the Erp5-dev mailing list