Skip to content

Commit

Permalink
Drop Form::jsInput() shortcut method (#2120)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Oct 12, 2023
1 parent 04aba5b commit 2a84a1a
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 22 deletions.
17 changes: 17 additions & 0 deletions demos/_unit-test/form-empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Atk4\Ui\Demos;

use Atk4\Ui\Form;
use Atk4\Ui\Js\JsToast;

/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

$form = Form::addTo($app);

$form->onSubmit(static function (Form $form) {
return new JsToast('Post ' . ($form->getApp()->getRequest()->getParsedBody() === [] ? 'ok' : 'unexpected'));
});
5 changes: 5 additions & 0 deletions demos/form-control/multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsFunction;
use Atk4\Ui\Js\JsToast;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use SebastianBergmann\CodeCoverage\CodeCoverage;

/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';
Expand All @@ -24,6 +26,9 @@
$inventory->getField($inventory->fieldName()->box)->ui['multiline'] = [Form\Control\Multiline::TABLE_CELL => ['width' => 2]];
$inventory->getField($inventory->fieldName()->total_sql)->ui['multiline'] = [Form\Control\Multiline::TABLE_CELL => ['width' => 1, 'class' => 'blue']];
$inventory->getField($inventory->fieldName()->total_php)->ui['multiline'] = [Form\Control\Multiline::TABLE_CELL => ['width' => 1, 'class' => 'blue']];
if ($app->db->getDatabasePlatform() instanceof PostgreSQLPlatform || class_exists(CodeCoverage::class, false)) {
$inventory->setOrder($inventory->idField);
}

$form = Form::addTo($app);

Expand Down
2 changes: 1 addition & 1 deletion demos/form/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
}

return new JsBlock([
$form->jsInput('email')->val('[email protected]'),
$form->getControl('email')->jsInput()->val('[email protected]'),
$form->getControl('is_accept_terms')->js()->checkbox('set checked'),
]);
});
Expand Down
4 changes: 2 additions & 2 deletions demos/init-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ public function addField(string $name, $seed = []): Field
* @property string $sys_name @Atk4\Field()
* @property string $iso @Atk4\Field()
* @property string $iso3 @Atk4\Field()
* @property string $numcode @Atk4\Field()
* @property string $phonecode @Atk4\Field()
* @property int $numcode @Atk4\Field()
* @property int $phonecode @Atk4\Field()
*/
class Country extends ModelWithPrefixedFields
{
Expand Down
19 changes: 7 additions & 12 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,6 @@ public function addGroup($title = null)
return $this->layout->addGroup($title);
}

/**
* Returns JS Chain that targets INPUT element of a specified field. This method is handy
* if you wish to set a value to a certain field.
*
* @return Jquery
*/
public function jsInput(string $name): JsExpressionable
{
return $this->layout->getControl($name)->jsInput();
}

// }}}

// {{{ Internals
Expand Down Expand Up @@ -443,7 +432,12 @@ protected function loadPost(): void
foreach ($this->controls as $k => $control) {
// save field value only if field was editable in form at all
if (!$control->readOnly && !$control->disabled) {
$postRawValue = $postRawData[$k];
$postRawValue = $postRawData[$k] ?? null;
if ($postRawValue === null) {
throw (new Exception('Form POST param does not exist'))
->addMoreInfo('key', $k);
}

try {
$control->set($this->getApp()->uiPersistence->typecastLoadField($control->entityField->getField(), $postRawValue));
} catch (\Exception $e) {
Expand Down Expand Up @@ -527,6 +521,7 @@ public function setupAjaxSubmit(): void
'on' => 'submit',
'url' => $this->cb->getJsUrl(),
'method' => 'POST',
'contentType' => 'application/x-www-form-urlencoded; charset=UTF-8', // remove once https://github.com/jquery/jquery/issues/5346 is fixed
'serializeForm' => true,
], $this->apiConfig));

Expand Down
8 changes: 4 additions & 4 deletions src/Form/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ public function onChange($expr, $defaults = []): void
*
* $field->jsInput(true)->val(123);
*
* @param bool|string $when
* @param JsExpressionable $action
* @param bool|string $when
* @param ($when is false ? null : JsExpressionable|null) $action
*
* @return Jquery
* @return ($action is null ? Jquery : null)
*/
public function jsInput($when = false, $action = null): JsExpressionable
public function jsInput($when = false, $action = null): ?JsExpressionable
{
return $this->js($when, $action, '#' . $this->name . '_input');
}
Expand Down
2 changes: 1 addition & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ public function getHtml()
*
* @return ($action is null ? Jquery : null)
*/
public function js($when = false, $action = null, $selector = null)
public function js($when = false, $action = null, $selector = null): ?JsExpressionable
{
// binding on a specific event
// TODO allow only boolean $when, otherwise user should use self::on() method
Expand Down
5 changes: 5 additions & 0 deletions tests-behat/form.feature
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ Feature: Form
When I click using selector "//label[text()='I am a developer']"
Then I should not see "Check all language that apply"
Then I should not see "Css"

Scenario: empty POST
Given I am on "_unit-test/form-empty.php"
When I press button "Save"
Then Toast display should contain text "Post ok"
9 changes: 9 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,13 @@ public function testUrl(string $requestUrl, array $appStickyGetArguments, array
$expectedUrlAutoindex2 = $makeExpectedUrlFx('', '.html');
self::assertSame($expectedUrlAutoindex2, $app->url($page, $extraRequestUrlArgs));
}

public function testTerminateNoContentTypeException(): void
{
$app = $this->createApp();

$this->expectException(Exception::class);
$this->expectExceptionMessage('Content type must be always set');
$app->terminate();
}
}
10 changes: 8 additions & 2 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,13 @@ public function testDemoAssertJsonResponsePost(string $path, array $postData): v
/**
* @dataProvider provideDemoCallbackErrorCases
*/
public function testDemoCallbackError(string $path, string $expectedExceptionMessage): void
public function testDemoCallbackError(string $path, string $expectedExceptionMessage, array $options = []): void
{
if (static::class === self::class) {
$this->expectExceptionMessage($expectedExceptionMessage);
}

$response = $this->getResponseFromRequest5xx($path);
$response = $this->getResponseFromRequest5xx($path, $options);

self::assertSame(500, $response->getStatusCode());
self::assertSame('text/html', preg_replace('~;\s*charset=.+$~', '', $response->getHeaderLine('Content-Type')));
Expand Down Expand Up @@ -540,6 +540,12 @@ public function provideDemoCallbackErrorCases(): iterable
'_unit-test/callback-nested.php?err_sub_loader2&' . Callback::URL_QUERY_TRIGGER_PREFIX . 'trigger_main_loader=callback&' . Callback::URL_QUERY_TRIGGER_PREFIX . 'trigger_sub_loader=callback&' . Callback::URL_QUERY_TARGET . '=trigger_sub_loader',
'Exception II from Sub Loader',
];

yield [
'_unit-test/post.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'test_submit=ajax&' . Callback::URL_QUERY_TARGET . '=test_submit',
'Form POST param does not exist',
['form_params' => []],
];
}
}

Expand Down

0 comments on commit 2a84a1a

Please sign in to comment.