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
onBeforeRender 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 implement one of the following callbacks:
public function __construct(string $label, string $name, ?string $otherResource = null)
{
parent::__construct($label, $name, $otherResource);
// Always called before rendering the field on show/form/index
$this->registerOnBeforeRenderCallback(function (?Model $model): void {
$this->withComponentProperties('my-key', 'my-value')
})
// Called when rendering detail page
$this->registerOnBeforeRenderDetailCallback(function (Model $model): void {
$this->withComponentProperties('my-key', 'my-value')
})
// Called when rendering forms
$this->registerOnBeforeRenderFormCallback(function (?Model $model): void {
$this->withComponentProperties('my-key', 'my-value')
})
// Called when rendering as index collum
$this->registerOnBeforeRenderIndexCallback(function (): void {
$this->withComponentProperties('my-key', 'my-value')
})
// Called when rendering the index filter
$this->registerOnBeforeRenderIndexFilterCallback(function (): void {
$this->withComponentProperties('my-key', 'my-value')
})
}
When overriding the onBeforeRenderX methods, make sure to call the parent method.
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)