I think there is, but currently I've came up with:
def year():
# too many - fail
if len(request.args) > 1:
redirect(URL(r=request,args=[]))
# init
table = db.year_year
app_settings = AppSettings()
arg0 = None
if len(request.args) == 1:
arg0 = request.args[0]
if arg0 == 'add':
def update_settings(form):
requested = form.request_vars.newyear
if not app_settings['min_year']:
requested = form.vars.year
app_settings['min_year'] = requested
app_settings['max_year'] = requested
app_settings.commit()
def onvalidate_addyear(form):
if app_settings['max_year']:
next = str(int(app_settings['max_year']) + 1)
requested = form.request_vars.newyear
if next and requested != next:
if requested <= next:
session.flash = \
"The year %s has already been added" % requested
else:
session.flash = \
"The year %s does not follow %s" % (requested, next)
redirect(URL(r=request, args=['add']))
form.vars.year = requested
default = dict(
table = table,
onaccept = update_settings,
onvalidation = onvalidate_addyear
)
if app_settings['min_year'] == None:
# no years yet - need to add first
form = crud.create(**default)
return dict(form = form)
else:
newyear = int(app_settings['max_year']) + 1
table.year.writeable = False
table.year.default = newyear
form = crud.create(**default)
# add field to hold value for input
form.append(INPUT(_type='hidden', _value=newyear, _name =
'newyear'))
return dict(form = form)
# no add
rows = db().select(table.ALL)
all_years = extract_field(rows, 'year')
if arg0 and int(arg0) and \
arg0 >= app_settings['min_year'] and arg0 <=
app_settings['max_year']:
form = crud.read(table, rows[all_years.index(arg0)])
return dict(form = form)
elif arg0:
redirect(URL(r=request,args=[]))
return dict(app = app_settings.row, years = all_years)
On Sun, Jun 14, 2009 at 2:16 AM, mdipierro<[email protected]> wrote:
>
> can you post an example? I am sure there is a better way.
>
> Massimo
>
> On Jun 13, 3:28 pm, Hans Donner <[email protected]> wrote:
>> the model has some more fields, entered by the user. This filed is
>> prepopulated by the server.
>> On submitting the prepopluated value may be not valid any more, but
>> may not be updated by the server
>> (is considered part of the user submitted fileds).
>>
>> Currently worked around it by adding a hidden field with the data and
>> supplying an onvalidation.
>>
>> On Sat, Jun 13, 2009 at 6:31 PM, mdipierro<[email protected]> wrote:
>>
>> > I do not understand. If the fields are marked readonly (as in
>> > db.table.field.readonly=True) the field is not submitted from the
>> > client to the server with the form. The form is populated serverside.
>>
>> > Massimo
>>
>> > On Jun 13, 10:38 am, Hans Donner <[email protected]> wrote:
>> >> Well, depends what you call 'inserted by the visitor'. In this case
>> >> I'm prepopulating the form, some fields are not editable, but the user
>> >> should submit these values as well.
>> >> Guess I have to use onvalidate then to make sure all is ok.
>>
>> >> The second remark in my original posting: the 'unique' should somehow
>> >> be supported by DAL, or warnings should be submitted that it is
>> >> unsupported.
>>
>> >> On Sat, Jun 13, 2009 at 4:39 PM, mdipierro<[email protected]> wrote:
>>
>> >> > back to the original question. Should they validate? If they are
>> >> > readonly it means they are not inserted by the visitor. We only
>> >> > validate visitor provided input.
>>
>> >> > Massimo
>>
>> >> > On Jun 13, 9:02 am, Hans Donner <[email protected]> wrote:
>> >> >> sorry, should be db.table.field.default = ''
>>
>> >> >> On Sat, Jun 13, 2009 at 3:54 PM, mdipierro<[email protected]>
>> >> >> wrote:
>>
>> >> >> > I am not what you mean by:
>>
>> >> >> > db.table.field = 'some invalid value for the requires[]'
>>
>> >> >> > It seems to be redefining the field. It should give an error.
>>
>> >> >> > On Jun 13, 8:28 am, Hans Donner <[email protected]> wrote:
>> >> >> >> db.define_table(
>> >> >> >> 'test',
>> >> >> >> SQLField('unique', 'string', unique = True,
>> >> >> >> requires = IS_NOT_IN_DB(db, 'test.unique')),
>> >> >> >> )
>>
>> >> >> >> def pseudo():
>> >> >> >> db.table.field.writable = False
>> >> >> >> db.table.field = 'some invalid value for the requires[]'
>> >> >> >> form = crud.create(db.table)
>>
>> >> >> >> the form doesn't run the validators for the non-writable field, and
>> >> >> >> when you are on GAE the uniqueness is not enforced on db level (and
>> >> >> >> DAL does not make up for this).
>>
>> >> >> >> When I have the following:
>> >> >> >> def pseudo():
>> >> >> >> db.table.field = 'some invalid value for the requires[]'
>> >> >> >> form = crud.create(db.table)
>> >> >> >> form.custom.widget.jaar.update(**dict(_disabled = True))
>>
>> >> >> >> the value is non-editable by the user, it is validated but the
>> >> >> >> value is removed
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" 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/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---