-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathform3.php
More file actions
49 lines (38 loc) · 1.75 KB
/
form3.php
File metadata and controls
49 lines (38 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace Atk4\Ui\Demos;
use Atk4\Data\Model;
use Atk4\Ui\App;
use Atk4\Ui\Button;
use Atk4\Ui\Form;
use Atk4\Ui\Header;
use Atk4\Ui\Js\JsBlock;
use Atk4\Ui\Js\JsReload;
use Atk4\Ui\View;
/** @var App $app */
require_once __DIR__ . '/../init-app.php';
Header::addTo($app, ['Form automatically decided how many columns to use']);
$buttons = View::addTo($app, ['ui' => 'green basic buttons']);
$seg = View::addTo($app, ['ui' => 'raised segment']);
Button::addTo($buttons, ['Use Country Model', 'icon' => 'arrow down'])
->on('click', new JsReload($seg, ['m' => 'country']));
Button::addTo($buttons, ['Use File Model', 'icon' => 'arrow down'])
->on('click', new JsReload($seg, ['m' => 'file']));
Button::addTo($buttons, ['Use Stat Model', 'icon' => 'arrow down'])
->on('click', new JsReload($seg, ['m' => 'stat']));
$form = Form::addTo($seg, ['layout' => [Form\Layout\Columns::class]]);
$modelClass = ['country' => Country::class, 'file' => File::class][$app->tryGetRequestQueryParam('m')] ?? Stat::class;
$form->setEntity((new $modelClass($app->db))->loadAny());
$form->onSubmit(static function (Form $form) {
$errors = [];
$modelDirty = \Closure::bind(static function () use ($form): array { // TODO Model::dirty property is private
return $form->entity->dirty;
}, null, Model::class)();
foreach ($modelDirty as $field => $value) {
// we should care only about editable fields
if ($form->entity->getField($field)->isEditable()) {
$errors[] = $form->jsError($field, 'Value was changed, ' . $form->getApp()->encodeJson($value) . ' to ' . $form->getApp()->encodeJson($form->entity->get($field)));
}
}
return $errors !== [] ? new JsBlock($errors) : 'No fields were changed';
});