Drupal 7 missing css rules after form validation failed - css

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');
}

Related

Custom Trigger when code found inside the page GTM

i ask you support to solve my issue. I made a custom code to dinamically fire schema.org snippet data on my site.
I need to trigger this only for blog post. My blog post is identified by this code:
div class="span12" data-motopress-wrapper-file="single.php" data-motopress-wrapper-type="content">
so when i find single.php i have to trigger the right tag into Google Tag Manager.
I need to create a custom dom variable, but i can't reach to understand how to do it. Thanks.
You should be able to create a DOM Element variable with the following configuration:
Selection Method: CSS selector
Element Selector: [data-motopress-wrapper-file]
Attribute Name: data-motopress-wrapper-file
And that variable should return single.php which you can then use a trigger (eg if {{My Dom Element}} equals single.php)
Alternatively, you can create a Custom JavaScript variable:
function() {
var attr = "data-motopress-wrapper-file";
return (document.querySelectorAll('['+attr+']')[0]).attributes[attr].value;
}
If you have multiple data-motopress-wrapper-file elements on your page, then you can create a function that will search for the specific single.php and return true/false based on its presence:
function() {
return (document.querySelectorAll('[data-motopress-wrapper-file="single.php"]').length>0);
}

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

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'));
}

How to verify in CSS that text fields/other fields are disabled for editing in Selenium 2?

The page is created by CSS using jquery and I am using selenium2 and junit for verification. It would be of great help if anyone can provide a way to identify that the fields are disabled and use an assert statement.
I assume that you are using Selenium 2 for your tests.
The WebDriver class provides a way to retrieve all WebElements on the page using the findElement and findElements methods. Both of these methods take selectors in the form of instances of the By class. So, in your case, you would have to use a selector that finds the input fields you are looking for, for instance with
WebElement element = webDriver.findElement(By.name("password"));
You can then use
if (element.isDisplayed() && element.isEnabled()) {
//your code here
}
or
assert (element.isDisplayed() && element.isEnabled());
to verify that the field is both displayed and enabled.

Drupal hook_form_alter: how to target only node/edit and NOT node/add. Aso, why #disabled doesn´t work?

I want to disable a cck field inside a specific form in Drupal 6.
I´ve created a new module full of alterations, using hook_form_alter.
The form id is articulo_node_form. The field in question is text, I´ve checked and it can be disabeled.
I´ve tried this:
function modding_form_articulo_node_form_alter(&$form, &$form_state, $form_id) {
$form['field_articulo_tipo']['#disabled'] = 1;
}
The field doesn´t disable at all, instead, it dissappears.
Anyway that happens when I try to create a new articulo node or when I try to edit that node. I want to target only node edits, I mean, the same form, but when it´s being edited.
What´s wrong with that code?
THANKS FOR YOUR HELP!!
Rosamunda
I can't think of a reason why the code you've got wouldn't work but you can use code like this to 'force' the attribute to be displayed:
$form['field_articulo_tipo']['#attributes']['disabled'] = 'disabled';

Silverstripe How to use Calendar on DateFields with custom form template?

I have created a custom form class and template for my form by following the instructions here.
However I am having trouble with adding DateFields with calendars. Usually I just do something like this:
$dateField = new DateField ('DateRequired', 'Date Required');
$dateField->setConfig ('showcalendar', true);
I have tried the above code in my custom form however the page doesn't include any of the jquery ui scripts or css files for the calendar field.
So my question is how can I get the my custom form to include all the scripts and render the fields with the jquery ui calendars?
actually the change of the template should not be necessary.
$dateField = new DateField('...');
$dateField->setConfig('showcalendar', true);
$dateField->setConfig('showdropdown', true);
$dateField->setConfig('dateformat', 'dd.MM.YYYY');
Ok I have worked it out. The code within the template that calls the fields needs to be changed from:
$dataFieldByName(DateRequired)
to:
$dataFieldByName(DateRequired).FieldHolder
Now all of the javascript is included within the page.

Resources