How to apply custom validation classes directly to input?

I would like to add a custom validation css class directly to my input on validation failure.

I don’t see a way to accomplish this other than to edit /vendor/cakephp/cakephp/src/View/Helper/FormHelper.php — which I obviously don’t want to do.

Is there a way to override/supplement the css classes defined here from within my src directory?

You want to add a general class that indicates which fields had validation failures? Or you want to add a specific class to a specific field when it has a specific validation failure? Some something somewhere in between?

Can you provide sample code for understanding problem statement?

Right now, if a field fails validation, it gets tagged with the ‘form-error’ class.

I want one or more custom classes to get added (instead of or in addition to ‘form-error’)

I think this is hard. I guess the unanswered question is why you want to do this. You may be able to put different styles on different fields, for example, using CSS selectors that target the combination of the form-error and your specific field ID.

I’m using bootstrap. I want to leverage bootstrap css as much as possible and customizing the bootstrap variables… while minimizing rewrite of classes or wholesale new classes as much as possible.

At this time, my workaround is to use @extend in scss whenever CakePHP uses a class that I want to have behave as a Bootstrap class. But given all immense variety of css frameworks out there… it seems to me CakePHP should be opting not to define any classes for any generated HTML and making all of it part of the config.

But it is what it is.

I’ve seen how the form template allows me to make some amount of customization to the stuff the form helper generates, but it doesn’t seem to go quite all the way. This case in particular I was curious if maybe I was missing some built-in means to accomplish it.

My recollection last time I ran into this is that if you try to use different templates to add classes to form elements, they generally end up with two “class” attributes, one with your details and one with what Cake wants to add, rather than a single attribute with all the classes listed. I don’t know if this is fixed yet, or even on the radar to be fixed.

Yes, I’ve made it that far with the wrapper div. I can apply css there.

For the actual inputs, I can simply pass css classes in the form helper options array. But those are static classes. Validation status is not static and I haven’t found a means to templatize the validation failure css state of an input itself. However, I did find the the validation failure wrapper as a template and was able to create my own template for that as well.

But the the inputs themselves… seems like it’s unsupported.