Hello All,

I need some help to have a widget work correctly.
BTW, once corrected, I think it could be useful to other users...

I built a widget to expose in a SELECT regions and their sub-regions 
('departement')  to choose.
For instance: a region A is divided into departement 11 and departement 12 
and so on.
The user can choose either a region or a specific departement (sub-region).

The widget works except that when submitted, the form returns errors:
"r_name: no data".
Below is a working example to show the problem.

Could anybody help me having this widget work correctly without returning 
errors, please.
I would greatly appreciate your kind help.
Thanks a lot in advance.

Dominique


in the model:
db.define_table('region',
    Field('r_name', 'string', length=250, required=True)
    )   
db.define_table('departement',
    Field('d_name', 'string', length=250, required=True),
    Field('d_code', 'string', length=3, required=True),
    Field('d_region', db.region)
    )

def region_widget(f,v,_class= "options"): 
    """
    Widget  that shows regions and departements in a SELECT.
    Can be used with SQLFORM and SQLFORM.factory
    Usage:
    db.region.r_name.widget = region_dptt_select_widget
    """
    region_dptt = db(db.region.id==db.departement.d_region)\
                    .select(db.region.r_name, db.region.id, 
db.departement.d_name, db.departement.d_code, db.departement.id, \
                    orderby=db.region.r_name, cache=(cache.ram,6))

    def prepare_select(rows):
        """
        Creates a list of tuples to be used in the SELECT helper:
        ['Region A', OPTGROUP('SubRegion 1','SubRegion 2', 'SubRegion 3'),
        'Region B', OPTGROUP('SubRegion 4','SubRegion 5', 'SubRegion 6')]
        """
        l_r=[]
        for row in rows:
            r = row.region.r_name
            if row.region.r_name not in l_r:
                l_r.append(row.region.r_name)
        interm_list=[]
        for reg in l_r:
           l_sr=[]
           for row in rows:
                if reg == row.region.r_name:
                    reg_id = row.region.id
                    dep = row.departement.d_code + ' ' + 
row.departement.d_name
                    dep_id = row.departement.id
                    l_sr.append(OPTION(dep,_value=row.departement.d_code))  
           option_reg = OPTION(reg, _value= 'R'+str(reg_id))
           interm_list.append((option_reg,l_sr))
        res=[]
        for reg, l_sr in interm_list:
            res.append(reg)
            res.append(OPTGROUP(*l_sr))
        return res
    return SELECT(*prepare_select(region_dptt), 
                  **dict(_name='region_to_search', _value=v, 
_id='region_to_search', _type = 'string', _class="options"))

in the controller:
def test():
    db.region.r_name.widget = region_widget
    fields=['r_name']
    labels = {'r_name':T('')}
    form= SQLFORM(db.region, fields = fields, labels = labels)
    a=None
    if form.accepts(request.vars, session, formname='test_form', 
dbio=False):
        a = form.vars
    elif form.errors:
        a = form.errors
    return dict(form=form, a = a)

def insertions():# TO RUN one time
    db.region.insert(r_name="Region A")
    db.region.insert(r_name="Region B")
    db.departement.insert(d_name="SubRegion 1", d_code="11", d_region=1)
    db.departement.insert(d_name="SubRegion 2", d_code="12",  d_region=1)
    db.departement.insert(d_name="SubRegion 3", d_code="21",  d_region=2)
    db.departement.insert(d_name="SubRegion 4", d_code="22",  d_region=2)
    db.departement.insert(d_name="SubRegion 5", d_code="25",  d_region=2)
    db.departement.insert(d_name="SubRegion 6", d_code="26",  d_region=2) 
  

Reply via email to