CakePHP extending FormHelper makes errors not report correctly

1.3k Views Asked by At

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!

3

There are 3 best solutions below

1
On BEST ANSWER

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 ?

0
On

As nIcO pointed out to me, this is a great solution, however I was not using my AppFormHelper for the whole form. So this works great, but make sure you use the custom class is used on the whole form.

0
On

To overwrite a HtmlHelper method in Cake 2.0 you can simply:

Create your OwnHelper class containing for example a link method, which extends HtmlHelper, in AppController specify:

$helpers = array('Html' => array('className' => 'OwnHelper'));

via ADmad