Tiago Matos
Tiago Matos

Reputation: 1706

cakephp 3: change class input error

I build my form template according the documentation. It seemed everything was fine until I get fields errors. Now I have two problems:

  1. How can I change the class name of the forms fields when they get error?

Solution:
$this->loadHelper('Form', [ 'templates' => 'your_template_file', 'errorClass' => 'your-class', ]);

  1. How can I set escape => false in the error-message from cakephp, when the field get error? Because I have icon within that div, such as
<div class="error-message"><i class="fa fa-times"></i> My error</div>

Well, I got part of th solution. To escape HTML I could put $this->Form->error('field', null, ['escape' => false]); in all fields, but it´s a hard manually task. I´d like to keep escape with default of all fields errors. I could edit the FormHelper.php class. However, I think that is not good idea.

My form template is:

'formStart'              => '<form {{attrs}} class="form-horizontal" novalidate>',
'inputContainer'         => '{{content}}',
'input'                  => '<input type="{{type}}" name="{{name}}" {{attrs}} class="form-control"/>',
'checkbox'               => '<input type="checkbox" value="{{value}}" name="{{name}}" {{attrs}}/>',
'textareaContainerError' => '{{content}}',
'textarea'               => '<textarea name="{{name}}" {{attrs}} class="form-control"></textarea>',
'select'                 => '<select name="{{name}}" {{attrs}} class="form-control">{{content}}</select>',
'button'                 => '<button {{attrs}} class="btn btn-primary">{{text}}</button>',
'nestingLabel'           => '{{input}}',
'formGroup'              => '{{input}}',

Upvotes: 0

Views: 2454

Answers (2)

azerto00
azerto00

Reputation: 1000

For those who wants an 'easy solution' to escape error message on some fields, you cant simply set escape options to false :

   <?= $this->Form->input('email', [
                                    "label" => "Email",
                                    "error" => [
                                        "escape" => false
                                    ]
                                ]) ?>

Upvotes: 0

user1418909
user1418909

Reputation:

to the second part of the question: you can extend FormHelper like in code below, so that escape will be set to false by default

// extended FormHelper, this goes in src/View/Helper
namespace App\View\Helper;
use Cake\View\Helper;

class MyFormHelper extends Helper\FormHelper
{
    public function error($field, $text = null, array $options = []) 
    {
        if (!isset($options['escape'])) {
            $options['escape'] = false;
        }

        return parent::error($field, $text, $options);
    }
}

next create alias for this helper in AppController.php

public $helpers = [
    'Form' => ['className' => 'MyForm']
];

this also allows you to add more customization of your own and at any time, you can go back to default implementation of FormHelper, just remove that alias from AppController.php.

Upvotes: 1

Related Questions