administrcms / form by mirovit

Form package for the administrcms as good as standalone laravel package
497
2
2
Package Data
Maintainer Username: mirovit
Maintainer Contact: mvvitanov@gmail.com (Miroslav Vitanov)
Package Create Date: 2016-01-29
Package Last Update: 2021-04-12
Language: JavaScript
License: Unknown
Last Refreshed: 2024-11-18 03:05:18
Package Statistics
Total Downloads: 497
Monthly Downloads: 6
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

Form Builder for the Administr package

Build Status Code Climate Test Coverage

Work-in-progress.

Installation

  • Install using composer:
composer require administrcms/form
  • Register the Service Provider (in config/app.php or in app/Providers/AppServiceProvider.php):
// in app.php
'providers' => [
    // ...
    Administr\Form\FormServiceProvider::class,
    // ...
],

// in AppServiceProvider
public function register()
{
    $this->app->register(\Administr\Form\FormServiceProvider::class);
}

  • Publish assets, configs, migrations and etc.
php artisan vendor:publish --provider="Administr\Form\FormServiceProvider"

Creating forms

There is a command available, which will generate a basic scaffold of a Form class for you.

php artisan administr:form MyForm

Or you can just create a new class which extends the Administr\Form\Form class and implement the two abstract methods - rules and form.

Form look and feel

By default the form fields are just basic html, without any styling. When you publish the assets which are connected to the package, the views will be exported to resources/views/vendor/administrcms/form, where you can modify them to adopt the theme of your application.

Usage

The form class needs to be resolved through the IoC container. One way to do that is to type hint the class in the method name.

The forms works like the FormRequests in Laravel, meaning that when you have type hinted the form in the method which responds to the post/put action, it will validate the form and if it is successful then it will execute the code in the method. Otherwise it will return the user back with the errors and populate the form with the user input and display the errors.

<?php

namespace App\Http\Controllers;

use App\Http\Forms\MyForm;
use App\MyModel;

class MyController extends Controller {
    public function create(MyForm $form)
    {
        $form->method = 'post';
        $form->action = route('my-form-action');
        
        return view('my-view', ['form' => $form]);
    }
    
    public function store(MyForm $form)
    {
        MyModel::create($form->request()->all());
    }
    
    public function edit($id, MyForm $form)
    {
        $form->method = 'put';
        $form->action = route('my-form-update', [$id]);
        
        // The datasource can be an array, Collection or Model
        $form->dataSource(MyModel::find($id));
        
        return view('my-view', ['form' => $form]);
    }
    
    public function update($id, MyForm $form)
    {
        $model = MyModel::find($id);
        
        $model->update($form->request()->all());
    }
}

then in the view you can simply say:

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

Radio groups

<?php

namespace App\Http\Forms;

use Administr\Form\Field\RadioGroup;
use Administr\Form\Form;
use Administr\Form\FormBuilder;

class TeamForm extends Form
{

    /**
     * Define the validation rules for the form.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name'      => 'required|max:100',
        ];
    }

    /**
     * Define the fields of the form.
     *
     * @param FormBuilder $form
     */
    public function form(FormBuilder $form)
    {
        $form
            ->text('name', 'Name', ['translated' => true])
            ->radioGroup('is_visible', 'Is Visible', function(RadioGroup $group) {
                $group
                    ->radio('да', ['value' => 1])
                    ->radio('не', ['value' => 0]);
            })
            ->submit('save', 'Save');
    }
}