Fields

There are a number of fields available in Enso. This page documents functionality that is available to all classes.

Note: Many of the methods here start with `set` or `add`. There are corresponding `get` methods for most of these.

The Name of the Field

You set the name of the field when you first instantiate it. For example here is a text field for a column called name.

TextField::make('name'),

The name should correspond to an attribute on the model and usually to the database column that this attribute is stored in.

You can manually get and set the name for a field, though you will rarely need to do this.

$field = TextField::make('name');
$field->setName('surname');
$field->getName(); // 'surname'

Label

Sometimes the name of a column isn't something that you want a user to see. For example image_id. In this case you can change the text that will be used to label the field.

TextField::make('user_group_name_string')->setLabel('Group');

Placeholder

You often want a placeholder in forms. You can do that.

TextField::make('name')->setPlaceholder('E.g. John Smith');

Help Text

You can display some small text below the field to give extra instructions to a user.

FileUploadFieldResumable::make('image')->setHelpText('Image should be at least 500 x 500px');

Fieldset Classes

You can use this to add a class to the field's wrapper element. This corresponds to a Bulma .column element, so you can add things like is-half or is-half-desktop.

Note: the naming of this is a legacy from when Enso used Bootstrap in the backend and is due for a refactor.

Also note: You want to use addFieldsetClass or setFieldsetClasses, not setFieldsetClass.

TextField::make('name')->addFieldsetClass('is-half-desktop');

Default Value

You can set a default value for the field. This can be dealt with at the database level but it can also be convenient for a user to see this value before saving.

TextField::make('type_of_thing')->setDefaultValue('normal_regular_thing');

Edit Permissions

You can prevent a certain field bneing edited by users that do not have the appropriate permissions.

TextField::make('secrets')
    ->setEditPermission('can_edit_secrets')

Restrictions

You can also use a callable to decide who can see a field.

TextField::make('name')
    ->setFieldRestrictions(function () {
        return false; // Nobody will see the name field
    });

Write Filters

You can filter data before it is written to the database. Here we'll reverse a string before saving it. I don't know why you would want to do that but you could.

TextField::make('name')
    ->addWriteFilter(function ($value) {
        return strrev($value);
    });

Custom Data Writing

By default a field will write the data to the field with the given name. You can override the method that does this and use your own log storage logic.

TextField::make('name')
    ->setCallback(function ($item, $data, $field) {
        Log::info("$data was just saved but we're going to log it instead.");
    });

Readonly

You can make a field readonly.

TextField::make('name')->setReadonly();

Disable

You can make a field disable.

TextField::make('name')->setDisabled();

Regex Pattern Validation

You can apply a value to the HTML pattern field for validation.

TextField::make('country_code')->setPattern('[A-Za-z]{3}');

Sanitization

You can set sanitization options. How these are used will vary from field to field. For TextField and TextareaField this value will be passed to HTML Purifier.

TextareaField::make('some_html')->setSanitizationSettings($settings);

Translations

See the Localisation section for more details.

TextField::make('title')->setTranslatable();