php - How to set up custom error class using Form Helper of CakePHP 2.2.4?
Get the solution ↓↓↓I have a "special" request. I have started with CakePHP 2.2.4 few days ago, now I'm developing few small forms using Form Helper.
I saw that Form Helper manage the errors automatically (awesome!), but I need to do few customizations. I know that I can change the class of the error (error-message) and use, for example, aspan
instead of thediv
.
But, I need to change the class OF the input IF there is an error.
<?php
echo $this->Form->input('User.email', array(
'label' => array(
'class' => 'name-form',
'text' => 'Email:'
),
'div' => 'field',
'class' => 'input-xlarge'
)
);
?>
as you can see the class that i use is named input-xlarge
I need to change that class IF THERE IS an error. The new class should be input-xlarge-error.
This class only change the border of the input text setting it to red.
Is this possible?
Thank you!
Answer
Solution:
Why do you need to change the class of the input if there is an error?
You can target the input using the class that has been appended to the outer div.
E.g.
div.error input { }
You can then style the input differently if there is an error.
All you need in the view is:
<?php echo $this->Form->create('User'); ?>
<?php echo $this->Form->input('email'); ?>
<?php echo $this->Form->end('Submit'); ?>
This should provide you with a quick example. Submit the form and then inspect the element to see where the error class has been added.
You can probably get away with:
.error input { border: red; }
In the CSS
Answer
Solution:
What you can do is to create your own FormHelper class and override addClass() method:
class BootstrapFormHelper extends FormHelper {
public function addClass($options = array(), $class = null, $key = 'class') {
if ($class === 'error') {
$class .= ' has-error';
}
return parent::addClass($options, $class, $key);
}
}
Answer
Solution:
Think about the logic of your question:
"If there is an error I want the class of the div to be x, otherwise y".
There are a couple of ways to accomplish this, if all you need to do is flip the class based on error, thenFormHelper::isFieldError
will suffice...
<?php
echo $this->Form->input('User.email', array(
'label' => array(
'class' => 'name-form',
'text' => 'Email:'
),
'div' => 'field',
'class' => $this->Form->isFieldError('User.email') ? 'input-xlarge-error' : 'input-xlarge'
));
?>
The above will not stop the "form-error" class from being appended to the input. (solutions for that are outside the scope of the question)
The reason I answered this question is because I needed to specifically set the error class on my input to work with bootstrap 3 the way I wanted. This can also help with input classes being wiped on error. You may also want to review the options available forFormHelper::input()
which I link below. For more complex control over error, there is aFormHelper::Error
which is even more robust, you can find this all on the cakephp website....
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.