Hi Diez,


On Apr 6, 10:40 pm, "Diez B. Roggisch" <[email protected]> wrote:

> >    http://docs.turbogears.org/1.0/UnifiedControllers
>
> Which is a rather uninformed and heavily opinionated recipe - to say the
> least. The 6 reasons given for adopting it are mostly nonsense - and the
> resulting code is nothing more readable or anything. Instead it goes against
> what the usual practice of creating form-pageflows is in TG.
>
> For example, """You can't use dynamically created forms - the form has to be
> created ahead of time to be passed to the @validate decorator.""" is either an
> outright lie, or the poster has no deeper knowledge of TG and thus doesn't
> know that it's perfectly fine to pass a callable to @validate-decorator so 
> that
> you can choose or create a form based on runtime-criteria.

Yes I agree that the article is quite opinionated. In fairness to the
author of the code Unified Controller recipe, it is a lot cleaner to
my first attempt at implementing this approach myself.

If you could point out a good implementation of dynamic form
validation in a controller using TG widgets, I'd be keen to emulate
best practice.

> From your post I have troubles seeing what the dynamicity is you require and
> if there isn't a better way to work with that.  Maybe if you could elaborate
> on that, we could provide better help.
>
> Diez


I believe my form needs to be dynamic because fields may be rendered
as select fields, hidden or text fields depending on tbe access group
of the user.

I'll give a few examples.

Depending on the user or the point in the workflow, the Store may be a
displayed with a hidden id, or it may be a select field.
When an order is first entered there is a qty_ordered field that is
rendered as a text input. After a user enters their order the form
renders as all display.
When the order is processed by the HQ, the qty_ordered field is
rendered as a display field. An additional qty_supplied field is
rendered as a display field.
After the HQ processes the order, the order is then rendered as all
display.

I have included my form function. Much of the conditional logic is in
the generating the individual detail lines (this is handled by a call
to getPluClassSetsForm).

I have taken the approach of attaching validators to individual fields
in the form, because if I added them to a schema I'd probably need to
duplicate the conditional logic.

What I'm immediately concerned with is the validation error knocking
validation over. I suspect it is an artefact of my approach.

Admittedly this form seems a lot more complicated than any of the
examples that I see in the documentation and I'd be interested to know
if there are cleaner design approaches to doing this.

Regards,
Chris Guest


def get_order_form(mode, status_code, store_options=None,
outerRepetitions=1, innerRepetitions=1):

    if store_options is None:
        store_options = []

    store_id = None
    store_disp = None
    action = widgets.HiddenField(name="action")

    if mode=='add' and len(store_options)>1:
        store_id = widgets.SingleSelectField(name="store_id",
label="Store", options=store_options)
    else:
        store_disp = DisplayField(name='store_disp', label='Store')
        store_id = widgets.HiddenField(name="store_id")

    order_id = None
    if mode not in ('add',):
        order_id = widgets.HiddenField(name="order_id")

    order_date_edit = widgets.CalendarDatePicker(
            name='order_date',
            label='Order Date', format=wo_config.date_entry_format,
calendar_lang='en',
            validator=validators.DateTimeConverter
(format=wo_config.date_entry_format))
    order_date = DisplayDateField(name='order_date', label='Order
Date', format=wo_config.long_datetime_format)
    if mode == 'add':
        order_date = order_date_edit


    delivery_date_edit = widgets.CalendarDatePicker(
            name='delivery_date',
            label='Delivery Date', format=wo_config.date_entry_format,
calendar_lang='en',
            validator=validators.DateTimeConverter
(format=wo_config.date_entry_format))
    delivery_date = DisplayDateField(name='delivery_date',
label='Delivery Date', format=wo_config.long_datetime_format)
    if mode == 'add':
        delivery_date = delivery_date_edit

    wo_form_key = widgets.HiddenField(name="wo_form_key")

    custom_number_disp = None
    custom_number = None
    description = widgets.TextField(name='description',
label='Description', attrs=dict(size=40, maxlength=40))
    if mode not in ('add',):
        custom_number_disp = DisplayField(name='custom_number_disp',
label='Order Number')
        custom_number = widgets.HiddenField(name='custom_number')
        description = DisplayField(name='description',
label='Description')

    invoice_number = None
    if mode=='open' and wo_config.auto_invoice_from_supplied_order:
        invoice_number =  widgets.TextField
(name='custom_number_invoice', label='Invoice Number')

    net_amount = DisplayField(name='net_amount', label='Net Amount')

    pluClassSetsForm = getPluClassSetsForm('order', mode,
status_code,  outerRepetitions=outerRepetitions,
innerRepetitions=innerRepetitions)
    fields = [field for field in
        (wo_form_key, action, order_id, store_id, store_disp,
order_date, delivery_date,
        custom_number, custom_number_disp, invoice_number, net_amount,
gst_amount, pluClassSetsForm, description)
        if field is not None]
    fieldrows = [[col for col in row if col and type(col)!
=widgets.HiddenField] for row in [
        (order_id, store_id, store_disp, delivery_date),
        (custom_number, custom_number_disp, invoice_number),
        (net_amount,),
        (pluClassSetsForm,),
        (description,),
        ]]

    submit = widgets.SubmitButton(name='order_submit')
    submit_text = dict(add='Submit Order', edit='Edit Order',
open='Open Order', view='OK')[mode]
    action = dict(add='/order/?', open='/order/?', edit='/order/?',
view='/')[mode]


    repeating_form = WOTabberListForm(
        'orderform',
        fields = fields,
        action=action,
        submit=submit,
        submit_text = submit_text,
        form_tint = 'order_tint', tab_default='',
        fieldrows = fieldrows,
        colspans = {'plu_classes':'3',}
    )

    repeating_form.javascript.append(widgets.JSLink('webordering',
'javascript/calculations.js'))

    return repeating_form



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to