2017 © Pedro Peláez
 

library laravel-zend-form

Laravel Zend Form

image

revolution/laravel-zend-form

Laravel Zend Form

  • Monday, June 18, 2018
  • by revolution
  • Repository
  • 1 Watchers
  • 0 Stars
  • 42 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Laravel Zend Form

Build Status Maintainability Test Coverage, (*1)

Laravel Zend Form, (*2)

https://docs.zendframework.com/zend-form/, (*3)

Laminas

https://github.com/kawax/laravel-laminas-form, (*4)

Requirements

  • PHP >= 7.1
  • Laravel >= 5.5

Installation

composer require revolution/laravel-zend-form

Suggest from ZendForm

https://github.com/zendframework/zend-form/blob/master/composer.json, (*5)

        "zendframework/zend-captcha": "^2.7.1, required for using CAPTCHA form elements",
        "zendframework/zend-code": "^2.6 || ^3.0, required to use zend-form annotations support",
        "zendframework/zend-eventmanager": "^2.6.2 || ^3.0, reuired for zend-form annotations support",
        "zendframework/zendservice-recaptcha": "in order to use the ReCaptcha form element"

Demo

https://github.com/kawax/laravel-zend-form-project, (*6)

Artisan command

php artisan make:form SampleForm

app/Http/Forms/SampleForm.php, (*7)

Form class

namespace App\Http\Forms;

use Revolution\ZendForm\Form as ZendForm;
use Zend\Form\Element;

class SampleForm extends ZendForm
{
    /**
     * Create a new form.
     *
     * @param null|string $name
     *
     * @return void
     */
    public function __construct($name = null)
    {
        parent::__construct($name);

        $this->setAttributes([
            'action' => url('/'),
            'method' => 'post',
        ]);

        $name = new Element\Text('name');
        $name->setAttributes([
            'id'    => 'name',
            'class' => 'form-control',
            'value' => old('name'),
        ]);
        $name->setLabel('Your name');
        $name->setLabelAttributes([
            'class' => 'col-sm-2 col-form-label',
        ]);
        $name->setOptions([
            'wrapper-class' => 'form-group row',
            'element-class' => 'col-sm-10',
        ]);

        $this->add($name);

        $this->add([
            'type'       => Element\Email::class,
            'name'       => 'email',
            'attributes' => [
                'id'    => 'email',
                'class' => 'form-control',
                'value' => old('email'),
            ],
            'options'    => [
                'label'            => 'Your email address',
                'label_attributes' => [
                    'class' => 'col-sm-2 col-form-label',
                ],
                'wrapper-class'    => 'form-group row',
                'element-class'    => 'col-sm-10',
            ],
        ]);

        $this->add([
            'type'       => Element\Hidden::class,
            'name'       => '_token',
            'attributes' => [
                'value' => csrf_token(),
            ],
        ]);

        $this->add([
            'name'       => 'send',
            'type'       => 'Submit',
            'attributes' => [
                'value' => 'Submit',
                'class' => 'btn btn-primary',
            ],
        ]);
    }
}

Controller

use App\Http\Forms\SampleForm;

    public function __invoke()
    {
        $form = new SampleForm;

        return view('form')->with(compact('form'));
    }
use App\Http\Forms\SampleForm;

    public function __invoke(SampleForm $form)
    {
        return view('form')->with(compact('form'));
    }

View

Simple render

{{ $form->render() }}

Same as ZendForm's echo $this->form($form);, (*8)

Detail render

@php
    $form->prepare();
@endphp

{!! $form->form()->openTag($form) !!}

{{ csrf_field() }}



{!! $form->formInput($form->get('name')) !!}
{!! $form->formInput($form->get('email')) !!}
{!! $form->formSubmit($form->get('send')) !!}
{!! $form->form()->closeTag($form) !!}

Form object can call Zend's ViewHelper by magic method., (*9)

See https://docs.zendframework.com/zend-form/quick-start/, (*10)

ViewHelper render

{{ $form->render('bootstrap4horizon') }}

https://github.com/kawax/laravel-zend-form/blob/master/docs/helpers.md, (*11)

Validation

Use Laravel's FormRequest., (*12)

LICENSE

MIT
Copyright kawax, (*13)

The Versions