kodeine's avatar

Possible to auto generate forms based on model.

As the title says, would it be possible to define the field types etc in Model array so view can automatically generate a form based on that. This way we can have views ready without doing extra work. And generating admin/users panels will be almost painless.

If you have better ideas feel free to share :)

0 likes
6 replies
kodeine's avatar

Do you think its a good idea to use FormModels to generate views?

2 likes
RachidLaasri's avatar

I don't like you'll find something that will create views exactly as you want them, you still need to do a lot of work.

I prefer creating snippets and build form components easily.

1 like
chrisgeary92's avatar

How about creating some blade templates:

/views/fields/text.blade.php
/views/fields/textarea.blade.php
/views/fields/select.blade.php

Within these files you write the code for that one field, with variables that you pass in for things like field name, value, options, label etc so you would use it like this:

@include('fields.text', ['name' => 'first_name', 'label' => 'First Name', 'value' => 'Some Value'])

Then if you wanted to go a step further, you could add this to your model

class User extends Model {
    public function formFields() {
        return [
            [
                'name' => 'first_name',
                'type' => 'text',
                'label' => 'First Name',
                'value' => 'Some Value'
            ]
        ];
    }
}

And in your view you could do..

@foreach(User::formFields() as $field)
    @include(sprintf('fields.%s', $field['type']), $field)
@endforeach
13 likes

Please or to participate in this conversation.