Laravel mit Formbuilder

Posted in Laravel, 03.08.2016 09:08

Formbuilder ermöglich erstellen eines Formulars über die Konsole:

php artisan make:form Forms/AdForm --fields="headline:text, ad_text:textarea,category_id:select,ad_price:number,ad_versandkosten:number,ad_picture1:image,published:checkbox"

Allerdings fehlen noch die Rules und auch Labels müssen manuell hinzugefügt werden. In diesem Beispiel wird auch eine DB-Abfrage für die Selectbox-Options abgesetzt:

<?php

namespace LaravelCms\Forms;

use Kris\LaravelFormBuilder\Form;
use LaravelCms\Models as Models;

class AdForm extends Form
{

public function buildForm()
{

$categories = Models\Categories::where('parent', 0)->orderBy('name')->lists("name", "id")->all();

$this
->add('id', 'hidden')
->add('user_id', 'hidden')
->add('ad_headline', 'text', [
'label' => 'Headline',
'rules' => 'required|max:100'])
->add('ad_text', 'textarea', [
'label' => 'Text',
'rules' => 'required|max:5000'
])
->add('category_id', 'select', [
'label' => 'Category',
'choices' => $categories,
'empty_value' => '=== Select Category ==='
])
->add('ad_price', 'text', [
'label' => 'Price',
'rules' => 'required|numeric'
])
->add('ad_versandkosten', 'text', [
'label' => 'Versandkosten',
'rules' => 'numeric'
])
->add('ad_picture1', 'file', [
'label' => 'Image',
'rules' => 'image'
])
->add('published', 'checkbox')
->add('speichern', 'submit', ['wrapper' => ['class' => 'form-group'], 'attr' => [ 'class' => 'btn btn-primary']]);
}

}