Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

DataForm

Felice Ostuni edited this page Oct 4, 2015 · 16 revisions

DataForm is a form builder, you can add fields, rules and buttons.
It will build a bootstrap form, on submit it will check rules and if validation pass it'll store new entity.

//start with empty form to create new Article
$form = \DataForm::source(new Article);
   
//or find a record to update some value
$form = \DataForm::source(Article::find(1));

//add fields to the form
$form->add('title','Title', 'text'); //field name, label, type
$form->add('body','Body', 'textarea')->rule('required'); //validation
  
//can also support readonly 
$form->text('nickname','Nickname')->mode('readonly');

//or passing values to model without output
$form->set('user_id', Auth::user()->id);

## some enhanced field (images, wysiwyg, autocomplete, etc..):
//image upload
$form->add('photo','Photo', 'image')
     ->move('uploads/images/')
     ->preview(80,80);

//wysiwyg editor
$form->add('body','Body', 'redactor'); 

//autocomplete (on related belongsTo field)
$form->add('author.fullname','Author','autocomplete')
      ->search(array('firstname','lastname'));

//multiple autocomplete (on related belongsTo field)
$form->add('categories.name','Categories','tags'); 
   
//you can also use smart syntax, ->{type}(... ): 
$form->textarea('title','Title'); 
$form->autocomplete('author.name','Author')..

//by default DataForm and DataEdit are horizontal, DataFilter is inline
//but you can change form orientation passing the right bootstrap class
$form->attributes(['class'=>'form-inline']);

//submit button
$form->submit('Save');

//at the end you can use closure to add stuffs or redirect after save
$form->saved(function() use ($form)
{
    $form->message("ok record saved");
    $form->link("/another/url","Next Step");
});

return view('article', compact('form'));
//or 
return $form->view('article', compact('form'));
   #article.blade.php
  {!! $form !!}

note: DataForm can also work without entity, just as Form builder, use DataForm::create() instead of DataForm::source in this case:

$form = \DataForm::create();
$form->text('title','Title'); 
$form->textarea('body','Body')->rule('required');
$form->submit('Save');

$form->passed(function() use ($form)
{
    dd(\Input::all());
});

return view('article', compact('form'));

customize field position

you can group fields, change position etc.. directly in the view: https://github.com/zofe/rapyd-laravel/wiki/Custom-Form-Layout

redirect on success

note: if you need a redirect you should use $form->view method instead View::make.

Clone this wiki locally