Hi Clara,

I think (someone please correct me if I'm wrong -- and have a look at my 
other question posted just after this :D) that the validator has no access 
to the record.

But because you can set the validator on a field at any time, one solution 
is to chang your validator class so it knows about the record id, and only 
assign the validator to the table's field just before you need to use it in 
a form, but after you have obtained the record id.

class MY_VALIDATOR(IS_IN_SET):
    
    def __init__(self, record_id=None):
        self.record_id = record_id
        #whatever your validator does....

    def __call__(self, value):
        if value == self.self.record_id:
             #whatever....

def some_controller_url():
    record_id = request.vars.record_id
    #Override the validator set in models (if any) with this validator 
which does have knowledge of the record....
    db.my_table._my_field.requires = MY_VALIDATOR(record=record_id)
    form = SQLFORM( db.my_table, record=record_id....)
    ...#Table will be built with a validator which knows about your record 
:)
     

Does this make sense?


On Monday, December 1, 2014 2:49:45 PM UTC, clara wrote:
>
> Hello all,
>
> I am developing a web2py application and I am writing the logic to control 
> stock of certain items. To that end, I wrote a custom validator which is 
> working fine when creating new "sale" items. Now the problem arises when 
> updating these sale items, because I would need to know the actual record 
> being updated to adjust the logic in this cases.
>  
> I could get this information through ._before_update method, but how would 
> I pass it to the validator ?
>
> I am not sure if this is the best way to do it. I will appreciate any help 
> on this matter!
>
> Best regards,
>
> Clara
>
>
>

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to