1

I am attempting to perform form validation on various CiviCRM forms (profile forms and forms within contribution pages) by hooking into hook_civicrm_validateForm.

In my php code, I would like to differentiate between the different forms so that I can execute appropriate validation functions. According to the CiviCRM documentation, this can be done by checking against the $formName parameter.

While debugging, I found out that a contribution page I am using has a $formName of CRM_Contribute_Form_Contribution_Main. For a profile form its $formName is CRM_Profile_Form_Edit.

Is there a way of finding out how the names of form are defined? If so, does CiviCRM supporting assigning forms with custom form names?

4
  • 1
    Are you trying to find a particular profile form? That can be achieved by checking the gid as well as the form name.
    – Coleman
    Commented Jun 1, 2018 at 22:05
  • Yes I am. I would like to also find particular profiles via their gid. After taking a look at the class reference of the CRM_Contribute_Form_ContributionBase class, the id can be retrieve via _id. The problem with profile forms extending CRM_Profile_Form is that the gid is a protected int with no way of accessing it. This is a bummer, I am still trying to find a way.
    – Ivan
    Commented Jun 2, 2018 at 8:01
  • UPDATE: actually I found out it is indeed possible to read the _gid property. This can be done by calling $form->get_var('_gid'). I found some example code within a CiviCRM forum post
    – Ivan
    Commented Jun 4, 2018 at 7:36
  • Actually the function name is getVar not get_var but yes that's right. Nice work finding the solution.
    – Coleman
    Commented Jun 4, 2018 at 15:46

2 Answers 2

2

In principle the names are based on the files, so the form which is CRM/Contact/Form/Contact.php will be CRM_Contact_Form_Contact.

AFAIK the name of the form will always be commented if you inspect the form with Firebug, Firefox or Chrome.

In some cases forms will be called from pages (like the contribution page). This is kinda hard to find out, I always do a post hook in my extensions to log the $formName parameter to find out what forms are called during the process.

AFAIK you will need to include a call to the form hook to make sure your customized form is also usable in a validateForm hook (see below). But I would certainly try first if the parent CRM_Core_Form does not handle that and you can indeed already check for your customized form name.

    CRM_Utils_Hook::validateForm(
  get_class($this),
  $this->_submitValues,
  $this->_submitFiles,
  $this,
  $hookErrors
);
2
  • Hmm that sounds like a bit of overkill. It's too bad that profile/contribution title values are not made available through the $form parameter. It would make differentiating between forms for validation much simpler.
    – Ivan
    Commented May 22, 2018 at 11:57
  • Well, if you just check the formNames once you will get them! I have recently done some stuff on contribution pages so I think you can get there without too much hardship? Commented May 22, 2018 at 12:03
0

The form names are usually the same as the class name of the form object. And the class names follow a pattern as CRM_aaa_Form_bbb meaning that this class resides in the file CRM/aaa/Form/bbb.php

There is no possibility to define custom form names. Or at least I am not aware of it and I would object against it because then the same form could be named differently in different installations and that makes the whole thing more complex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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