Skip to content

Commit

Permalink
wip - not reviewed!
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 26, 2024
1 parent 826484f commit 5b7d715
Show file tree
Hide file tree
Showing 45 changed files with 100 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $form->addField('email');
$form->onSubmit(function (Form $form) {
// implement subscribe here

return $form->jsSuccess('Subscribed ' . $form->model->get('email') . ' to newsletter.');
return $form->jsSuccess('Subscribed ' . $form->entity->get('email') . ' to newsletter.');
});

// decorate anything
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$button->on('click', new JsModal('Edit First Record', $vp));

$form->onSubmit(static function (Form $form) use ($table) {
$form->model->save();
$form->entity->save();

return new JsBlock([
$table->jsReload(),
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/lookup-virtual-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$form = Form::addTo($vp);
$form->addControl('category', [Form\Control\Lookup::class, 'model' => new Category($vp->getApp()->db)]);
$form->onSubmit(static function (Form $form) {
$category = $form->getControl('category')->model->load($form->model->get('category'));
$category = $form->getControl('category')->model->load($form->entity->get('category'));

return new JsToast($category->getTitle());
});
Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/scope-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$form->addControl('qb', [Form\Control\ScopeBuilder::class, 'model' => $model], ['type' => 'object']);

$form->onSubmit(static function (Form $form) use ($model) {
$message = $form->model->get('qb')->toWords($model);
$message = $form->entity->get('qb')->toWords($model);
$view = View::addTo($form)->addClass('atk-scope-builder-response');
$view->set($message);

Expand Down
2 changes: 1 addition & 1 deletion demos/_unit-test/virtual-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
$form = Form::addTo($thirdPage);
$form->addControl('category', [Form\Control\Lookup::class, 'model' => new Category($thirdPage->getApp()->db)]);
$form->onSubmit(static function (Form $form) {
$category = $form->getControl('category')->model->load($form->model->get('category'));
$category = $form->getControl('category')->model->load($form->entity->get('category'));

return new JsToast($category->getTitle());
});
Expand Down
4 changes: 2 additions & 2 deletions demos/form-control/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

$form->onSubmit(static function (Form $form) use ($app) {
$data = [];
foreach ($form->model->get() as $k => $v) {
$data[$k] = $app->uiPersistence->typecastSaveField($form->model->getField($k), $v) ?? 'empty';
foreach ($form->entity->get() as $k => $v) {
$data[$k] = $app->uiPersistence->typecastSaveField($form->entity->getField($k), $v) ?? 'empty';
}

return new JsToast(implode(', ', $data));
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
$form->addControl('also_checked', ['caption' => 'Also checked by default'], ['type' => 'boolean'])->set(true);

$form->onSubmit(static function (Form $form) use ($app) {
return new JsToast($app->encodeJson($form->model->get()));
return new JsToast($app->encodeJson($form->entity->get()));
});
4 changes: 2 additions & 2 deletions demos/form-control/dropdown-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$form->addControl('product_id', [Form\Control\DropdownCascade::class, 'cascadeFrom' => 'sub_category_id', 'reference' => SubCategory::hinting()->fieldName()->Products]);

$form->onSubmit(static function (Form $form) use ($app) {
$message = $app->encodeJson($app->uiPersistence->typecastSaveRow($form->model, $form->model->get()));
$message = $app->encodeJson($app->uiPersistence->typecastSaveRow($form->entity, $form->entity->get()));

$view = new Message('Values: ');
$view->setApp($form->getApp());
Expand Down Expand Up @@ -105,7 +105,7 @@
]);

$form->onSubmit(static function (Form $form) use ($app) {
$message = $app->encodeJson($form->model->get());
$message = $app->encodeJson($form->entity->get());

$view = new Message('Values:');
$view->setApp($form->getApp());
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/form6.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
$form->addControl('string_r', [Form\Control\Radio::class], ['values' => ['F' => 'female', 'M' => 'male']])->set('M');

$form->onSubmit(static function (Form $form) use ($app) {
return new JsToast($app->encodeJson($form->getApp()->uiPersistence->typecastSaveRow($form->model, $form->model->get())));
return new JsToast($app->encodeJson($form->getApp()->uiPersistence->typecastSaveRow($form->entity, $form->entity->get())));
});
4 changes: 2 additions & 2 deletions demos/form-control/input2.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
]));

$form->onSubmit(static function (Form $form) {
return $form->model->get('name');
return $form->entity->get('name');
});

Header::addTo($app, ['Multiple Form Layouts']);
Expand All @@ -142,7 +142,7 @@
$formPage->addControl('age', new Form\Control\Line());

$form->onSubmit(static function (Form $form) {
return $form->model->get('name') . ' has age ' . $form->model->get('age');
return $form->entity->get('name') . ' has age ' . $form->entity->get('age');
});

Header::addTo($app, ['onChange event', 'subHeader' => 'see in browser console']);
Expand Down
4 changes: 2 additions & 2 deletions demos/form-control/lookup-dep.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
]);

$form->onSubmit(static function (Form $form) {
return 'Submitted: ' . print_r($form->model->get(), true);
return 'Submitted: ' . print_r($form->entity->get(), true);
});

Header::addTo($app, ['Lookup multiple values']);
Expand Down Expand Up @@ -92,5 +92,5 @@
]);

$form->onSubmit(static function (Form $form) {
return 'Submitted: ' . print_r($form->model->get(), true);
return 'Submitted: ' . print_r($form->entity->get(), true);
});
8 changes: 4 additions & 4 deletions demos/form-control/lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
$view = new Message('Select:');
$view->setApp($form->getApp());
$view->invokeInit();
$view->text->addParagraph(Country::assertInstanceOf($form->model->ref('country1'))->name ?? 'null');
$view->text->addParagraph(Country::assertInstanceOf($form->model->ref('country2'))->name ?? 'null');
$view->text->addParagraph($form->model->get('country3') !== '' // related with https://github.com/atk4/ui/pull/1805
? (new Country($form->getApp()->db))->load($form->model->get('country3'))->name
$view->text->addParagraph(Country::assertInstanceOf($form->entity->ref('country1'))->name ?? 'null');
$view->text->addParagraph(Country::assertInstanceOf($form->entity->ref('country2'))->name ?? 'null');
$view->text->addParagraph($form->entity->get('country3') !== '' // related with https://github.com/atk4/ui/pull/1805
? (new Country($form->getApp()->db))->load($form->entity->get('country3'))->name
: 'null');

return $view;
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/scope-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
$form->addControl('qb', [Form\Control\ScopeBuilder::class, 'model' => $model, 'options' => ['debug' => true]]);

$form->onSubmit(static function (Form $form) use ($model) {
return "Scope selected:\n\n" . $form->model->get('qb')->toWords($model);
return "Scope selected:\n\n" . $form->entity->get('qb')->toWords($model);
});
4 changes: 2 additions & 2 deletions demos/form-control/tree-item-selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@

$form->onSubmit(static function (Form $form) use ($app) {
$response = [
'multiple' => $form->model->get('tree'),
'single' => $form->model->get('tree1'),
'multiple' => $form->entity->get('tree'),
'single' => $form->entity->get('tree1'),
];

$view = new Message('Items:');
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@

$form->onSubmit(static function (Form $form) {
// implement submission here
return $form->jsSuccess('Thanks for submitting file: ' . $form->model->get('img') . ' / ' . $form->model->get('file'));
return $form->jsSuccess('Thanks for submitting file: ' . $form->entity->get('img') . ' / ' . $form->entity->get('file'));
});
4 changes: 2 additions & 2 deletions demos/form/form-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
$model = $model->loadAny();

$saveAndDumpValues = static function (Form $form) {
$form->model->save();
$form->entity->save();

return new JsToast([
'title' => 'POSTed field values',
'message' => '<pre>' . $form->getApp()->encodeJson($form->model->get()) . '</pre>',
'message' => '<pre>' . $form->getApp()->encodeJson($form->entity->get()) . '</pre>',
'class' => 'success',
'displayTime' => 5000,
]);
Expand Down
14 changes: 7 additions & 7 deletions demos/form/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
$form->addControl('status_integer_required', [Form\Control\Dropdown::class], ['type' => 'integer', 'values' => $values, 'required' => true]);

$form->onSubmit(static function (Form $form) use ($app) {
return new JsToast($app->encodeJson($form->model->get()));
return new JsToast($app->encodeJson($form->entity->get()));
});

Header::addTo($tab, ['Comparing Field type vs Form control class']);
Expand All @@ -69,7 +69,7 @@
$form->buttonSave->set('Compare Date');

$form->onSubmit(static function (Form $form) {
$message = 'field = ' . print_r($form->model->get('field'), true) . '; <br> control = ' . print_r($form->model->get('control'), true);
$message = 'field = ' . print_r($form->entity->get('field'), true) . '; <br> control = ' . print_r($form->entity->get('control'), true);
$view = new Message('Date field vs control:');
$view->setApp($form->getApp());
$view->invokeInit();
Expand Down Expand Up @@ -194,8 +194,8 @@
$form->setModel($modelRegister);

Check failure on line 194 in demos/form/form.php

View workflow job for this annotation

GitHub Actions / Smoke (latest, StaticAnalysis)

Call to method Atk4\Ui\Form::setModel() with Atk4\Data\Model will always evaluate to true.

$form->onSubmit(static function (Form $form) {
if ($form->model->get('name') !== 'John') {
return $form->jsError('name', 'Your name is not John! It is "' . $form->model->get('name') . '". It should be John. Pleeease!');
if ($form->entity->get('name') !== 'John') {
return $form->jsError('name', 'Your name is not John! It is "' . $form->entity->get('name') . '". It should be John. Pleeease!');
}

return new JsBlock([
Expand Down Expand Up @@ -233,13 +233,13 @@

$form->onSubmit(static function (Form $form) {
$errors = [];
foreach ($form->model->getFields() as $name => $ff) {
foreach ($form->entity->getFields() as $name => $ff) {
if ($name === 'id') {
continue;
}

if ($form->model->get($name) !== 'a') {
$errors[] = $form->jsError($name, 'Field ' . $name . ' should contain exactly "a", but contains ' . $form->model->get($name));
if ($form->entity->get($name) !== 'a') {
$errors[] = $form->jsError($name, 'Field ' . $name . ' should contain exactly "a", but contains ' . $form->entity->get($name));
}
}

Expand Down
10 changes: 5 additions & 5 deletions demos/form/form2.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@
$form->onSubmit(static function (Form $form) {
$countryEntity = (new Country($form->getApp()->db))->createEntity();
// Model will have some validation too
foreach ($form->model->getFields('editable') as $k => $field) {
foreach ($form->entity->getFields('editable') as $k => $field) {
if ($countryEntity->hasField($k)) {
$countryEntity->set($k, $form->model->get($k));
$countryEntity->set($k, $form->entity->get($k));
}
}

// in-form validation
$errors = [];
if (mb_strlen($form->model->get('first_name')) < 3) {
$errors[] = $form->jsError('first_name', 'too short, ' . $form->model->get('first_name'));
if (mb_strlen($form->entity->get('first_name')) < 3) {
$errors[] = $form->jsError('first_name', 'too short, ' . $form->entity->get('first_name'));
}
if (mb_strlen($form->model->get('last_name')) < 5) {
if (mb_strlen($form->entity->get('last_name')) < 5) {
$errors[] = $form->jsError('last_name', 'too short');
}

Expand Down
6 changes: 3 additions & 3 deletions demos/form/form3.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
$form->onSubmit(static function (Form $form) {
$errors = [];
$modelDirty = \Closure::bind(static function () use ($form): array { // TODO Model::dirty property is private
return $form->model->dirty;
return $form->entity->dirty;
}, null, Model::class)();
foreach ($modelDirty as $field => $value) {
// we should care only about editable fields
if ($form->model->getField($field)->isEditable()) {
$errors[] = $form->jsError($field, 'Value was changed, ' . $form->getApp()->encodeJson($value) . ' to ' . $form->getApp()->encodeJson($form->model->get($field)));
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)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion demos/form/form5.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
]);

$formSubmit = static function (Form $form) use ($app) {
return new JsToast($app->encodeJson($form->model->get()));
return new JsToast($app->encodeJson($form->entity->get()));
};

$cc = Columns::addTo($app);
Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/accordion-nested.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
$form = Form::addTo($vp);
$form->addControl('email');
$form->onSubmit(static function (Form $form) {
return $form->jsSuccess('Subscribed ' . $form->model->get('email') . ' to newsletter.');
return $form->jsSuccess('Subscribed ' . $form->entity->get('email') . ' to newsletter.');
});

$addAccordionFx($vp, $maxDepth, $level + 1);
Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/accordion.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
$form = Form::addTo($vp);
$form->addControl('Email');
$form->onSubmit(static function (Form $form) {
return $form->jsSuccess('Subscribed ' . $form->model->get('Email') . ' to newsletter.');
return $form->jsSuccess('Subscribed ' . $form->entity->get('Email') . ' to newsletter.');
});
});

Expand Down
7 changes: 3 additions & 4 deletions demos/interactive/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ public function generateReport()

$console = Console::addTo($vp, ['event' => false]);
$console->set(static function (Console $console) use ($form) {
$entity = $form->model;
$entity->setMulti($_SESSION['atk4_ui_console_demo']);
$form->entity->setMulti($_SESSION['atk4_ui_console_demo']);

$console->output('Executing process...');
$console->info(var_export($entity->get(), true));
$console->info(var_export($form->entity->get(), true));
sleep(1);
$console->output('Wait...');
sleep(3);
Expand All @@ -141,7 +140,7 @@ public function generateReport()
$console->js(true)->hide();

$form->onSubmit(static function (Form $form) use ($console) {
$_SESSION['atk4_ui_console_demo'] = $form->model->get();
$_SESSION['atk4_ui_console_demo'] = $form->entity->get();

return new JsBlock([
$console->js()->show(),
Expand Down
10 changes: 5 additions & 5 deletions demos/interactive/modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
$form = Form::addTo($p);
$form->addControl('color', [], ['enum' => ['red', 'green', 'blue'], 'default' => 'green']);
$form->onSubmit(static function (Form $form) use ($vp2Modal) {
return $vp2Modal->jsShow(['color' => $form->model->get('color')]);
return $vp2Modal->jsShow(['color' => $form->entity->get('color')]);
});
});

Expand Down Expand Up @@ -208,15 +208,15 @@
$form->setModel($modelRegister->createEntity());

$form->onSubmit(static function (Form $form) use ($nextAction, $session) {
if ($form->model->get('name') !== 'John') {
return $form->jsError('name', 'Your name is not John! It is "' . $form->model->get('name') . '". It should be John. Pleeease!');
if ($form->entity->get('name') !== 'John') {
return $form->jsError('name', 'Your name is not John! It is "' . $form->entity->get('name') . '". It should be John. Pleeease!');
}

$session->memorize('success', true);
$session->memorize('name', $form->model->get('name'));
$session->memorize('name', $form->entity->get('name'));

$js = [];
$js[] = $form->jsSuccess('Thank you, ' . $form->model->get('name') . ' you can go on!');
$js[] = $form->jsSuccess('Thank you, ' . $form->entity->get('name') . ' you can go on!');
$js[] = $nextAction->js()->removeClass('disabled');

return $js;
Expand Down
6 changes: 3 additions & 3 deletions demos/interactive/popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ public function linkCart(View $cart, ?JsExpressionable $jsAction = null): void
// popup handles callbacks properly, so dynamic element such as form works
// perfectly inside a popup.
$form->onSubmit(static function (Form $form) {
if ($form->model->get('password') !== '123') {
if ($form->entity->get('password') !== '123') {
return $form->jsError('password', 'Please use password "123"');
}

// refreshes entire page
return $form->getApp()->jsRedirect(['logged' => $form->model->get('email')]);
// return new JsExpression('alert([])', ['Thank you ' . $form->model->get('email')]);
return $form->getApp()->jsRedirect(['logged' => $form->entity->get('email')]);
// return new JsExpression('alert([])', ['Thank you ' . $form->entity->get('email')]);
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions demos/interactive/tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
$form = Form::addTo($vp, ['class.segment' => true]);
$form->setModel($modelRegister->createEntity());
$form->onSubmit(static function (Form $form) {
if ($form->model->get('name') !== 'John') {
return $form->jsError('name', 'Your name is not John! It is "' . $form->model->get('name') . '". It should be John. Pleeease!');
if ($form->entity->get('name') !== 'John') {
return $form->jsError('name', 'Your name is not John! It is "' . $form->entity->get('name') . '". It should be John. Pleeease!');
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion demos/interactive/wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

$form->addControl('dsn', ['caption' => 'Connect DSN'], ['required' => true])->placeholder = 'mysql://user:[email protected]/mydb';
$form->onSubmit(static function (Form $form) use ($wizard) {
$wizard->memorize('dsn', $form->model->get('dsn'));
$wizard->memorize('dsn', $form->entity->get('dsn'));

return $wizard->jsNext();
});
Expand Down
2 changes: 1 addition & 1 deletion demos/layout/layouts_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
$form->onSubmit(static function (Form $form) {
$errors = [];
foreach (['first_name', 'last_name', 'address'] as $field) {
if (!$form->model->get($field)) {
if (!$form->entity->get($field)) {
$errors[] = $form->jsError($field, 'Field ' . $field . ' is mandatory');
}
}
Expand Down
2 changes: 1 addition & 1 deletion demos/tutorial/intro.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function init(): void
$form->setModel($entity);

$form->onSubmit(static function (Form $form) {
$form->model->save();
$form->entity->save();

return new JsToast('Saved!');
});
Expand Down
Loading

0 comments on commit 5b7d715

Please sign in to comment.