Search code examples
phpclasscakephpformhelper

CakePHP extending FormHelper makes errors not report correctly


I am working on an application using CakePHP (2.x) and I was reusing some settings whenever I was creating a form, so I decided to extend the default FormHelper class that would automatically load the default values if nothing else was passed into it (see code below).

<?php

class AppFormHelper extends FormHelper
{
    public function input($fieldName, $options = array())
    {
        $defaults = array(
            'class' =>  'input',
            'div'   =>  array(
                            'class' => 'button-height block-label margin-bottom'
                        )
        );

        $options = Set::merge($defaults, $options);

        return parent::input($fieldName, $options);
    }
}

At first glance, this appeared to work perfectly when calling it in my view like this $this->AppForm->input('test');. But, when that form is submitted and has an error, that error is not being displayed to the screen. When calling $this->Form->input('test') and there is an error, a div is created that looks something like this:

<div class="error-message">This form had an error</div>

Ultimately, I just want to have an easy way to replicate input options for the FormHelper, and thought this was the right way to do it, but since it's creating problems, I am not sure anymore. Anyone know how to make the errors show up again, or have a better solution for providing default options for the FormHelper.

Thanks!


Solution

  • Well, as my comment solved your problem, I take the liberty to write it as an answer... ;-) By the way, I'm happy I could help you !

    To me this seems a quite logical approach.

    Not sure if that can be the problem, but did you use your AppFormHelper for the whole form ? I often use a custom helper inheriting from FormHelper myself, and mixing the core helper with mine can bring some problems, for example if you use the SecurityComponent.

    Maybe something similar is happening here ?