Field validatio with multiple columns

49 views
Skip to first unread message

JC

unread,
Aug 12, 2015, 11:17:45 PM8/12/15
to web2py-users
Hello,
I´ve the following model:

db.define_table('bsc_input',
               
Field('id_p_indicator', db.p_indicator, label=T('Indicator'), notnull=True),
               
Field('current_month', 'integer', label=T('Month'), notnull=True),
               
Field('current_year', 'integer', label=T('Year'), notnull=True),
               
Field('current_value', 'float', label=T('Value'), notnull=True))  

  and I want to setup a validation  so that I´m sure the combination of  'id_p_indicator', 'current_month' and 'current_year' are unique.

How can I do it ?

Thanks

JC

Dave S

unread,
Aug 13, 2015, 2:13:28 AM8/13/15
to web2py-users

I've done one custom validator; see
<URL:https://groups.google.com/d/msg/web2py/KC5BPC_xjv8/MeD88yDYtOcJ>
for the example.
 
I think I left out how I invoked that; my choice was to do it in a FORM:
  form = FORM('Activation code:',
              INPUT
(_name='left', requires=[IS_NOT_EMPTY(), IS_HEXSTR()], _type="text", _maxlength="4", _class='activation', _id="left"),
 
# more fields here
 
)



Another choice would be to use the onvalidation() method
<URL:http://web2py.com/books/default/chapter/29/07/forms-and-validators#onvalidation>
which I believe applies to the more sophisticated form helpers as well.

You could also use validate_and_insert() routine, but my understanding is that that is pretty much covered by the above techniques.
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer?search=before_#validate_and_insert--validate_and_update>

I'm not quite sure how you would enforce  that sort of validation at the SQL level or in the actual database.

/dps



Michael Beller

unread,
Aug 13, 2015, 8:50:42 AM8/13/15
to web2py-users

Val K

unread,
Aug 13, 2015, 3:09:11 PM8/13/15
to web...@googlegroups.com
Forget all what you've read there (joke)
If you want this rules works anywhere and independent of any insert/update EVEN BY PROGRAM  - use db.table._before_insert/update  and so on (see http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer?search=_before_insert#callbacks-on-record-insert--delete-and-update)
It's almost SQL-level, I would say that pure SQL-level if your DB is accessed through web2py only and you don't use db.executesql
Just keep in mind:
  • validators - UI level only
  • callbacks on record insert, delete and update - any level, except third party access  and  db.executesql

Message has been deleted

Dave S

unread,
Aug 13, 2015, 3:44:42 PM8/13/15
to web2py-users


On Thursday, August 13, 2015 at 12:09:11 PM UTC-7, Val K wrote:
Forget all what you've read there (joke)
If you want this rules works anywhere and independent of any insert/update EVEN BY PROGRAM  - use db.table._before_insert/update  and so on (see http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer)

To be more specific
<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer?search=_before_insert#callbacks-on-record-insert--delete-and-update>

And, dang it, I searched for "before_", which didn't turn up anything specific; I think it did turn up the chapter level, as does "_before_insert", but I didn't think to expand it an use browser search.  So I knew I was missing something and not knowing what.


 

Валерий Кучеров

unread,
Aug 13, 2015, 4:16:21 PM8/13/15
to web...@googlegroups.com
 OK, thank you, Dave, I've corrected my post according to your helpful suggestions



Dave S <snide...@gmail.com> писал(а) в своём письме Thu, 13 Aug 2015 22:44:42 +0300:

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/atu_jVYj-5Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Написано с помощью почтового клиента Opera: http://www.opera.com/mail/
Reply all
Reply to author
Forward
0 new messages