'On-validation routine for s3_fields:
- field name must be unique within the table
- @ToDo: Check for Reserved Words'

@staticmethod
def s3_field_onvalidation(form):

    db = current.db
    table = db.s3_field
    form_vars = form.vars
    if ('id' in form_vars):
        record_id = form_vars.id
    elif hasattr(form, 'record_id'):
        record_id = form.record_id
    else:
        record_id = None
    record = (form.record if hasattr(form, 'record') else None)
    if record_id:
        fields_required = ('table_id', 'name', 'field_type', 'component_key', 'component_alias')
        if (not record):
            missing = fields_required
        else:
            missing = []
            for fieldname in fields_required:
                if (fieldname not in record):
                    missing.append(fieldname)
        if missing:
            query = (table.id == record_id)
            record = db(query).select(limitby=(0, 1), *missing).first()
    table_id = None
    if ('table_id' in form_vars):
        table_id = form_vars.table_id
    elif (record and ('table_id' in record)):
        table_id = record.table_id
    else:
        table_id = table.table_id.default
    if ('name' in form_vars):
        new_name = form_vars.name
        if record:
            current_name = record.name
        else:
            current_name = None
        if ((new_name != current_name) and table_id):
            query = (((table.name == new_name) & (table.table_id == table_id)) & (table.deleted != True))
            if record_id:
                query &= (table.id != record_id)
            row = db(query).select(table.id, limitby=(0, 1)).first()
            if row:
                form.errors['name'] = 'A    field    with    this    name    already    exists    in    this    table'
    if ('component_key' in form_vars):
        component_key = form_vars.component_key
    elif record:
        component_key = record.component_key
    else:
        component_key = table.component_key.default
    if component_key:
        master = None
        if ('field_type' in form_vars):
            field_type = form_vars.field_type
        elif record:
            field_type = record.field_type
        else:
            field_type = table.field_type.default
        field_type = str(field_type)
        if (field_type[:10] == 'reference    '):
            ktablename = field_type.split('    ', 1)[1]
            if ('.' in ktablename):
                ktablename = ktablename.split('.', 1)[0]
            master = ktablename
        if ('component_alias' in form_vars):
            component_alias = form_vars.component_alias
        elif record:
            component_alias = record.component_alias
        if (not component_alias):
            ttable = current.s3db.s3_table
            query = (ttable.id == table_id)
            row = db(query).select(ttable.name, limitby=(0, 1)).first()
            tablename = row.name
            component_alias = tablename.split('_', 1)[1]
            form_vars.component_alias = component_alias
        query = (((((table.field_type == ('reference    %s' % master)) | table.field_type.like(('reference    %s.%%' % master))) & (table.component_key == True)) & (table.component_alias == component_alias)) & (table.deleted != True))
        if record_id:
            query &= (table.id != record_id)
        row = db(query).select(table.id, limitby=(0, 1)).first()
        if row:
            form.errors['component_alias'] = 'A    component    with    this    alias    already    exists    for    this    master    table'
