opengui.Field¶
- class opengui.Field(name, value=None, original=None, default=None, options=None, required=False, multi=False, trigger=False, readonly=False, validation=None, content=None, errors=None, fields=None)¶
Class for creating and manipulating a field
- Parameters:
name – name of the field
value – value of the field
original – original value (if updating)
default – default value
options – list of options to choose from
required – whether reuired
multi – whether multiple options can be selected
trigger – whether to reload if value changed
readonly – whether read only
validation – validation, see
Field.validationcontent – customer attributes
errors – list of errors
fields – sub fields of
Fields
- ATTRIBUTES¶
List of actual attributes vs. what goes in content
- content: dict¶
Custom attributes
- default¶
Default value
- errors: list¶
List of error for this field
- fields: opengui.Fields¶
Sub fields of this field
- multi: bool¶
Whether can select multiple values
- name: str¶
Name of the field
- options: list¶
List of options
- original¶
The orginal value (when updating)
- readonly: bool¶
Whether readonly
- required: bool¶
Whether required
- trigger: bool¶
Whether changes should trigger a reload
- validation: str or callable¶
How to validate
If str, will use as regex. If regex doesn’t match value, will add to errors.
If callable, will call with value and current list of errors. Add to list of errors if invalid.
- value¶
The value(s)
- __contains__(key: str) bool¶
Check to see if sub field exists
- Parameters:
key (str) – field name
- Return type:
bool
Usage
field = Field("one", fields=[{"name": "foo"}]) "foo" in field # True "bar" in field # False
- __getattr__(attr: str)¶
Shorthand for field.values returning field.value and orginal/s too. Used so this and
Fieldshave feature parity- Parameters:
attr (str) – Either ‘values’ or ‘orginals’
Usage
field.value = "foo" field.values # "foo"
- __getitem__(key: str) 'opengui.Field'¶
Retrieves a sub field by name
- Parameters:
key (str) – field name
- Return type:
Usage
field = Field("many", fields=[{"name": "foo"},{"name": "bar"}]) field["foo"].name: # "foo" # "bar"
- __iter__()¶
Allows iteration over sub fields
Usage
field = Field("many", fields=[{"name": "foo"},{"name": "bar"}]) for sub in field: sub.name # "foo" # "bar"
- __len__() int¶
Returns the number of sub fields
- Return type:
int
Usage
field = Field("many", fields=[{"name": "foo"},{"name": "bar"}]) len(field) # 2
- append(*args, **kwargs)¶
Appends a field onto this Field’s sub fields
- Parameters:
args – args
kwargs – kwargs
Usage
Check out
Fields.append
- extend(fields)¶
Extens a field onto this Field’s sub fields
- Parameters:
fields – fields
Usage
Check out
Fields.extend
- to_dict() dict¶
Returns dictionary representation of field
The content dict values will be added to the dict as long as they’re not part of attributes.
- Return type:
dict
Usage
field = opengui.Field( "unit", value="test", original="recipe", default="factory", options="family", required="pants", multi="functional", trigger="ed", readonly="yes", validation="sure", content={"name": "nope", "label": "yep"}, errors="whoops", fields=[{"name": "a"}] ) # field.to_dict() # { # "name": "unit", # "value": "test", # "original": "recipe", # "default": "factory", # "options": "family", # "required": "pants", # "multi": "functional", # "trigger": "ed", # "readonly": "yes", # "validation": "sure", # "label": "yep", # "errors": "whoops", # "fields": [{"name": "a"}] # }
- validate(store=True) bool¶
Validates the data in the field, even if validation isn’t set. Returns errors.
If value is None and default is not, set value to default
If value is None and readonly is True, sets value to original
If value is still None and required, adds ‘missing value’ to errors
If multi is set and value is not None but not a list, adds ‘multi requires list’ to errors
If options is set,if value isn’t within, adds ‘invalid value’ or ‘invalid values’ (listing the invalid ones) to errors
If validation is set, applies it. See
Field.validationfor more
- Parameters:
store – whether to store the errors (if any) on the Field
- Returns:
Whether valid or not
- Return type:
bool