kristijanhusak / laravel-form-builder

Laravel Form builder for version 5+!
https://packagist.org/packages/kris/laravel-form-builder
MIT License
1.7k stars 297 forks source link

Adding custom fields from inside another package (Laravel 4) #37

Closed stevebauman closed 9 years ago

stevebauman commented 9 years ago

I'm having trouble adding custom fields from inside a separate package.

The problem:

Since I'm using this package from another package, I can't seem to modify / override the custom fields array from your package config file. I've also added your service provider before mine so there shouldn't be an issue of your service provider overriding the config values I've just set.

I've tried modifying the config at runtime through my packages service provider in the register() function like so:

$this->app->config->set('laravel-form-builder::custom-fields', array(
     'select-category' => 'Stevebauman\Maintenance\Forms\Fields\WorkOrderCategoryField',
));

I can add these custom fields fine when building the form like you've shown in the documentation (which is also amazing), but I need these custom fields available for all of my forms unfortunately.

Would you happen to know anything I can try?

Thanks for the great package!

EDIT:

I have tried this in my register() function as well:

$this->app['laravel-form-helper']->addCustomField('select-category', 'Stevebauman\Maintenance\Forms\Fields\Select\WorkOrderCategoryField');

But this gives a View [] not found exception in my blade view where I call the {{ form($form) }} function

kristijanhusak commented 9 years ago

Woah, i'm really surprised about usage in another package :) Can i get the view on it somewhere? :)

Here are some solutions for this, though not ideal:

  1. You can try to create custom service provider for my package that will handle loading your custom configuration with mine - you merge them before passing it to the FormHelper. One con to this is the updates, but i'm pretty sure that i will not update my service provider any time soon.
  2. Create your Form class that extends this package Form class, override setFormOptions, and load customFields, like this:
<?php
use Kris\LaravelFormBuilder\Form;

abstract class YourForm extends Form {

    public function setFormOptions($formOptions)
    {

        $this->addCustomField(
        'select-category',
        'Stevebauman\Maintenance\Forms\Fields\WorkOrderCategoryField'
        );

        $this->addCustomField(
        'select-something-else',
        'Stevebauman\Maintenance\Forms\Fields\WorkOrderCategoryFieldSomethingelse'
        );  

        return parent::setFormOptions($formOptions);
    }

Did you try using maybe http://laravel.com/docs/4.2/configuration#provider-configuration ? I haven't tested it.

stevebauman commented 9 years ago

Awesome! Your second option works! Thanks a ton!

stevebauman commented 9 years ago

Also it'll be integrated into my package in development Maintenance so if you'd like to see it it'll be in there :).

On another note, sorry if I should be creating a new issue for this, but any chance of a version bump for the Laravel 4 version to v1.0 instead of v0.1? I think that this package is tested and stable enough to warrant a 1.0 realease, what are your thoughts?

kristijanhusak commented 9 years ago

I have a small problem with that, because i didn't start versioning properly. At first i thought creating this package only for Laravel 5, but since there were requests for Laravel 4, I just added it as another branch and use versioning from 0.* . This now causes the problems because Laravel 5 is from version v1.* , and Laravel 4 version is from v0.* . I'm trying to think of solution that will not break backward compatibility. Will probably leave this repo for laravel4, and create another one for laravel5. Will let you know.

kristijanhusak commented 9 years ago

@stevebauman I moved laravel 4 version to another repo. laravel4-form-builder, and this one is now used only for laravel 5. I will keep them both up to date.