lara-zeus / bolt

form builder for your users, with so many use cases
https://larazeus.com/bolt
MIT License
190 stars 35 forks source link

Custom schema #301

Closed atmonshi closed 5 months ago

atmonshi commented 5 months ago

this is a continuance of the initial PR #300 by @blhylton, thank you so much.

will update the doc soon and add more details here...

what-the-diff[bot] commented 5 months ago

PR Summary

These changes were made to expand the flexibility, customizability, and robustness of the existing model by including support for custom schemas.

blhylton commented 5 months ago

It worked like a charm for me once I determined that the issue with option validation was the reason it wasn't saving initially.

For some additional insight, my end goal is to create a sort of meta-field on the form fields so that I can define some custom functionality on FormSent. For my uses, it ends up working something like this:

$event->response->fieldsResponses->each(function($fieldResponse){
    $data_binding = explode('.', $fieldResponse->field->options['meta']['data_binding']); // ['Foo', 'name']
    $model = (new $data_binding[0])([ $data_binding[1] => $fieldResponse->response]);
    $model->save();
});

Wanted to include this here to help with any documentation needs and/or in case anyone needs to do something similar and finds this in the future.

Warning: Don't trust your user input explicitly like I'm doing in my example. That could open some pretty serious security holes. This is a simplified example. If you copy & paste this code without understanding the implications, I'm not responsible.

atmonshi commented 5 months ago

thank you @blhylton, can you share the schema you have or simple version of it, so I'll update the doc and add it as an example.

blhylton commented 5 months ago

@atmonshi Sure thing. This is the one I was using for this particular example:

<?php

namespace App\Zeus;

use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\TextInput;
use LaraZeus\Accordion\Forms\Accordion;
use LaraZeus\Bolt\Fields\FieldsContract;

class CustomSchema implements \LaraZeus\Bolt\Contracts\CustomSchema
{

    public function make(?FieldsContract $field = null): Accordion
    {
        return Accordion::make('MetaFields')
            ->schema([
                TextInput::make('options.meta.data_binding')
                    ->label('Data Binding'),
            ]);
    }

    public function hidden(?FieldsContract $field = null): array
    {
        return [
            Hidden::make('options.meta.data_binding'),
        ];
    }
}

It's excessively simple because I was ultimately making sure it was going to work like I expected.