kristijanhusak / laravel-form-builder

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

generate selectbox from enum datatype #438

Open ThomasPfuhl opened 6 years ago

ThomasPfuhl commented 6 years ago

Hello, I want to generate a select box from a given enum list. Example: php artisan make:form Forms/TestForm --fields="id:number,title:enum('coffee','tea','juice')"

I did not find a way to generate the desired piece of code from the commandline:

->add('title', 'select', [
    'choices' => ['coffee' => 'coffee', 'tea' => 'tea', 'juice' => 'juice'],
    'empty_value' => '=== please select ==='
  ])

Any hints ? Thanks a lot !

kristijanhusak commented 6 years ago

Hi, Currently there's no support for generating enum types like that.

ThomasPfuhl commented 6 years ago

Thanx Kristijan for the info. So let me point out what I did: Avoid commas in the fields argument: php artisan make:form Forms/TestForm --fields="myfield:choices=>['coffee';'filter';'tea']" The created code is then processed (regexps are fine for that) to generate ->add('myfield', 'select', [ 'choices' => ['0'=>'--','coffee','filter','tea']]) It's just an ugly workaround but it works.

kristijanhusak commented 6 years ago

I don't see a reason to add a choices option to a cli generator, since you can just go easily into the form class and add it there. It would be ok if it would be simple, but all these comma stuff makes it really complicated.