'Generate a Form widget
@param r: the S3Request instance
@param widget: the widget as a tuple: (label, type, icon)
@param attr: controller attributes for the request'

def _form(self, r, widget, **attr):

    widget_get = widget.get
    label = widget_get('label', '')
    if label:
        label = current.T(label)
    icon = widget_get('icon', '')
    if icon:
        icon = ICON(icon)
    context = widget_get('context')
    tablename = widget_get('tablename')
    (resource, context) = self._resolve_context(r, tablename, context)
    widget_filter = widget_get('filter')
    if widget_filter:
        resource.add_filter(widget_filter)
    record = resource.select(['id'], limit=1, as_rows=True).first()
    if record:
        record_id = record.id
    else:
        record_id = None
    if record_id:
        readonly = (not current.auth.s3_has_permission('update', tablename, record_id))
    else:
        readonly = (not current.auth.s3_has_permission('create', tablename))
    sqlform = widget.get('sqlform')
    if (not sqlform):
        sqlform = resource.get_config('crud_form')
    if (not sqlform):
        from s3forms import S3SQLDefaultForm
        sqlform = S3SQLDefaultForm()
    get_config = current.s3db.get_config
    if record_id:
        onvalidation = (get_config(tablename, 'create_onvalidation') or get_config(tablename, 'onvalidation'))
        onaccept = (get_config(tablename, 'create_onaccept') or get_config(tablename, 'onaccept'))
    else:
        onvalidation = (get_config(tablename, 'create_onvalidation') or get_config(tablename, 'onvalidation'))
        onaccept = (get_config(tablename, 'create_onaccept') or get_config(tablename, 'onaccept'))
    form = sqlform(request=r, resource=resource, record_id=record_id, readonly=readonly, format='html', onvalidation=onvalidation, onaccept=onaccept)
    _class = self._lookup_class(r, widget)
    output = DIV(H4(icon, label, _class='profile-sub-header'), DIV(form, _class='form-container    thumbnail'), _class=_class)
    return output
