Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify Control readOnly property case to camelCase #1810

Merged
merged 8 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/_includes/DemoLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function initQuickNewRecord()

$buttonSeed = is_string($buttonSeed) ? ['content' => $buttonSeed] : $buttonSeed;

$defaultSeed = [\Atk4\Ui\Button::class, 'class.disabled' => ($this->disabled || $this->readonly)];
$defaultSeed = [\Atk4\Ui\Button::class, 'class.disabled' => ($this->disabled || $this->readOnly)];

$this->action = Factory::factory(array_merge($defaultSeed, (array) $buttonSeed));

Expand Down
18 changes: 9 additions & 9 deletions demos/form-control/input2.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
/** @var \Atk4\Ui\App $app */
require_once __DIR__ . '/../init-app.php';

\Atk4\Ui\Header::addTo($app, ['Disabled and read only form controls (normal / readonly / disabled)']);
\Atk4\Ui\Header::addTo($app, ['Disabled and read only form controls (normal / read only / disabled)']);

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

// Test all kinds of input fields
$group = $form->addGroup('Line');
$group->addControl('line_norm')->set('editable');
$group->addControl('line_read', ['readonly' => true])->set('read only');
$group->addControl('line_read', ['readOnly' => true])->set('read only');
$group->addControl('line_disb', ['disabled' => true])->set('disabled');

$group = $form->addGroup('Text Area');
$group->addControl('text_norm', [Form\Control\Textarea::class])->set('editable');
$group->addControl('text_read', [Form\Control\Textarea::class, 'readonly' => true])->set('read only');
$group->addControl('text_read', [Form\Control\Textarea::class, 'readOnly' => true])->set('read only');
$group->addControl('text_disb', [Form\Control\Textarea::class, 'disabled' => true])->set('disabled');

$group = $form->addGroup('Checkbox');
$group->addControl('c_norm', [Form\Control\Checkbox::class], ['type' => 'boolean'])->set(true);
$group->addControl('c_read', [Form\Control\Checkbox::class, 'readonly' => true], ['type' => 'boolean'])->set(true); // allows to change value
$group->addControl('c_read', [Form\Control\Checkbox::class, 'readOnly' => true], ['type' => 'boolean'])->set(true); // allows to change value
$group->addControl('c_disb', [Form\Control\Checkbox::class, 'disabled' => true], ['type' => 'boolean'])->set(true); // input is not disabled

$group = $form->addGroup('Dropdown');
Expand All @@ -42,13 +42,13 @@
'file' => ['File', 'icon' => 'file icon'],
];
$group->addControl('d_norm', [Form\Control\Dropdown::class, 'values' => $values, 'width' => 'three'])->set('globe');
$group->addControl('d_read', [Form\Control\Dropdown::class, 'values' => $values, 'readonly' => true, 'width' => 'three'])->set('globe'); // allows to change value
$group->addControl('d_read', [Form\Control\Dropdown::class, 'values' => $values, 'readOnly' => true, 'width' => 'three'])->set('globe'); // allows to change value
$group->addControl('d_disb', [Form\Control\Dropdown::class, 'values' => $values, 'disabled' => true, 'width' => 'three'])->set('globe'); // css disabled, but can focus with Tab and change value

$group = $form->addGroup('Radio');

$group->addControl('radio_norm', [Form\Control\Radio::class], ['enum' => ['one', 'two', 'three']])->set('two');
$group->addControl('radio_read', [Form\Control\Radio::class, 'readonly' => true], ['enum' => ['one', 'two', 'three']])->set('two');
$group->addControl('radio_read', [Form\Control\Radio::class, 'readOnly' => true], ['enum' => ['one', 'two', 'three']])->set('two');
$group->addControl('radio_disb', [Form\Control\Radio::class, 'disabled' => true], ['enum' => ['one', 'two', 'three']])->set('two');

$group = $form->addGroup('File upload');
Expand All @@ -62,7 +62,7 @@
$control->onDelete($onDelete);
$control->onUpload($onUpload);

$control = $group->addControl('file_read', [Form\Control\Upload::class, ['accept' => ['.png', '.jpg'], 'readonly' => true]])->set('readonly', 'readonly.jpg');
$control = $group->addControl('file_read', [Form\Control\Upload::class, ['accept' => ['.png', '.jpg'], 'readOnly' => true]])->set('readonly', 'readonly.jpg');
$control->onDelete($onDelete);
$control->onUpload($onUpload);

Expand All @@ -84,7 +84,7 @@
Form\Control\Lookup::class,
'model' => new Country($app->db),
'plus' => true,
'readonly' => true,
'readOnly' => true,
])->set($model->loadAny()->getId());

$group->addControl('Lookup_disb', [
Expand All @@ -97,7 +97,7 @@
$group = $form->addGroup('Calendar');

$group->addControl('date_norm', [Form\Control\Calendar::class, 'type' => 'date'])->set(new \DateTime());
$group->addControl('date_read', [Form\Control\Calendar::class, 'type' => 'date', 'readonly' => true])->set(new \DateTime());
$group->addControl('date_read', [Form\Control\Calendar::class, 'type' => 'date', 'readOnly' => true])->set(new \DateTime());
$group->addControl('date_disb', [Form\Control\Calendar::class, 'type' => 'date', 'disabled' => true])->set(new \DateTime());

$form->onSubmit(function (Form $form) {
Expand Down
2 changes: 1 addition & 1 deletion demos/form-control/multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function init(): void
$sublayout = $form->layout->addSubLayout([Form\Layout\Section\Columns::class]);
$sublayout->addColumn(12);
$column = $sublayout->addColumn(4);
$controlTotal = $column->addControl('total', ['readonly' => true])->set($total);
$controlTotal = $column->addControl('total', ['readOnly' => true])->set($total);

// Update total when qty and box value in any row has changed.
$multiline->onLineChange(function ($rows, Form $form) use ($controlTotal) {
Expand Down
2 changes: 1 addition & 1 deletion docs/form-control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ or you can inject a view with a custom template::
Read only and disabled form controls
------------------------------------

.. php:attr:: readonly
.. php:attr:: readOnly

Read only form controls can be seen in form, can be focused and will be submitted, but we don't allow to
change their value.
Expand Down
2 changes: 1 addition & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ protected function loadPost()
foreach ($this->controls as $k => $control) {
try {
// save field value only if field was editable in form at all
if (!$control->readonly && !$control->disabled) {
if (!$control->readOnly && !$control->disabled) {
$control->set($this->getApp()->ui_persistence->typecastLoadField($control->entityField->getField(), $_POST[$k] ?? null));
}
} catch (\Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions src/Form/AbstractLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ public function setModel(Model $model, array $fields = null): void
$fields = $this->getModelFields($model);
}

// prepare array of controls - check if fields are editable or readOnly/disabled
// prepare array of controls - check if fields are editable or read-only/disabled
$controls = [];
foreach ($fields as $fieldName) {
$field = $model->getField($fieldName);

if ($field->isEditable()) {
$controls[] = [$field->shortName];
} elseif ($field->isVisible()) {
$controls[] = [$field->shortName, ['readonly' => true]];
$controls[] = [$field->shortName, ['readOnly' => true]];
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/Form/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,14 @@ class Control extends View
/**
* Is input field disabled?
* Disabled input fields are not editable and will not be submitted.
*
* @var bool
*/
public $disabled = false;
public bool $disabled = false;

/**
* Is input field read only?
* Read only input fields are not editable, but will be submitted.
*
* @var bool
*/
public $readonly = false;
public bool $readOnly = false;

protected function init(): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function init(): void

protected function renderView(): void
{
if ($this->readonly) {
if ($this->readOnly) {
$this->options['clickOpens'] = false;
}

Expand Down
7 changes: 1 addition & 6 deletions src/Form/Control/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ protected function init(): void
}
}

/**
* Render view.
*/
protected function renderView(): void
{
if ($this->label) {
Expand All @@ -66,12 +63,10 @@ protected function renderView(): void
// We don't want this displayed, because it can only affect "checked" status anyway
$this->content = null;

// take care of readonly status
if ($this->readonly) {
if ($this->readOnly) {
$this->addClass('read-only');
}

// take care of disabled status
if ($this->disabled) {
$this->addClass('disabled');
$this->template->set('disabled', 'disabled="disabled"');
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function getInput()
'type' => $this->inputType,
'id' => $this->name . '_input',
'value' => $this->getValue(),
'readonly' => $this->readonly ? 'readonly' : false,
'readonly' => $this->readOnly ? 'readonly' : false,
'disabled' => $this->disabled ? 'disabled' : false,
], $this->inputAttr));
}
Expand Down Expand Up @@ -252,7 +252,7 @@ protected function renderView(): void

$this->addClass($this->defaultClass);

if ($this->readonly || $this->disabled) {
if ($this->readOnly || $this->disabled) {
$this->setDropdownOption('showOnFocus', false);
$this->setDropdownOption('allowTab', false);
$this->removeClass('search');
Expand All @@ -265,7 +265,7 @@ protected function renderView(): void
$this->addClass('disabled');
}

if ($this->readonly) {
if ($this->readOnly) {
$this->setDropdownOption('allowTab', false);
$this->setDropdownOption('onShow', new JsFunction([new JsExpression('return false')]));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function getInput()
'placeholder' => $this->placeholder,
'id' => $this->name . '_input',
'value' => $this->getValue(),
'readonly' => $this->readonly ? 'readonly' : false,
'readonly' => $this->readOnly ? 'readonly' : false,
'disabled' => $this->disabled ? 'disabled' : false,
], $this->inputAttr));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected function initQuickNewRecord()
$buttonSeed = ['content' => $buttonSeed];
}

$defaultSeed = [\Atk4\Ui\Button::class, 'class.disabled' => ($this->disabled || $this->readonly)];
$defaultSeed = [\Atk4\Ui\Button::class, 'class.disabled' => ($this->disabled || $this->readOnly)];

$this->action = Factory::factory(array_merge($defaultSeed, $buttonSeed));

Expand Down Expand Up @@ -346,7 +346,7 @@ public function getInput()
'type' => 'hidden',
'id' => $this->name . '_input',
'value' => $this->getValue(),
'readonly' => $this->readonly ? 'readonly' : false,
'readonly' => $this->readOnly ? 'readonly' : false,
'disabled' => $this->disabled ? 'disabled' : false,
], $this->inputAttr));
}
Expand Down Expand Up @@ -393,7 +393,7 @@ protected function renderView(): void
$this->template->set('disabled', 'disabled');
}

if ($this->readonly) {
if ($this->readOnly) {
$this->settings['showOnFocus'] = false;
$this->settings['allowTab'] = false;
$this->settings['apiSettings'] = null;
Expand Down
3 changes: 1 addition & 2 deletions src/Form/Control/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ protected function renderView(): void

$this->lister->setModel($this->model);

// take care of readonly and disabled statuses
if ($this->disabled) {
$this->addClass('disabled');
}

$this->lister->onHook(Lister::HOOK_BEFORE_ROW, function (Lister $lister) use ($value) {
if ($this->readonly) {
if ($this->readOnly) {
$lister->t_row->set('disabled', $value !== (string) $lister->model->getId() ? 'disabled="disabled"' : '');
} elseif ($this->disabled) {
$lister->t_row->set('disabled', 'disabled="disabled"');
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getInput()
'rows' => $this->rows,
'placeholder' => $this->placeholder,
'id' => $this->name . '_input',
'readonly' => $this->readonly ? 'readonly' : false,
'readonly' => $this->readOnly ? 'readonly' : false,
'disabled' => $this->disabled ? 'disabled' : false,
], $this->inputAttr),
(string) $this->getValue() // need to cast to string to avoid null values which break html markup
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function init(): void
$this->cb = \Atk4\Ui\JsCallback::addTo($this);

if (!$this->action) {
$this->action = new \Atk4\Ui\Button(['icon' => 'upload', 'class.disabled' => ($this->disabled || $this->readonly)]);
$this->action = new \Atk4\Ui\Button(['icon' => 'upload', 'class.disabled' => ($this->disabled || $this->readOnly)]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ForFieldUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ public function testRegularField(): void
$f = new \Atk4\Ui\Form();
$f->invokeInit();
$f->setModel($this->m->createEntity());
$this->assertFalse($f->getControl('regular_field')->readonly);
$this->assertFalse($f->getControl('regular_field')->readOnly);
}

public function testJustDataField(): void
{
$f = new \Atk4\Ui\Form();
$f->invokeInit();
$f->setModel($this->m->createEntity(), ['just_for_data']);
$this->assertTrue($f->getControl('just_for_data')->readonly);
$this->assertTrue($f->getControl('just_for_data')->readOnly);
}

public function testShowInUi(): void
{
$f = new \Atk4\Ui\Form();
$f->invokeInit();
$f->setModel($this->m->createEntity());
$this->assertFalse($f->getControl('no_persist_but_show_in_ui')->readonly);
$this->assertFalse($f->getControl('no_persist_but_show_in_ui')->readOnly);
}
}
3 changes: 3 additions & 0 deletions tests/HtmlTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ public function testGetTagTree(): void
public function testGetTagRefNotFoundException(): void
{
$t = new HtmlTemplate('{foo}hello{/}');

$this->expectException(Exception::class);
$t->getTagTree('bar');
}

public function testLoadFromFileNonExistentFileException(): void
{
$t = new HtmlTemplate();

$this->expectException(Exception::class);
$t->loadFromFile(__DIR__ . '/bad_template_file');
}
Expand All @@ -80,6 +82,7 @@ public function testHasTag(): void
public function testSetBadTypeException(): void
{
$t = new HtmlTemplate('{foo}hello{/} guys');

$this->expectException(Exception::class);
$t->set('foo', new \stdClass()); // @phpstan-ignore-line
}
Expand Down
3 changes: 2 additions & 1 deletion tests/SessionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ protected function tearDown(): void

public function testException1(): void
{
$m = new SessionWithoutNameMock($this->app);

// when try to start session without NameTrait
$this->expectException(Exception::class);
$m = new SessionWithoutNameMock($this->app);
$m->memorize('test', 'foo');
}

Expand Down