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

Form Improvements #165

Merged
merged 6 commits into from
May 29, 2017
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/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function init()
$this->addFields(['start_date', 'finish_date'], ['type'=>'date']);
$this->addField('finish_time', ['type'=>'time']);

$this->addFields(['created', 'updated'], ['type'=>'datetime']);
$this->addFields(['created', 'updated'], ['type'=>'datetime', 'ui'=>['form'=>['FormField/Line', 'disabled'=>true]]]);
}
}

Expand Down
5 changes: 4 additions & 1 deletion demos/form3.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
$form->onSubmit(function ($form) {
$errors = [];
foreach ($form->model->dirty as $field => $value) {
if ($form->model->getElement($field)->never_persist) {
continue;
}
$errors[] = $form->error($field, 'Value was changed, '.json_encode($value).' to '.json_encode($form->model[$field]));
}

return $errors ?: $form->success('No changed fields');
return $errors ?: 'No fields were changed';
});
3 changes: 3 additions & 0 deletions demos/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
$g->menu->addItem(['Re-Import', 'icon'=>'power'], new \atk4\ui\jsReload($g));
$g->menu->addItem(['Delete All', 'icon'=>'trash', 'red active']);

$g->addColumn(['Template', 'hello<b>world</b>']);
$g->addFormatter('name', ['Link', 'page2']);

$g->addAction('Say HI', function ($j, $id) use ($g) {
return 'Loaded "'.$g->model->load($id)['name'].'" from ID='.$id;
});
Expand Down
29 changes: 29 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ public function _fieldFactory(\atk4\data\Field $f)
return new FormField\Dropdown($arg);
}

// Field values can be picked from the model.
if (isset($f->reference)) {
//$arg['values'] = $f->ref();

$dd = new FormField\Dropdown($arg);
$dd->setModel($f->reference->refModel());

return $dd;
}

switch ($f->type) {
case 'boolean':
return new FormField\Checkbox($arg);
Expand All @@ -346,6 +356,25 @@ public function _fieldFactory(\atk4\data\Field $f)
case 'password':
return new FormField\Password($arg);

case 'datetime':
$arg['options']['ampm'] = false;

return new FormField\Calendar($arg);

case 'date':
$arg['type'] = 'date';

return new FormField\Calendar($arg);

case 'time':
$arg['type'] = 'time';
$arg['options']['ampm'] = false;

return new FormField\Calendar($arg);

case 'money':
return new FormField\Money($arg);

case null:
return new FormField\Line($arg);

Expand Down
39 changes: 39 additions & 0 deletions src/FormField/Calendar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace atk4\ui\FormField;

use atk4\ui\Form;

/**
* Input element for a form field.
*/
class Calendar extends Input
{
/**
* Set this to 'date', 'time', 'month' or 'year'. Leaving this blank
* will show both date and time.
*/
public $type = null;

/**
* Any other options you'd like to pass to calendar JS.
*/
public $options = [];

public function renderView()
{
if (!$this->icon) {
switch ($this->type) {
//case 'date': $this->icon = '
}
}

if ($this->type) {
$this->options['type'] = $this->type;
}

$this->js(true)->calendar($this->options);

parent::renderView();
}
}
29 changes: 24 additions & 5 deletions src/FormField/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Dropdown extends Input

public $values = [];

public $empty = '...';

public function init()
{
parent::init();
Expand All @@ -27,14 +29,31 @@ public function getInput()
$value = isset($this->field) ? $this->app->ui_persistence->typecastSaveField($this->field, $this->field->get()) : $this->content ?: '';

$options = [];
foreach ($this->values as $key=>$val) {
$item = ['option', 'value'=>(string) $key, $val];
if ($value == $val) {
$item['selected'] = true;
}
if ($this->empty) {
$item = ['option', 'value'=>'', $this->empty];
$options[] = $item;
}

if (isset($this->model)) {
foreach ($this->model as $key=>$row) {
$title = $row[$row->title_field];

$item = ['option', 'value'=>(string) $key, $title];
if ($value == $key) {
$item['selected'] = true;
}
$options[] = $item;
}
} else {
foreach ($this->values as $key=>$val) {
$item = ['option', 'value'=>(string) $key, $val];
if ($value == $key) {
$item['selected'] = true;
}
$options[] = $item;
}
}

return $this->app->getTag('select', [
'name' => $this->short_name,
'type' => $this->inputType,
Expand Down
10 changes: 9 additions & 1 deletion src/FormField/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public function jsInput($when = null, $action = null)
return $this->js($when, $action, '#'.$this->id.'_input');
}

/**
* Returns presentable value to be inserted into input tag.
*/
public function getValue()
{
return isset($this->field) ? $this->app->ui_persistence->typecastSaveField($this->field, $this->field->get()) : $this->content ?: '';
}

/**
* returns <input .../> tag.
*/
Expand All @@ -61,7 +69,7 @@ public function getInput()
'type' => $this->inputType,
'placeholder'=> $this->placeholder,
'id' => $this->id.'_input',
'value' => isset($this->field) ? $this->app->ui_persistence->typecastSaveField($this->field, $this->field->get()) : $this->content ?: '',
'value' => $this->getValue(),
]);
//return '<input name="'.$this->short_name.'" type="'.$this->inputType.'" placeholder="'.$this->placeholder.'" id="'.$this->id.'_input"/>';
}
Expand Down
31 changes: 31 additions & 0 deletions src/FormField/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace atk4\ui\FormField;

use atk4\ui\Form;

/**
* Input element for a form field.
*/
class Money extends Input
{
public function getValue()
{
$v = $this->field ? $this->field->get() : ($this->content ?: null);

if (is_null($v)) {
return;
}

return number_format($v, 2);
}

public function renderView()
{
if ($this->label === null) {
$this->label = $this->app->ui_persistence->currency;
}

parent::renderView();
}
}
7 changes: 4 additions & 3 deletions src/Persistence/UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
*/
class UI extends \atk4\data\Persistence
{
public $date_format = 'd/m/Y';
public $date_format = 'M d, Y';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this format better that d/m/Y ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not confusing for Americans and is working with the calendar widget.


public $time_format = 'h:i:s';
public $time_format = 'H:i';

public $datetime_format = 'D, d M Y H:i:s O';
public $datetime_format = 'M d, Y H:i:s';
// 'D, d M Y H:i:s O';

public $currency = '€';

Expand Down
11 changes: 9 additions & 2 deletions src/TableColumn/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ class Template extends Generic
*/
public function __construct($template)
{
$this->template = $template;
$this->template = is_object($template) ? $template : new \atk4\ui\Template($template);
}

public function getDataCellHTML(\atk4\data\Field $f = null)
{
return $this->getTag('body', $this->template);
return $this->getTag('body', '{$c_'.$this->short_name.'}');
}

public function getHtmlTags($row, $field)
{
$this->table->add($this->template);

return ['c_'.$this->short_name => $this->template->set($row)->render()];
}
}
4 changes: 3 additions & 1 deletion template/semantic-ui/html.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<head>
<title>{title}Agile UI - Untitled Project{/}</title>{$meta}
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/mdehoog/Semantic-UI-Calendar/0.0.8/dist/calendar.css">
<script src="https://cdn.rawgit.com/mdehoog/Semantic-UI-Calendar/0.0.8/dist/calendar.min.js"></script>
<script>
$.fn.api.settings.successTest = function(response) {
if(response && response.eval) {
Expand Down
6 changes: 5 additions & 1 deletion template/semantic-ui/html.pug
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ html(lang="{lang}en{/}")
title {title}Agile UI - Untitled Project{/}
| {$meta}
meta(charset='utf-8')
link(rel='stylesheet', type='text/css', href='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.css')
script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js')
script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery-serialize-object/2.5.0/jquery.serialize-object.min.js')

link(rel='stylesheet', type='text/css', href='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.css')
script(src='https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.min.js')

link(rel='stylesheet', type='text/css', href='https://cdn.rawgit.com/mdehoog/Semantic-UI-Calendar/0.0.8/dist/calendar.css')
script(src='https://cdn.rawgit.com/mdehoog/Semantic-UI-Calendar/0.0.8/dist/calendar.min.js')
script.
$.fn.api.settings.successTest = function(response) {
if(response && response.eval) {
Expand Down