FriendsOfCake / bootstrap-ui

CakePHP: Transparently use Bootstrap
MIT License
340 stars 145 forks source link

Cannot create own Custom Widget #354

Closed umer936 closed 3 years ago

umer936 commented 3 years ago

This is a (multiple allowed):

What you did

Intend to create a file dropper/dropzone widget. Currently just trying to make Form add a new Widget using https://book.cakephp.org/4/en/views/helpers/form.html#adding-custom-widgets.

$this->Form->addWidget(
    'autocomplete',
    ['Autocomplete', 'text', 'label']
);

Expected Behavior

I am able to use echo $this->Form->autocomplete('search', $options);

Actual Behavior

CakePHP Error

The "Form" alias has already been loaded. The className was not defined in the previous configuration data.

This is a feature discussion as the plugin should not remove the ability to add other custom Form fields.

ndm2 commented 3 years ago

Where exactly are you using $this->Form->addWidget()? It should work fine in templates, but it could break the form helper loading "order" when used in a view class.

umer936 commented 3 years ago

The first message in this thread was from the View class. I figured that was the case that it would break the Form helper. However, there are issues trying to use it in templates as well.

in templates/Pages/home.php, putting

$this->Form->addWidget(
    'autocomplete',
    ['Autocomplete', 'text', 'label']
);

results in Cannot find template named 'autocomplete'.

Whereas putting

$autocomplete = new AutocompleteWidget(
    $this->Form->getTemplater(),
    $this->Form->getWidgetLocator()->get('text'),
    $this->Form->getWidgetLocator()->get('label'),
);
$this->Form->addWidget('autocomplete', $autocomplete);

results in Missing field name for FormHelper::getTemplater

I was thinking this was related to bootstrap-ui overwriting the templates. I have not tried this in a fresh project without the plugin

ndm2 commented 3 years ago

The first error means that you have not (or incorrectly) added a template to the form helper, if you look in the linked book section, its done there using:

$this->Form->setTemplates([
    'autocomplete' => '<input type="autocomplete" name="{{name}}" {{attrs}} />'
]);

The second error means that you possibly did something like $this->Form->autocomplete() without an argument for the field name.

umer936 commented 3 years ago

Oh wow. I did not understand the docs correctly.

This widget would render the “autocomplete” string template, such as:

$this->Form->setTemplates([
    'autocomplete' => '<input type="autocomplete" name="{{name}}" {{attrs}} />'
]);

Made me think that simply doing the addWidget() would make CakePHP run the setTemplates()—not that I had to do it.

Works as intended. Evidently not an issue with bootstrap-ui.

Thank you for your help. Closing.