You shouldn't create a field called id. Check the recommendations on the
docs. web2py creates an id (auto-increment) field automatically.
Anyway, form.vars.* are only available on onvalidation if the field is
editable.
Check your grid config. Maybe adding field_id=db.brix_test.id would help.
Check request.vars.id.
quinta-feira, 25 de Abril de 2019 às 00:50:22 UTC+1, Jim S escreveu:
>
> Give the following table:
>
> db.define_table('brix_test',
> Field('id', 'id', readable=False),
> Field('site', 'reference site',
> requires=IS_IN_DB(self.db, 'site',
> '%(siteNumber)s - %(name)s',
> zero='..')),
> Field('mixer_tech', 'reference mixerTech',
> requires=IS_IN_DB(self.db, 'mixerTech', '%(name)s', zero='..')),
> Field('sampled', 'datetime', requires=IS_DATE('%m/%d/%Y %I:%M %p')),
> Field('sample_id', length=20, requires=IS_NOT_EMPTY()),
> Field('source', length=50, requires=IS_NOT_EMPTY()),
> Field('water_weight', 'integer', requires=IS_INT_IN_RANGE(100, 300)))
>
>
> I want to create an onvalidation function to be run when saving from by
> SQLFORM.grid that will ensure that the 'source' field is unique within each
> site.
>
> Ex:
>
> A record for Site 1 can have a sample_id of 'Tank 1'
> A record for Site 2 can have a sample_id of 'Tank 1'
>
> But you cannot have more than 1 record for Site 1 where sample_id is 'Tank
> 1'
>
> My problem is that I can't get my 'id' field to be recognized in my
> onvalidation function:
>
> def validate_brix_test(form):
> brix_test_id = form.vars.id
> site_id = form.vars.site
> mixer_tech_id = form.vars.mixer_tech
> sample_id = form.vars.sample_id
>
>
> site = db.site(site_id)
>
>
> # mixer tech must belong to the site specified
> mt = db.mixerTech(mixer_tech_id)
> if not mt:
> form.errors.mixer_tech = 'Mixer Tech was not found'
> else:
> if mt.siteId != site_id:
> form.errors.mixer_tech = 'Mixer Tech does not belong to %s.' %
> site.name
>
>
> # sample id must be unique within the site
> if brix_test_id and brix_test_id > 0:
> sample_id_count = db((db.brix_test.id != brix_test_id) &
> (db.brix_test.sample_id == sample_id) &
> (db.brix_test.site == site_id)).count()
> else:
> sample_id_count = db((db.brix_test.sample_id == sample_id) &
> (db.brix_test.site == site_id)).count()
>
>
> if sample_id_count > 0:
> form.errors.sample_id = 'This sample ID has already been used at
> %s.' % site.name
>
>
> return
>
> The problem is that form.vars.id is always None regardless of whether or
> not I have db.brix_test.id.readable = db.brix_test.id.writable = True or
> False
>
> Any idea how I can get the ID of the record in the form into the
> onvalidation function?
>
> -Jim
>
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.