1
0
mirror of https://github.com/chylex/Lightning-Tracker.git synced 2025-04-10 02:15:43 +02:00

Improve forms (disabled field & pre-fill handling)

This commit is contained in:
chylex 2020-08-05 11:28:23 +02:00
parent c76cb42f6b
commit e0bb045859
4 changed files with 29 additions and 5 deletions

View File

@ -19,7 +19,7 @@ abstract class AbstractFormField implements IFormField{
return $this;
}
public function disabled(): self{
public function disable(): self{
$this->disabled = true;
return $this;
}
@ -32,6 +32,10 @@ abstract class AbstractFormField implements IFormField{
$this->errors[] = $message;
}
public function isDisabled(): bool{
return $this->disabled;
}
public function acceptsMissingField(): bool{
return false;
}

View File

@ -10,6 +10,7 @@ use Pages\Components\Forms\Elements\FormButton;
use Pages\Components\Forms\Elements\FormCheckBox;
use Pages\Components\Forms\Elements\FormHiddenValue;
use Pages\Components\Forms\Elements\FormIconButton;
use Pages\Components\Forms\Elements\FormSelect;
use Pages\Components\Forms\Elements\FormSplitGroupEnd;
use Pages\Components\Forms\Elements\FormSplitGroupStart;
use Pages\Components\Forms\Elements\FormTextArea;
@ -35,6 +36,8 @@ HTML;
}
private string $id;
private bool $is_filled = false;
private ?string $confirm_message = null;
/**
@ -120,12 +123,22 @@ HTML;
$this->elements[] = Text::plain($html);
}
public function isFilled(): bool{
return $this->is_filled;
}
/**
* Fills form fields using the provided data.
* @param array $data
*/
public function fill(array $data){
$this->is_filled = true;
foreach($this->fields as $name => $field){
if ($field->isDisabled()){
continue;
}
if (isset($data[$name]) || $field->acceptsMissingField()){
$field->value($data[$name] ?? null);
}
@ -141,10 +154,16 @@ HTML;
if (!isset($data[self::ACTION_KEY]) || $data[self::ACTION_KEY] !== $this->id){
return false;
}
$this->is_filled = true;
$filled_fields = 0;
foreach($this->fields as $name => $field){
if ($field->isDisabled()){
$filled_fields++;
continue;
}
if (isset($data[$name]) || $field->acceptsMissingField()){
$field->value($data[$name] ?? null);
$filled_fields++;

View File

@ -7,11 +7,12 @@ use Pages\IViewable;
interface IFormField extends IViewable{
public function value(string $value): IFormField;
public function disabled(): IFormField;
public function disable(): IFormField;
public function getName(): string;
public function addError(string $message): void;
public function isDisabled(): bool;
public function acceptsMissingField(): bool;
}

View File

@ -62,8 +62,8 @@ HTML;
$form = new FormComponent();
$form->startSplitGroup(50);
$form->addTextField('Name')->label('Username')->value($logon_user->getNameSafe())->disabled();
$form->addTextField('Email')->value($logon_user->getEmailSafe())->disabled();
$form->addTextField('Name')->label('Username')->value($logon_user->getNameSafe())->disable();
$form->addTextField('Email')->value($logon_user->getEmailSafe())->disable();
$form->endSplitGroup();
echo <<<HTML