Define User Action
Agile UI Demo v5.0-dev
UI Integration
Arguments
Crud integration
Next demo defines an user action that requires arguments. You can specify arguments when the user action is invoked, but if not defined - user will be asked to supply an argument. User action will automatically validate argument types and it uses same type system as fields.
$model = new Model($owner->getApp()->db, ['table' => 'test']);
$model->addUserAction('greet', [
'appliesTo' => Model\UserAction::APPLIES_TO_NO_RECORDS,
'args' => [
'name' => [
'type' => 'string',
],
],
'callback' => static function (Model $model, string $name) {
return 'Hi ' . $name;
},
]);
$model->addUserAction('ask_age', [
'appliesTo' => Model\UserAction::APPLIES_TO_NO_RECORDS,
'args' => [
'age' => [
'type' => 'integer',
'required' => true,
],
],
'callback' => static function (Model $model, int $age) {
return 'Age is ' . $age;
},
]);
Form\Control\Line::addTo($owner, [
'action' => $model->getUserAction('greet'),
]);
View::addTo($owner, ['ui' => 'divider']);
Button::addTo($owner, ['Ask Age'])
->on('click', $model->getUserAction('ask_age'));