Got little further with this concept and created new validator upon
modified html helpers and form handling classes. Basically im thinking
to generalize and automate form processing, that takes values from
several tables, perhaps even from different databases.

This validator was also inspired from Massimos tip on how to use
request vars to validate against other form fields, yet it is
basically same as IS_NOT_IN_DB validator from the functionality point.
Yet it is not dependent on extra method, that is used to set record_id
from form class as IS_NOT_IN_DB is.

[code="python"]

class FIELD_NOT_IN_DB(object):
    """
    usage example:
    MyForm.addField(INPUT(_type="hidden", _name='db.table.id'))
    MyForm.addField(INPUT(_name='db.table.field')).addValidator
(FIELD_NOT_IN_DB(POOL, "db.table.field", request.vars.get
('db.table.id')))
    """
    def __init__(self, POOL, field, record_id, error_message='value
already in database!'):
        self.POOL = POOL
        self.field = field
        self.record_id = record_id
        self.error_message = error_message

    def __call__(self, value, **kwargs):
        if self.field.find('.') == 2:
            database_ref, table_ref, field_ref = self.field.split('.')
            database = self.POOL[database_ref]
            table = database[table_ref]
            field = table[field_ref]
        else:
            raise RuntimeError, "Validator was not initialized
properly. Please give field name in a form of: db.table.field"
        # in case of update old record...
        if self.record_id:
            # check there are no fields with given value except record
itself
            rows = database((field == value) & (table.id !=
self.record_id)).select(limitby=(0, 1))
        else:
            # in case of inserting new record, there shouldnt be any
rows found
            rows = database(field == value).select(limitby=(0, 1))
        if len(rows) > 0:
            return (value, self.error_message)
        return (value, None)

[/code]

On Nov 29, 10:20 pm, mmstud <[EMAIL PROTECTED]> wrote:
> No problems, typo of mine.
>
> What do you suggest, if i want to get all database connection objects
> listed. Would it be a good solution to collect them to dictionary
> like:
>
> POOL = {'web2py_test': db, 'web2py_test2': db2,...}
>
> and using that dictionary on controllers?
>
> -Marko
>
> On Nov 29, 9:55 pm,mmstud<[EMAIL PROTECTED]> wrote:
>
> > Im trying to get this working:
>
> > db=SQLDB('mysql://[EMAIL PROTECTED]/web2py_test')
> > db2=SQLDB('mysql://[EMAIL PROTECTED]/web2py_test2')
>
> > then i defined tables db... and db2... but found, that all tables were
> > created under one database, the first one (web2py_test). So how this
> > should work?
>
> > Thanks,
> > -Marko
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to