Open dan-developer opened 8 years ago
The following example of use, pay attention as I did in getTagForm
method in the controller, how do I create a new one.
I'm trying to facilitate and not to rewrite code using a new method in FormBuilderTrait. Option is_child
causes "Argument 1 passed to form_row() must be an instance of Kris\LaravelFormBuilder\Fields\FormField, null given".
<?php
namespace Yoocloud\LaravelFormBuilder;
use Kris\LaravelFormBuilder\Fields\ChildFormType;
trait FormBuilderTrait
{
/**
* Create a Form instance
*
* @param string $name Full class name of the form class
* @param array $options Options to pass to the form
* @param array $data additional data to pass to the form
*
* @return \Kris\LaravelFormBuilder\Form
*/
protected function form($name, array $options = [], array $data = [])
{
return \App::make('laravel-form-builder')->create($name, $options, $data);
}
/**
* Create a plain Form instance
*
* @param array $options Options to pass to the form
* @param array $data additional data to pass to the form
*
* @return \Kris\LaravelFormBuilder\Form
*/
protected function plain(array $options = [], array $data = [])
{
return \App::make('laravel-form-builder')->plain($options, $data);
}
/**
* Create a Child Form instance
*
* @param \Kris\LaravelFormBuilder\Form $form
* @param string $collectionName
* @param int $key
*
* @return \Kris\LaravelFormBuilder\Fields\ChildFormType
*/
private function childForm($form, $collectionName, $key = null)
{
try {
// test if collection exists
$form->$collectionName;
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException('The collection ' . $collectionName . ' not exists in '.get_class($form));
}
if (is_null($key)) {
$key = 0;
}
$options = $form->$collectionName->getOption('options');
// bug treatment
if (array_key_exists('is_child', $options)) {
unset($options['is_child']);
}
$type = $form->$collectionName->getOption('type');
return new ChildFormType("{$collectionName}[{$key}]", $type, $form, $options);
}
}
namespace App\Http\Controllers\Teste;
use App\Forms\Teste\PostForm;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Models\Teste\Post;
use Illuminate\Http\Request;
use Kris\LaravelFormBuilder\FormBuilder;
use Yoocloud\LaravelFormBuilder\FormBuilderTrait;
class DefaultController extends Controller
{
use FormBuilderTrait;
public function getCreate(FormBuilder $formBuilder)
{
$post = new Post();
$form = $formBuilder->create(PostForm::class, [
'model' => $post,
'method' => 'POST',
'url' => url('teste/default/create'),
]);
return view('teste.default.create', compact('form'));
}
public function postCreate(Request $request)
{
//
}
public function getTagForm()
{
$key = \Input::get('key');
/** @var PostForm $form */
$form = $this->form(PostForm::class);
$child = $this->childForm($form, 'tags', $key);
return view('teste.default.partials._form-tag-child', compact('child'));
}
}
What would be the proper way to create a new child to a specific index?
Collections are often used (at least by me) in application, even when business application.
I will propose the following situation:
The documentation I should do something like this:
In
{!! form_row($form->tags) !!}
I would not like to have the option to do a foreach manually thus making it a bit more "malleable", something like:
In the view:
So back greater flexibility. There may also be the case that instead of using prototype, we can use ajax to get that "piece" of the tag, and enables reuse the same view.
So to summarize the scenario is registered something, and then it can be edited, and this edition will be "bootstrap" the form where there should already be filled, can be added more items in the collection, so when more flexible better.
As I'm doing manually:
If you are already doing this, tell me, but I saw in the documentation.
Thank you.