Drupal 7 form api. Add custom class to element after validation - drupal

I looked around and could not find the solution as to how to add a custom class to the form element/elements or the <form> tag for that matter. I have a custom form validation function which does some custom validation. form_set_error does set an error class on the elements but I wanted to add my custom class anywhere within the form tag.

Since the goal is just to customize the display of your field on error, a cleaner way is to create your own theme_form_element() in your theme and use the function form_get_error($element) to add the class you want if any error is returned on a field.
Using this method you can also display the error message next to the field in error, instead of on top of the form.

I found the answer here. Turns out you can use $form_state to make alterations to form after submission. I did
if($haserror) {
$form_state['complete form']['#attributes'] = array('class'=>array('contains_error'));
}

Related

How to manually add an error to a form component in Symfony?

I want to manually add an error to a form component.
I did this:
$form->get('videoId')->addError(new FormError('error message'));
But the error is getting assigned to the form itself, instead of the form component.
Do you have any idea why it is not assigned to my videoId field?
Thanks for your help
I found my mistake... the field type is HiddenType, and it seems you can't assign errors to hidden fields for some reason

How to show wordpress setting api validation error message?

I have created a setting api for my new plugin. It has four <input> field and one <select> option. In that field , user can change value according to their requirement. But it must be valid value. Suppose , one <input> field support only integer value. so when user will fill that <input> field by string or by other things, it will show an error message. Please tell me how can i do that? Can you suggest any tutorial ?
When using add_setting you can pass a sanitize_callback option as part of the array, this can contain the name of a function that is run which can contain your validation.
A similar method exists for JavaScript too, which you could use to validate the fields as well.
You asked for a tutorial too, here is one that outlines the method I've described: http://themeshaper.com/2013/04/29/validation-sanitization-in-customizer/

Drupal 7 missing css rules after form validation failed

I am trying to validate my custom module form. The validation works like charm even if I use hook_validate or use the #validate attribute in the submit button. The problem is that after validation failed the form losses the css styles I have attached to the module. I am attaching the css on module using the .info file. I have also tried to re attache the css at the bottom of the validation function using drupal_add_css or the #attached attribute.
If anyone had the same issue please let me know. I will appreciate any ideas or any help.
Thanks.
If someone having the same problem I found the solution. After validation Drupal changes the html form id. If you are using css selectors based on the form id, just add a custom class name to your form using the #attributes form attribute somewhere in your form creating function.
$form['#attributes'] = array('class' => array('your_id'));
Then use this class for various selections in your css file.
OR
You can always use attribute CSS selectors with wildcards.
Use #afterbuild, this is an optional form element called $form['#after_build'].
function yourmodulename_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'thisformid'){
$form['#after_build'][] = 'yourmodulename_after_build';
}
}
function yourmodulename_after_build($form, &$form_state) {
drupal_add_css('your_file.css');
}

Validation while rendering multiple partial views

I have multiple views/models that are being looped into the main view. I load them via #Html.Partial()...These views/models are basically form elements with certain properties...Unfortunately I soon found out that only the first of each type of view/model is validated. I tried moving the fields around and only the first of each kind would validate.
My partial views look something like this:
#Html.DropDownListFor(model=>model.dropdownVal,Model.SelectItems,new { id=Model.FieldID, Name = Model.FieldID })
I looked at the HTML rendered, and it seems that the validation tags like "data-val" are not applied...
Any ideas would be greatly appreciated!
Add the following at the top of your partials to trick ASP.NET MVC into thinking that the helpers are used inside a form and generate the proper data-val attributes:
#{
this.ViewContext.FormContext = new FormContext();
}
Basically the Html.* helpers are generating data-val clients side validation attributes only if they are placed inside an Html.BeginForm. Except that in your case I guess that this Html.BeginForm is in your parent view and not inside the partial, so the #Html.DropDownListFor doesn't emit any validation attributes. By setting the current FormContext to a new instance in the partial as shown previously, the helper will generate the proper client side validation attributes on the corresponding input field.

Entity reference field and dependent dropdown

I have a content type with an entity reference field referencing to a custom entity. I need to use a select box because an autocomplete widget is not suitable in my case. However, I cannot load all the entities at once as selectable values because they are too many (72000+ the form won't even load). So I default the entity reference select box to a limited number of values using a views filter and then hide it by default. Then I use an ajax dependent dropdown to show and populate the entity reference select box with filtered down values (I'm using a module that implements hook_form_alter).
My problem is that the form won't validate because now I can select entity reference values which are not the default ones in the select box. So I guess I should control in some way the validation rules of the entity reference field. Is there an easy way to do this? Which hook should I use?
Set the entity reference field to autocomplete and take it out of the process entirely in your form alter with $form['field_entity_ref']['#access'] = FALSE. This should fix the validation problem. (of course, "field_entity_ref" is what I'm calling your actual reference field.
Add your own validation to the form, if that is still necessary.
Finally, implement hook_node_presave() to manually put the value of your custom ajax drop down box.
So if your custom ajax select box was named my_custom_ref, then it would look something like this:
function mymodule_node_presave($node) {
if (isset($node->my_custom_ref)) {
$node->field_entity_ref[$node->language][0]['target_id'] = $node->my_custom_ref;
}
}

Resources