Reputation: 154
i'm trying to add a default class for each input in my cakephp 3 app. Example of what i want:
Input:
<echo $this->Form->control('email');
Output:
<input class="form-control" class="is-invalid"/>
Desired output:
<input class="form-control is-invalid"/>
for this i have edited input template of FormHelper
$this->viewBuilder()->setHelpers([
'Form' => [
'templates' => [
'input' => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/>'
]
]
]);
the problem is that {{attrs}}
possibly contain others classes. Do you have any idea how to do it?
Upvotes: 0
Views: 915
Reputation: 1
Adding to Matoran's answer, I also checked to see if there was another 'class' provided when the form control element was created and include that with the override class. For me I still want form-control
as a class but also needed to add the class datepicker
or datetimepicker
while I was generating the form.
class BootstrapFormHelper extends FormHelper{
public function control($fieldName, array $options = []){
if($this->request->is('post') && !$this->isFieldError($fieldName)){
$options['class'] = (isset($options['class'])) ? 'form-control is-valid '.$options['class'] : 'form-control is-valid';
}else{
$options['class'] = (isset($options['class'])) ? 'form-control '.$options['class'] : 'form-control';
}
return parent::control($fieldName, $options);
}
}
Upvotes: 0
Reputation: 154
solved :D create a FormHelper to override method control and add class.
class BootstrapFormHelper extends FormHelper{
public function control($fieldName, array $options = []){
if($this->request->is('post') && !$this->isFieldError($fieldName)){
$options['class'] = 'form-control is-valid';
}else{
$options['class'] = 'form-control';
}
return parent::control($fieldName, $options);
}
}
then change your AppView
class AppView extends View{
public function initialize()
{
$this->loadHelper(
'Form', [
'className' => 'BootstrapForm',
]
);
}
}
Upvotes: 2