Forms

Creating a form:

<?php
namespace App\Form;

use 
Gobline\Form\Form;
use 
Gobline\Form\Element;

class 
LoginForm extends Form
{
    public function 
__construct()
    {
        
parent::__construct();

        
$this->setMethod('post');

        
$this->add((new Element\Email('email'))
            ->
setLabel('Email')
            ->
setAttribute('placeholder''Enter your email address'));

        
$this->add((new Element\Password('password'))
            ->
setLabel('Password')
            ->
setAttribute('placeholder''Enter your password'));

        
$this->add(new Element\Submit('submit'));
    }
}

Validating and processing the form:

<?php
namespace App\ActionModel;

use 
App\Form\LoginForm;
use 
Gobline\Auth\CurrentUserInterface;
use 
Gobline\Auth\Authenticator\AuthenticatorInterface;

class 
LoginActionModel
{
    private 
$authenticator;
    private 
$identity;
    public 
$form;
    public 
$success;
    public 
$invalidCredentials;

    public function 
__construct(
        
LoginForm $form,
        
CurrentUserInterface $identity,
        
AuthenticatorInterface $authenticator
    
) {
        
$this->form $form;
        
$this->identity $identity;
        
$this->authenticator $authenticator;
    }

    public function 
login()
    {
        
$this->identity->clearIdentity();

        
$email $this->form->getValue('email');
        
$password $this->form->getValue('password');

        
$this->authenticator->setIdentity($email);
        
$this->authenticator->setCredential($password);

        if (
$this->authenticator->authenticate($this->identity)) {
            
$this->success true;
        } else {
            
$this->invalidCredentials true;
        }
    }

    public function 
__invoke($request)
    {
        if (
            
$request->getMethod() === 'POST'
            
&& $this->form->validate($request->getParsedBody())
        ) {
            
$this->login();
        }
    }
}

Displaying the form, using the Form View Helper:

<?php
$form
->setElementAttributes('class="form-control"')
     ->
setLabelAttributes('class="control-label"')
     ->
setErrorAttributes('class="help-block"')
     ->
setRowWrapper('div''class="form-group"''has-error');
?>
<?= $form
->open($model->form'action="'.$route('login')->uri().'"'?>

    <?php if ($model->invalidCredentials): ?>
        <div class="alert alert-danger">Invalid username and/or password</div>
    <?php endif ?>

    <?= $form->row('email'?>
    <?= $form->row('password'?>

    <?= $form->element('submit''value="Connect" class="btn btn-default"'?>

<?= $form->close() ?>

Creating a form attached to an entity:

<?php
namespace App\Form;

use 
Gobline\Form\Form;
use 
Gobline\Form\Element;

class 
UserForm extends Form
{
    public function 
__construct()
    {
        
parent::__construct();

        
$this->setMethod('post');

        
$this->add(new UserFieldSet());

        
$this->add(new Element\Submit('submit'));
    }
}
<?php
namespace App\Form;

use 
Gobline\Form\FieldSet;
use 
Gobline\Form\Element;
use 
App\Model\Entity\User;

class 
UserFieldSet extends FieldSet
{
    public function 
__construct()
    {
        
parent::__construct('user');

        
$this->setEntity(new User());

        
$this->add((new Element\Text('name'))
            ->
setLabel('Name')
            ->
setFilters('required')
            ->
setAttribute('placeholder''Enter your name'));
    }
}

Displaying the User form:

<?php 
$form
->setElementAttributes('class="form-control"')
     ->
setLabelAttributes('class="control-label"')
     ->
setErrorAttributes('class="help-block"')
     ->
setRowWrapper('div''class="form-group"''has-error');
?>
<?= $form
->open($model->form'id="userForm"'?>

    <?php $form->openFieldset('user'?>

        <?= $form->row('name'?>

    <?php $form->closeFieldset() ?>

    <?= $form->element('submit''value="Add" class="btn btn-default"'?>

<?= $form->close() ?> 

You can nest openFieldset/closeFieldset for nested entities:

<?php 
$form
->setElementAttributes('class="form-control"')
     ->
setLabelAttributes('class="control-label"')
     ->
setErrorAttributes('class="help-block"')
     ->
setRowWrapper('div''class="form-group"''has-error');
?>
<?= $form
->open($model->form'id="userForm"'?>

    <?php $form->openFieldset('user'?>

        <?= $form->row('name'?>

        <?= $form->openFieldset('address'?>

            <?= $form->row('street'?>

            <?= $form->row('city'?>

        <?= $form->closeFieldset() ?>

    <?php $form->closeFieldset() ?>

    <?= $form->element('submit''value="Add" class="btn btn-default"'?>

<?= $form->close() ?>