//config/custom_forms.php
<?php
//Sobrecribe los templates usados por FormHelper
return [
// Used for checkboxes in checkbox() and multiCheckbox().
'checkbox' => '<input type="checkbox" class="form-check-input" name="{{name}}" value="{{value}}"{{attrs}}>',
// Input group wrapper for checkboxes created via control().
'checkboxFormGroup' => '{{label}}',
// Wrapper container for checkboxes.
'checkboxWrapper' => '<div class="checkbox">{{label}}</div>',
// Error message wrapper elements.
'error' => '<div class="invalid-feedback">{{content}}</div>',
// General grouping container for control(). Defines input/label ordering.
'formGroup' => '{{prepend}}{{label}}{{input}}',
// Container element used by control().
'inputContainer' => '<div class="md-form{{required}}">{{content}}</div>',
// Container element used by control() when a field has an error.
'inputContainerError' => '<div class="md-form">{{content}}{{error}}</div>',
// Label element when inputs are not nested inside the label.
'label' => '<label class="mdb-main-label" {{attrs}}>{{text}}</label>',
// Label element used for radio and multi-checkbox inputs.
'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
// Multi-select element,
'selectMultiple' => '<select name="{{name}}[]" class="mdb-select colorful-select dropdown-primary md-form" multiple {{attrs}} searchable="Filtrar...">{{content}}</select>',
];
The problem is that I need to add default classes while still beign able of passing extra classes when using the Helper.
//actual output
'<input type="checkbox" class="form-check-input" name="is_active" value="0">'
//duplicated attr key "class" is removed/omitted and attr value is not added to class
Is there a way to achieve this in CakePHP 4.x? Thanks
EDIT:
An option would be to use templateVars, but I’d like to write the less code possible:
Any class that’s added as in your example should be included in the {{attrs}}, and hence in the output from your template. How are you checking your output? If it’s with something like the developer tools in a browser, then it may be missing because the result will be a second class attribute in the same tag, which is invalid, so they just drop the second one. If you look at raw HTML, it may be there? (I’m testing this with Cake 3, so it might have changed in 4.x.)
public function control($name, $options) {
$options['templateVars' => ['extraclasses' => 'extra-class']];
echo $this->Form->control($name, ['templateVars' => ['extraclasses' => 'extra-class']]);
}
In this way, when you do find the correct solution, you will simply have to change the helper called for control when the first arg is is_active and your code will be fix.
//Custom FormHelper that extends Cake's FormHelper
<?php
declare(strict_types=1);
namespace App\View\Helper;
use Cake\View\Helper\FormHelper as Helper;
class FormHelper extends Helper
{
/**
* Generates a form input element complete with label and wrapper div.
*
* @param string $fieldName This should be "Modelname.fieldname".
* @param array $options Each type of input takes different options.
* @return string Completed form widget.
*/
public function control(string $fieldName, array $options = []): string
{
if (!empty($options['class'])) {
$options['templateVars']['extraClasses'] = ' ' . $options['class'];
unset($options['class']);
}
return parent::control($fieldName, $options);
}
}
May not be the best approach because now I have to create all templates but it’s a beginning.
I can add an input type switch to filter, etc…