2

I have a Webform which I'm validating implementing the _validate event (i.e. altering $form["#validate"] by adding a custom validator which accepts &$form and &$form_state).

In my code I have something like this:

if ($nombre && !CasabacaS3SValidaciones::validarLongitud($nombre, 30))
{
    form_set_error('columnas][datos_personales][nombre', t('El nombre no debe superar 30 carácteres'));
    //what should I put in THIS LINE, replacing this comment, to add the error class?
}

I have no clue about adding the "error" class (e.g. fields marked as required have class "required", and also class "error" when the field is not set). How can I alter the field (take this example field in the form_set_error and assume input variables are &$form and &$form_state) so I can add the error for display?

NOTE: I'm seeking to add the "error" class to the fields, and NOT to the wrapping divs (i.e. those divs having form-item class).

(If a quick solution involves themes, please attach a link so I can read about because i'm banging my head about drupal yet, i'm a total n00b :s).

1

You could use a preprocess functions in your theme for this. You can add preprocess in your template.php.

function mytheme_preprocess_textfield(&$vars) { // OR preprocess_field.

  $vars['#attributes']['class'][] = 'test' ;
}

Check the $vars parameter for values that may help you determine the logic. For examples, just search your code base for preprocess_textfield or preprocess_field. For more inf

  • How do I install those theme handlers? Can you point me a link? (i.e. how to actually make "mytheme" to available to my forms). – Luis Masuelli Mar 14 '14 at 17:09
  • I edited my answer. If you have not idea what a preprocess function is then I suggest you read some tutorials about the thmeing system in drupal. For preprocesses functions you can take a look here drupal.org/node/11811 And here drupal.org/node/223430 – awm Mar 14 '14 at 17:14
  • Still not getting a clear idea. Documentation did not help me. – Luis Masuelli Mar 14 '14 at 17:54
  • found the current theme! but when i edit the template.php adding casabaca_preprocess_field(&$vars) { dpm($vars); }, and refreshing the cache, it does nothing (yes, casabaca.info is the theme name). – Luis Masuelli Mar 14 '14 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.