Field Configurations
You're free to configure your field in any way.
Configurations
You can call any method from within the constructor:
class Example extends Field
{
public function __construct(string $label, string $name, string $otherResource = null)
{
parent::__construct($label, $name, $otherResource);
$this->rules('min:6');
$this->withAttributes(['type' => 'number']);
$this->setMaxWidth('md');
}
public function component(): string
{
return 'Example';
}
}
Default field value
You can set the default value for each field:
Text::make(...)
->default('Test')
You can also set a default based on the form state:
Text::make(...)
->default(function (ManagesForm $form) {
$model = $form->getModel();
return $model ? $model->first_name : $form->getState('last_name');
})
Default value for editing resources
The default value on a field will be ignored when editing resources, because it will fill the value based on the model.
In rare cases you might want to have control over the default value when editing a resource too. You can achieve this with the following method:
Text::make(...)
->initFormValueUsing(function(ManagesForm $form) {
$model = $form->getModel();
return 'myValue';
})
When this closure returns null, it will fall back to the default (model) value.
The initFormValueUsing
closure will always overrule the default
value / closure if it does not return null
Preparation methods
Sometimes (mostly for performance reasons), you may need to execute some code which is only relevant for show/form/index.
For example, when you have a BelongsTo
field, you only want to retrieve options when needed inside a form.
For this reason, you may on of the following fields
public function onBeforeRender(Model $model = null): void
{
// Always called before rendering the field on show/form/index
}
public function onBeforeRenderDetail(Model $model): void
{
// Only called when rendering show
}
public function onBeforeRenderForm(Model $model = null): void
{
// Only called when rendering form
}
public function onBeforeRenderIndex(): void
{
// Only called when rendering index
}
public function onBeforeRenderIndexFilter(): void
{
// Used before the field filter component is rendered
}
For example:
public function onBeforeRenderForm(Model $model = null): void
{
$this->componentProperties['general_ledgers'] = $this->generalLedgers();
}
Debounce value
Fields have the option to react to user input (via ->onUpdate
).
When users type in your fields' form input, you will want to avoid firing requests everytime the user presses on a key.
For this reason, Qore has a built-in delay (in milliseconds) that it will "wait" until it sends the onUpdate
request.
You can set this value (the default value is 200
) for every field:
Text::make()
->setDebounceValue(500)
You can also disable debouncing (for example in select fields):
MyField::make()
->disableDebounce() // Same as: ->setDebounceValue(0)