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

Feature/form2 #30

Merged
merged 48 commits into from Feb 7, 2017

Conversation

Projects
None yet
2 participants
@romaninsh
Member

romaninsh commented Jan 5, 2017

This PR implements Forms:

$form = $layout->add('Form');
$form->addField('email');

Features

  • Rendering a valid HTML form:

    • including form fields
    • field grouping (more than one field per line)
    • adds and links labels, placeholders and hints
    • allows any "Field" variations (see :ref:field) - checkboxes, drop-downs, etc.
    • automatically sets ID for form/fields/labels
    • supports automated layouts or you can define a custom one
  • Integration with Model objects:

    • automatically populate all or specific fields
    • use of semi-automated layouting (you can arrange group of fields)
    • respect caption and other ui-related settins defined in a model
    • map field "type" into appropriate :ref:field
  • Augment Form with JS integration:

    • form is submitted using JavaScript
    • during submit, the loading indicator is shown
    • javascript sends data through POST
    • POST data is automatically parsed and imported into Model
  • You may define onSubmit PHP handler that:

    • can execute validate, save data and make decisions in PHP code
    • display errors for single or multiple fields
    • perform custom actions on "input" element such as insert value
    • perform custom action on "field" div, such as use checkbox APIs.
    • execute any other arbitrary JavaScript or jQuery code
    • indicate successful completion of a form through a nicely formatted message

More complex example

$f = $layout->add(new \atk4\ui\Form(['segment']));
$f->setModel(new \atk4\data\Model());

$f->addHeader('Example fields added one-by-one');
$f->addField('name');
$f->addField('email');

$f->addHeader('Example of field grouping');
$gr = $f->addGroup('Address with label');
$gr->addField('address', ['width'=>'twelve']);
$gr->addField('code', ['Post Code', 'width'=>'four']);

$gr = $f->addGroup(['n'=>'two']);
$gr->addField('city');
$gr->addField('country');

$gr = $f->addGroup(['Name', 'inline'=>true]);
$gr->addField('first_name', ['width'=>'eight']);
$gr->addField('middle_name', ['width'=>'three', 'disabled'=>true]);
$gr->addField('last_name', ['width'=>'five']);

$f->onSubmit(function ($f) {
    $errors = [];

    foreach ($f->model->elements as $name=>$ff) {
        if ($name == 'id') {
            continue;
        }

        if ($f->model[$name] != 'a') {
            $errors[] = $f->error($name, 'Field '.$name.' should contain exactly "a", but contains '.$f->model[$name]);
        }
    }

    return $errors ?: $f->success('No more errors', 'so we have saved everything into the database');
});

Demo

The source code above alone will produce the form as demonstrated here:

http://ui.agiletoolkit.org/demos/form.php

For full source that you can download and run yourself:

https://github.com/atk4/ui/blob/60b8c5820dfb20ba33d8f9481522bdd683858acd/demos/form.php

Documentation

Included. You can read here: https://github.com/atk4/ui/blob/b77119884c59a55b61fbdd1913d1ef542fa3fcae/docs/form.rst

romaninsh added some commits Jan 19, 2017

@romaninsh

This comment has been minimized.

Member

romaninsh commented Jan 19, 2017

This PR depends on atk4/data#214 (which is why build fails)

@romaninsh romaninsh added in review and removed in progress labels Jan 19, 2017

@romaninsh romaninsh requested review from DarkSide666 and skondakov Jan 19, 2017

romaninsh and others added some commits Jan 19, 2017

{
$p = new \atk4\ui\Persistence\POST();
$this->model['surname'] = 'DefSurname';

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

shouldn't we test field 'surname' after loading too? that would be useful.

This comment has been minimized.

@romaninsh
@@ -0,0 +1,27 @@
<?php

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

No method, parameter and return value description for this class

This comment has been minimized.

@@ -0,0 +1,178 @@
<?php

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

No method, parameter and return value description for this class

@@ -0,0 +1,31 @@
<?php

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

No method, parameter and return value description for this class

@@ -0,0 +1,60 @@
<?php

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

No method, parameter and return value description for this class

.. php:method:: success($title, [$sub_title])
Create and return :php:class:`jsChain` action, that will replace form with a success message.

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

what if I don't always want to replace form with success message?

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

then don't use this method, create your own jsChain.

Create and return :php:class:`jsChain` action, that will replace form with a success message.
.. php:attr:: successTemplate

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

if there is successTemplate, then maybe there should also be some kind of errorTemplate (for a field?)

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

not really because errorTemplate does not make form dissapear.

$form->onSubmit(function($f) {
if (!$f->model['terms']) {
return $form->error('terms', 'You must accept terms and conditions');

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

$form->error or $f->error?

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

$f, because it's inside a callback as parameter. I'm not sure which is better, to let users be ignorant or hit them with a stick.

$f->model->save();
return $form->success('Registration Successful', 'We will call you soon.');

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

$form->success or $f->success?

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

$f actually.

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

updated to $form everywhere.

or you can divide space equally between fields. I am also omitting header for this group::
$gr = $f->addGroup(['n'=>'two']);

This comment has been minimized.

@DarkSide666

DarkSide666 Jan 29, 2017

Member

what is 'n' attribute here?

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

renamed into width

This comment has been minimized.

@romaninsh

romaninsh Feb 7, 2017

Member

also added comments to properties.

DarkSide666 and others added some commits Jan 29, 2017

Reverting _POST superglobal nag, because I might need to refactor it …
…later.

Revert "PhpMD should not object about $_POST superglobal"

This reverts commit 9310d51.

@romaninsh romaninsh merged commit e9cfd09 into develop Feb 7, 2017

6 checks passed

codeclimate Approved by Romans Malinovskis.
Details
codeclimate/coverage 66.48% (-7.8%)
Details
continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/styleci/push The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@romaninsh romaninsh deleted the feature/form2 branch Feb 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment