Jeremy Penrod
Jeremy Penrod

Reputation: 331

CakePHP Form Helper - Show error class, but not error message

I'm attempting to customize the error output on the CakePHP 2.0 form helper. Currently, the form renders error messages below the input and applies an 'error' class to the input's label.

I have found that I can either disable error reporting altogether for an input, or output the error class and message.

I would like the error class to be applied to the label of the offending inputs WITHOUT any message below.

How do you turn off the error message outputting for a form, BUT still apply error classes to offending labels?

Upvotes: 0

Views: 3190

Answers (3)

pixelistik
pixelistik

Reputation: 7830

FormHelper::input() has a format option. It is a

format template for element order. Any element that is not in the array, will not be in the output.

Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')

You can pass the default format, leaving out the 'error':

echo $this->Form->input(
    'some_field',
     array('format' => array('before', 'label', 'between', 'input', 'after'))
);

That should produce the input markup without the error message.

If you want to apply this to multiple inputs in your form, you should keep it DRY:

$format = array('before', 'label', 'between', 'input', 'after');
echo $this->Form->input(
    'some_field',
     array('format' => $format)
);
echo $this->Form->input(
    'some_other_field',
     array('format' => $format)
);

It is also possible to set the default format for all inputs of a form by passing the format to FormHelper::create() as inputDefaults:

$this->Form->create(
    'MyModel',
    array(
        'inputDefaults' => array(
            'format' => array('before', 'label', 'between', 'input', 'after')
        )
    )
);

Upvotes: 2

dogmatic69
dogmatic69

Reputation: 7585

You can always make use of Form->error('field_name') which should return nothing if there is no errors.

$error = $this->Form->error('field_name');
echo $this->Html->input('field_name', array(
    'class' => !empty($error) ? 'error' : null,
    'error' => false
));

You now have the $error with the usual markup for errors that could be displayed in another location.

There is no way to get around without checks, the Form->input() method is a convinience method that does all these things like errors, divs, labels automatically which can be done through Form->label(), Form->checkbox(), Form->select() etc which is the basic elements only.

One of the options that can be passed to Form->create() is inputDefaults which you can use to set defaults for all the other form elements. This will not help much as you are doing field by field. ('error' => false would help a bit)

The other thing you can do is make your own form helper, extending the core FormHelper and customise the input method to do this all automatically. You can use aliasing to load your custom helper into $this->Form to be used as normal. See the bottom of this section.

You can also overload the input method in AppHelper but that is not a good place for it.

Upvotes: 0

shoesole
shoesole

Reputation: 105

You'll have to do some of this manually. First turn off the validations, and label generation on the Form Helper.

echo $this->Form->input('myfield', array('error' => false, 'label' => false));

Then to add the class to the create the label and add the error class if validations have failed. To find out which validations failed check the invalidFields array like so:

$error = null;
if (isset($this->invalidFields['Model']['myfield'])) {
    $error = 'error';
}

echo $this->Form->label('myfield', 'My Field', array('class' => $error));

Upvotes: 0

Related Questions