Skip to content

Commit

Permalink
rename Form\Control::$field to $entityField
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Nov 7, 2021
1 parent d502755 commit e2c2508
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 114 deletions.
2 changes: 1 addition & 1 deletion demos/form-control/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
'caption' => 'Javascript action',
'options' => ['clickOpens' => false],
])->set(new \DateTime());
$control->addAction(['Today', 'icon' => 'calendar day'])->on('click', $control->getJsInstance()->setDate($app->ui_persistence->typecastSaveField($control->field->getField(), new \DateTime())));
$control->addAction(['Today', 'icon' => 'calendar day'])->on('click', $control->getJsInstance()->setDate($app->ui_persistence->typecastSaveField($control->entityField->getField(), new \DateTime())));
$control->addAction(['Select...', 'icon' => 'calendar'])->on('click', $control->getJsInstance()->open());
$control->addAction(['Clear', 'icon' => 'times red'])->on('click', $control->getJsInstance()->clear());

Expand Down
2 changes: 1 addition & 1 deletion demos/javascript/vue-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
\Atk4\Ui\Header::addTo($app, ['Inline editing.', 'size' => 3, 'subHeader' => $subHeader]);

$inline_edit = \Atk4\Ui\Component\InlineEdit::addTo($app);
$inline_edit->field = $model->fieldName()->name;
$inline_edit->fieldName = $model->fieldName()->name;
$inline_edit->setModel($model);

$inline_edit->onChange(function ($value) {
Expand Down
2 changes: 1 addition & 1 deletion docs/form-control.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Link to Model Field
.. php:attr:: field
Form decorator defines $field property which will be pointing to a field object of a model, so technically
the value of the field would be read from `$decorator->field->get()`.
the value of the field would be read from `$decorator->entityField->get()`.

.. php:namespace:: Atk4\Ui\Form\Control
Expand Down
16 changes: 8 additions & 8 deletions src/Component/InlineEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class InlineEdit extends View
*
* @var string|null the name of the field
*/
public $field;
public $fieldName;

/**
* Whether component should save it's value when input get blur.
Expand Down Expand Up @@ -91,7 +91,7 @@ protected function init(): void
// Set default validation error handler.
if (!$this->formatErrorMsg || !($this->formatErrorMsg instanceof \Closure)) {
$this->formatErrorMsg = function ($e, $value) {
$caption = $this->model->getField($this->field)->getCaption();
$caption = $this->model->getField($this->fieldName)->getCaption();

return $caption . ' - ' . $e->getMessage() . '. <br>Trying to set this value: "' . $value . '"';
};
Expand All @@ -106,12 +106,12 @@ protected function init(): void
public function setModel(\Atk4\Data\Model $model)
{
parent::setModel($model);
$this->field = $this->field ?: $this->model->title_field;
$this->fieldName = $this->fieldName ?: $this->model->title_field;
if ($this->autoSave && $this->model->loaded()) {
$value = $_POST['value'] ?? null;
$this->cb->set(function () use ($value) {
try {
$this->model->set($this->field, $this->getApp()->ui_persistence->typecastLoadField($this->model->getField($this->field), $value));
$this->model->set($this->fieldName, $this->getApp()->ui_persistence->typecastLoadField($this->model->getField($this->fieldName), $value));
$this->model->save();

return $this->jsSuccess('Update successfully');
Expand All @@ -137,7 +137,7 @@ public function setModel(\Atk4\Data\Model $model)
public function onChange(\Closure $fx)
{
if (!$this->autoSave) {
$value = $this->getApp()->ui_persistence->typecastLoadField($this->model->getModel()->getField($this->field), $_POST['value'] ?? null);
$value = $this->getApp()->ui_persistence->typecastLoadField($this->model->getModel()->getField($this->fieldName), $_POST['value'] ?? null);
$this->cb->set(function () use ($fx, $value) {
return $fx($value);
});
Expand Down Expand Up @@ -185,20 +185,20 @@ protected function renderView(): void
{
parent::renderView();

$type = $this->model && $this->field ? $this->model->getModel()->getField($this->field)->type : 'text';
$type = $this->model && $this->fieldName ? $this->model->getModel()->getField($this->fieldName)->type : 'text';
$type = $type === 'string' ? 'text' : $type;

if ($type !== 'text' && $type !== 'integer') {
throw new Exception('Only string or number field can be edited inline. Field Type = ' . $type);
}

if ($this->model && $this->model->loaded()) {
$initValue = $this->model->get($this->field);
$initValue = $this->model->get($this->fieldName);
} else {
$initValue = $this->initValue;
}

$fieldName = $this->field ?: 'name';
$fieldName = $this->fieldName ?: 'name';

$this->vue('atk-inline-edit', [
'initValue' => $initValue,
Expand Down
6 changes: 3 additions & 3 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Atk4\Core\Factory;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Data\Model\EntityField;
use Atk4\Data\Reference\ContainsMany;
use Atk4\Ui\Form\Control;
use Atk4\Ui\Form\Control\EntityFieldProxy;

/**
* Implements a form.
Expand Down Expand Up @@ -523,7 +523,7 @@ public function controlFactory(Model $entity, Field $field, $ControlSeed = []):

$defaults = [
'form' => $this,
'field' => new EntityFieldProxy($entity, $field->short_name),
'entityField' => new EntityField($entity, $field->short_name),
'short_name' => $field->short_name,
];

Expand Down Expand Up @@ -557,7 +557,7 @@ protected function loadPost()
try {
// save field value only if field was editable in form at all
if (!$control->readonly && !$control->disabled) {
$control->set($this->getApp()->ui_persistence->typecastLoadField($control->field->getField(), $_POST[$k] ?? null));
$control->set($this->getApp()->ui_persistence->typecastLoadField($control->entityField->getField(), $_POST[$k] ?? null));
}
} catch (\Exception $e) {
$messages = [];
Expand Down
18 changes: 9 additions & 9 deletions src/Form/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Atk4\Ui\Form;

use Atk4\Data\Model\EntityField;
use Atk4\Ui\Exception;
use Atk4\Ui\Form;
use Atk4\Ui\Form\Control\EntityFieldProxy;
use Atk4\Ui\View;

/**
Expand All @@ -20,9 +20,9 @@ class Control extends View
public $form;

/**
* @var EntityFieldProxy
* @var EntityField
*/
public $field;
public $entityField;

/** @var string */
public $controlClass = '';
Expand Down Expand Up @@ -80,12 +80,12 @@ protected function init(): void
{
parent::init();

if ($this->form && $this->field) {
if (isset($this->form->controls[$this->field->getFieldName()])) {
if ($this->form && $this->entityField) {
if (isset($this->form->controls[$this->entityField->getFieldName()])) {
throw (new Exception('Form already has a field with the same name'))
->addMoreInfo('name', $this->field->getFieldName());
->addMoreInfo('name', $this->entityField->getFieldName());
}
$this->form->controls[$this->field->getFieldName()] = $this;
$this->form->controls[$this->entityField->getFieldName()] = $this;
}
}

Expand All @@ -100,8 +100,8 @@ protected function init(): void
*/
public function set($value = null, $junk = null)
{
if ($this->field) {
$this->field->set($value);
if ($this->entityField) {
$this->entityField->set($value);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Control/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function init(): void
// workaround. Otherwise send boolean "true".
if ($this->form) {
$this->form->onHook(Form::HOOK_LOAD_POST, function ($form, &$postRawData) {
$postRawData[$this->field->getFieldName()] = isset($postRawData[$this->field->getFieldName()]);
$postRawData[$this->entityField->getFieldName()] = isset($postRawData[$this->entityField->getFieldName()]);
});
}
}
Expand All @@ -64,7 +64,7 @@ protected function renderView(): void
$this->template->set('Content', $this->label);
}

if ($this->field ? $this->field->get() : $this->content) {
if ($this->entityField ? $this->entityField->get() : $this->content) {
$this->template->set('checked', 'checked');
}

Expand Down
12 changes: 6 additions & 6 deletions src/Form/Control/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public function getInput()
*/
public function getValue()
{
return $this->field !== null
? (is_array($this->field->get()) ? implode(',', $this->field->get()) : $this->field->get())
return $this->entityField !== null
? (is_array($this->entityField->get()) ? implode(',', $this->entityField->get()) : $this->entityField->get())
: parent::getValue();
}

Expand All @@ -193,11 +193,11 @@ public function getValue()
*/
public function set($value = null, $junk = null)
{
if ($this->field) {
if ($this->field->getField()->type === 'json' && is_string($value)) {
if ($this->entityField) {
if ($this->entityField->getField()->type === 'json' && is_string($value)) {
$value = explode(',', $value);
}
$this->field->set($value);
$this->entityField->set($value);

return $this;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ protected function jsRenderDropdown(): JsExpressionable
protected function htmlRenderValue(): void
{
// add selection only if no value is required and Dropdown has no multiple selections enabled
if ($this->field !== null && !$this->field->getField()->required && !$this->isMultiple) {
if ($this->entityField !== null && !$this->entityField->getField()->required && !$this->isMultiple) {
$this->_tItem->set('value', '');
$this->_tItem->set('title', $this->empty || is_numeric($this->empty) ? (string) $this->empty : '');
$this->template->dangerouslyAppendHtml('Item', $this->_tItem->renderToHtml());
Expand Down
8 changes: 4 additions & 4 deletions src/Form/Control/DropdownCascade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ protected function init(): void
}

$cascadeFromValue = isset($_POST[$this->cascadeFrom->name])
? $this->getApp()->ui_persistence->typecastLoadField($this->cascadeFrom->field->getField(), $_POST[$this->cascadeFrom->name])
: $this->cascadeFrom->field->get();
? $this->getApp()->ui_persistence->typecastLoadField($this->cascadeFrom->entityField->getField(), $_POST[$this->cascadeFrom->name])
: $this->cascadeFrom->entityField->get();

$this->model = $this->cascadeFrom->model ? $this->cascadeFrom->model->ref($this->reference) : null;

// populate default dropdown values
$this->dropdownOptions['values'] = $this->getJsValues($this->getNewValues($cascadeFromValue), $this->field->get());
$this->dropdownOptions['values'] = $this->getJsValues($this->getNewValues($cascadeFromValue), $this->entityField->get());

// js to execute for the onChange handler of the parent dropdown.
$expr = [
Expand All @@ -62,7 +62,7 @@ function ($t) use ($cascadeFromValue) {
*/
public function set($value = null, $junk = null)
{
$this->dropdownOptions['values'] = $this->getJsValues($this->getNewValues($this->cascadeFrom->field->get()), $value);
$this->dropdownOptions['values'] = $this->getJsValues($this->getNewValues($this->cascadeFrom->entityField->get()), $value);

return parent::set($value, $junk);
}
Expand Down
60 changes: 0 additions & 60 deletions src/Form/Control/EntityFieldProxy.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Form/Control/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public function setInputAttr($attr, $value = null)
*/
public function getValue()
{
return $this->field !== null
? $this->getApp()->ui_persistence->typecastSaveField($this->field->getField(), $this->field->get())
return $this->entityField !== null
? $this->getApp()->ui_persistence->typecastSaveField($this->entityField->getField(), $this->entityField->get())
: ($this->content ?? '');
}

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 @@ -434,17 +434,17 @@ protected function renderView(): void

$this->initDropdown($chain);

if ($this->field && $this->field->get()) {
if ($this->entityField && $this->entityField->get()) {
$id_field = $this->id_field ?: $this->model->id_field;

$this->model = $this->model->tryLoadBy($id_field, $this->field->get());
$this->model = $this->model->tryLoadBy($id_field, $this->entityField->get());

if ($this->model->loaded()) {
$row = $this->renderRow($this->model);

$chain->dropdown('set value', $row['value'])->dropdown('set text', $row['title']);
} else {
$this->field->setNull();
$this->entityField->setNull();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Money extends Input
{
public function getValue()
{
$v = $this->field ? $this->field->get() : ($this->content ?: null);
$v = $this->entityField ? $this->entityField->get() : ($this->content ?: null);

if ($v === null) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ public function onLineChange(\Closure $fx, array $fields): void
*/
public function getValue(): string
{
if ($this->field->getField()->type === 'json') {
$jsonValues = $this->getApp()->ui_persistence->typecastSaveField($this->field->getField(), $this->field->get() ?? []);
if ($this->entityField->getField()->type === 'json') {
$jsonValues = $this->getApp()->ui_persistence->typecastSaveField($this->entityField->getField(), $this->entityField->get() ?? []);
} else {
// set data according to hasMany ref. or using model.
$model = $this->getModel();
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function renderView(): void
$this->setSource($this->values);
}

$value = $this->field ? $this->field->get() : $this->content;
$value = $this->entityField ? $this->entityField->get() : $this->content;

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

Expand Down
6 changes: 3 additions & 3 deletions src/Form/Control/ScopeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected function init(): void

if ($this->form) {
$this->form->onHook(\Atk4\Ui\Form::HOOK_LOAD_POST, function ($form, &$postRawData) {
$key = $this->field->getFieldName();
$key = $this->entityField->getFieldName();
$postRawData[$key] = $this->queryToScope($this->getApp()->decodeJson($postRawData[$key] ?? '{}'));
});
}
Expand Down Expand Up @@ -366,8 +366,8 @@ protected function buildQuery(Model $model)
// this is used when selecting proper operator for the inputType (see self::$operatorsMap)
$inputsMap = array_column($this->rules, 'inputType', 'id');

if ($this->field && $this->field->get() !== null) {
$scope = $this->field->get();
if ($this->entityField && $this->entityField->get() !== null) {
$scope = $this->entityField->get();
} else {
$scope = $model->scope();
}
Expand Down
Loading

0 comments on commit e2c2508

Please sign in to comment.