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) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Custom validation using a hook in an extension
Pages: [1]

Author Topic: Custom validation using a hook in an extension  (Read 749 times)

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Custom validation using a hook in an extension
February 11, 2014, 10:19:19 am
I'm trying to set up a custom search extension, and need some custom validation. Essentially there is a field which needs to be mandatory if - and only if - some other field has also been completed.

I know I can call the validateForm hook from within the extension's main php file, and that works, but the problem I'm having is making sure the custom validation only fires when the relevant form is being processed. The sample implementations of validateForm in the documentation describe using if ($formName == ) as a control structure around the validation test, but the form name in this case is 'CRM_Contact_Form_Search_Custom' and that's true for all my custom searches, whereas this validation is only applicable in this one specific case. Is there a recommended way to proceed? Can I force a change of form name, or should I use a different control structure?

Thanks

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Custom validation using a hook in an extension
February 11, 2014, 05:12:50 pm
1. Have you inspected the content of $form? There may be a property in the object that indicates the actual custom-search.

If you have Drupal's devel module, then add "dpm($form);" in your hook_civicrm_validateForm to spit out details about $form.

2. Is this modifying a custom-search class that you wrote - or one that was supplied by someone else? If it's one you wrote, then you might add something like this:

Code: [Select]
<?php
class MyCustomSearch /* ... */ {
  function 
buildForm(&$form) {
    
/* ... */
    
$form->addFormRule(array(__CLASS__, 'formRule'), $form);
    
/* ... */
  
}
  static function 
formRule($fields, $files, $options) {
    
/* ... */
  
}
}

You can find many examples of addFormRule by searching codebase, e.g. https://github.com/civicrm/civicrm-core/blob/4.4/CRM/Admin/Form/Setting/Component.php#L63

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Custom validation using a hook in an extension
February 11, 2014, 05:21:09 pm
If you're interested in the second option ($form->addFormRule), there isn't a whole lot of documentation... but this page includes some breakdown of how buildForm, addFormRule, and validate fit together:

http://wiki.civicrm.org/confluence/display/CRMDOC/QuickForm+Reference

nickholden

  • I post occasionally
  • **
  • Posts: 111
  • Karma: 1
  • CiviCRM version: 4.4.1
  • CMS version: Drupal 7
  • MySQL version: 5.5.32
  • PHP version: 5.4
Re: Custom validation using a hook in an extension
February 12, 2014, 11:00:33 am
Thanks, totten.  This is a custom search I am writing, although it's based on the Activity Search, which is itself, I believe, based on the sample custom search from core.

The validation itself worked fine for me using the validateForm hook - so I don't think I need to go down the addFormRule route. I just need to make sure that the validation doesn't accidentally trigger when a different custom search is validated.

Why do the custom searches all pass the same $formName to the validateForm hook? Wouldn't it be useful for this to reflect the specific custom class itself rather than 'CRM_Contact_Form_Search_Custom' or would that cause more problems elsewhere?

I've inspected $form (I couldn't survive ten minutes without drupal_set_message and print_r on my dev box, I suspect) but both the _customClass and _customSearchClass properties are protected so I don't seem to be able to access them. Pity.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Custom validation using a hook in an extension
February 13, 2014, 11:03:25 am
If you want to submit a patch which makes those properties accessible, that's fine, e.g.

Code: [Select]
<?php
class  CRM_Contact_Form_Search_Custom ... {
  public function 
getCustomClass() {
    return 
$this->_customClass;
  }
}

I'm not sure why they chose to have CRM_Contact_Form_Search_Custom delegate to the custom-search class (rather than, say, using inheritance). Don't know if it was a good or bad decision.

FWIW, if I were in your shoes, I'd probably move the validation code from the hook to the search-class -- imagining the perspective of a new developer, it would be more intuitive to find the form-building and form-validation in the same file.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Custom validation using a hook in an extension

This forum was archived on 2017-11-26.