CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules »
  • Drupal Webform Integration »
  • Adding field validation to drupal component
Pages: [1]

Author Topic: Adding field validation to drupal component  (Read 1104 times)

VitorFF

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
  • CiviCRM version: 4.4.11
  • CMS version: Drupal 7.27
  • MySQL version: 5.5
  • PHP version: 5.4
Adding field validation to drupal component
February 09, 2015, 09:38:24 am
Hello all!

I have a webform where all of it's visible fields are programmatically generated based on a Civi's contact custom data.

All those fields are being added to the webform node as such: "$nodes[$iWebFormId]->webform['components'][] = $aField;" and each field's structure is:
 'nid'     
 'cid'     
 'pid'     
 'name'   
 'form_key'
 'type'   
 'value'   
 'required'
 'weight'
 'page_num'
 'extra'

What I am trying to do is to add validation rules to some of the fields I am adding. I've tried using the hook_alter_form but it is not being triggered.
I can trigger the hook_webform_submission_presave and see the data entered but it's too late for validation.

Any one was any idea on how to add validation rules to the fields as they are, or how to modify the way the fields are being created so that the alter_form hook is created?

All ideas/thoughts are always welcomed!

Thank you guys!
« Last Edit: February 09, 2015, 09:42:56 am by VitorFF »
Why don't you take a look at this? (You will not regret it!)

Capitalisation Corrector : https://civicrm.org/extensions/capitalisation-corrector

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Adding field validation to drupal component
February 09, 2015, 10:10:23 am
You might want to take a look at and emulate the way webform_civcirm does it. Basically use hook_form_alter to add a validation callback.
Try asking your question on the new CiviCRM help site.

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Adding field validation to drupal component
February 09, 2015, 12:31:36 pm
maybe skim reading too fast here but this has worked well for us

https://www.drupal.org/project/webform_validation
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

VitorFF

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
  • CiviCRM version: 4.4.11
  • CMS version: Drupal 7.27
  • MySQL version: 5.5
  • PHP version: 5.4
Re: Adding field validation to drupal component
February 11, 2015, 06:03:34 am
Hello guys!

Thanks very much for your help and replies.

I tried to use the hook_form_alter but I can't get it to trigger on my module.

I've tried adding my own validation rules using hook_webform_validation_validators and hook_webform_validation_validate. I've also added existing validation rules to the the dynamically generated components.

The problem is that the components being added to the webforms do not appear in the list of items to validate only on the full list of components.

If I check the drupal database I can see that the validation rule I added has the component ID's of the dynamically generated fields.


Below is a code snippet showing how I'm adding the validation rule.
Code: [Select]

// Implementation of hook_webform_validation_validate
function signup_additional_questions_webform_validation_validate($validator_name, $items, $components, $rule){
  if ( $validator_name == 'rule_name') {
    die(print_r($items,TRUE));
    return;
  } 
}


foreach ( array_values( $aNewFields ) as $aField ) {
      $nodes[$iWebFormId]->webform['components'][] = $aField;     
    }

  // validation rule array.
  $validation = array(
    'validator' => 'rule_name',
    'action' => 'add',
    'nid' => $iWebFormId,
    'rulename' => t('Rule name'),
  );

  $rule_components = array();
  foreach ( $nodes[$iWebFormId]->webform['components'] as $componentKey => $component ) {
    if ($component['type'] == 'textfield' ) {       
      $rule_components[$componentKey] = $component['name'];
    }     
  }

  if ( !empty($rule_components) ) {
    $validation['rule_components'] = $rule_components;
    webform_validation_rule_save($validation);
  }
Why don't you take a look at this? (You will not regret it!)

Capitalisation Corrector : https://civicrm.org/extensions/capitalisation-corrector

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Adding field validation to drupal component
February 11, 2015, 01:52:19 pm
Hmm, if you can't get hook_form_alter to fire then that sounds like a basic drupal module problem. Like maybe you misspelled the name of the hook? Or maybe your custom module is not enabled?
Drupal fires the hook_form_alter hook a lot so if you create a hook that prints some debug info to the screen you should see it fire probably multiple times per page load (e.g. once for your site's search bar if you have one, once for the webform, and once for any little forms you might have in your sidebar).
Try asking your question on the new CiviCRM help site.

VitorFF

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
  • CiviCRM version: 4.4.11
  • CMS version: Drupal 7.27
  • MySQL version: 5.5
  • PHP version: 5.4
Re: Adding field validation to drupal component
February 12, 2015, 02:22:23 am
Hi Coleman,

I double checked if the module is active, the hook name and syntax. They are all OK. I was able to temporarily write the validation code I need in another module and it works fine.

I am now trying to figure out why the hook_form_alter does not trigger in that particular module.
Why don't you take a look at this? (You will not regret it!)

Capitalisation Corrector : https://civicrm.org/extensions/capitalisation-corrector

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Adding field validation to drupal component
February 15, 2015, 04:21:12 pm
Did you figure it out?
Try asking your question on the new CiviCRM help site.

VitorFF

  • I post occasionally
  • **
  • Posts: 35
  • Karma: 1
  • CiviCRM version: 4.4.11
  • CMS version: Drupal 7.27
  • MySQL version: 5.5
  • PHP version: 5.4
Re: Adding field validation to drupal component
February 16, 2015, 01:19:19 am
Hello Coleman,

Sorry for the late update.

Without any configuration change or event code syntax alteration the hook started to work again and I don't know why.
With it working it was really easy to add the form validation I wanted via the validation callback.

I never figured out why the hook was not working in the first place, there were no log entries about it or any other error/warning.

Thank you guys for your help!
Vitor
Why don't you take a look at this? (You will not regret it!)

Capitalisation Corrector : https://civicrm.org/extensions/capitalisation-corrector

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules »
  • Drupal Webform Integration »
  • Adding field validation to drupal component

This forum was archived on 2017-11-26.