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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • validateForm hook not working for select field
Pages: [1]

Author Topic: validateForm hook not working for select field  (Read 2028 times)

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
validateForm hook not working for select field
October 26, 2012, 01:02:13 pm
I have a profile form that has multi-record values from one row of one custom_group_id and a table at the bottom that pulls in records from a second custom_group_id. The problem I am having is that the error message for the select validation (in the multi-record table) that is being returned is not being displayed in the profile form.

For clarification, the profile is for a contact that has a role of coordinator at an event to report on the event.  The profile is mostly custom fields that are fields from one row of a custom value table related to this event. But at the bottom of the profile is a table that includes products being promoted at the event for the sponsor. That table has multiple rows from a different multi-record table with multiple rows for the event. The entity for both tables is the contact (I am using a subtype). The fields used to fill the options is from yet another multi-record table and the saved value for the select is a key to that table. Hidden fields passed in when the profile is created help connect everything.

The $fields parameter passed to the civicrm_validateForm hook includes all the fields of the profile correctly, and the $error parameter has all the correct error messages set correctly just prior to returning. The Smarty debug window also shows the correct error messages for the element. It is just not being displayed, and I do not know how it gets displayed. It all works for the other elements. It is just the two select elements that are not working.

I have added the data array for all the form elements to the form using  $form->assign('dataFields', $dataFields) and added the select elements using

$form->add( 'Select', $customField['element_name'], $customField['label'], NULL, TRUE);
I also tried
$form->addSelect($customField['element_name'], $customField['label'], NULL, TRUE); but it adds an "_id" string at the end of the name.

I was also not able to get the options for the select boxes to add through the civicrm_customFieldOptions hook for the select field elements. The hook was not being called for the select boxes and after some hours I gave up and added them manually to the template instead of using {$form.$element_name.html} as I had done for all the other elements.

I added
Code: [Select]
... prior table cells ...
<td class="html-adjust custom_{$field_id}" >
<select name="{$element_name}" id="{$element_name}" class="form-select required">
    <option value=>- select -</option>
    {foreach from=$eventProducts key=k item=eventProduct}
   <option value="{$eventProduct.product_id}">{$eventProduct.product_name}</option>
   {/foreach}
</select>
</td>
... subsequent cells .....

The result from Firebug is
Code: [Select]

...
<td class="html-adjust custom_81">
<select class="form-select required" id="custom_81_-1" name="custom_81_-1">
    <option value="">- select -</option>
    <option value="20">Product #1</option>
    <option value="21">Product #2</option>
</select>
</td>
...


and similarly for each row except for using -2 for row 2, etc.

I just can not see how to get the error that was sent back by the civicrm_validateForm to display on the template. It works for all the other element types on the profile by just returning the error from the hook, but I am mostly copying approaches I found in templates handling custom fields I find in the civicrm code (e.g. in code for custom sets tabs of the contact edit screens) without yet fully understanding how it all works.

I had also marked the field as required when it was added, and it shows up in the civicrm_custom_fields as required, but the system validation outside the hook does not work either. Without further hacking by resorting to writing my own javascript instead of using the hook, I am at a loss.

I am new to this and would appreciated any help.

Jere

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: validateForm hook not working for select field
October 26, 2012, 04:00:02 pm

1. can u show us your validation code

2. Seems like your addSelect call is wrong. Check the function in CRM/Core/Form.php

Code: [Select]
  function addSelect($name, $label, $prefix = NULL, $required = NULL, $extra = NULL, $select = '- select -') {

or an example of add from

CRM/Contact/Form/Edit/Individual.php

Code: [Select]
      $prefix = CRM_Core_PseudoConstant::individualPrefix();
      if (!empty($prefix)) {
        $form->addElement('select', 'prefix_id', ts('Prefix'), array('' => '') + $prefix);
      }

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: validateForm hook not working for select field
October 28, 2012, 03:49:02 pm
Lobo,

Although the validation of the Select does not show the error messages beside the select elements when I hand code the options (using a foreach loop and an array passed by $form->assign in the civicrm_buildForm hook), they show up at the top of the form . I was just hitting the Save on an empty form and I had overlooked them in the list of other validate error messages.

When I use {$form.$element_name.html} the message shows up beside the element like it should but there are no options in the select box because the civicrm_customFieldOptions hook is not getting called. At least the handcoded works (with the error message only at the top and not beside the element), but it may have to do until after I get other things on the site working.

I can see how the hooks are called for the regular templates, but not why the customFieldOptions hook is not called or how to call it myself from either the CRM/Profile/Form/Edit.php or one of the other hooks.

Here are the responses to your last message:

(1)
I have attached the validation code. It is a separate function called from the civicrm_validateForm hook where I filter based on the profile id.

(2)
I am not using addSelect. I tried it and it has the same problem as add('Select'. ...)   and it adds "_id" to the end of the name.

I am also not using addElement. The add function calls the addElement. I also tried the addElement and it has the same problem.

The syntax of the add function in CRM/Core/Form.php is   
function &add($type, $name, $label = '',     $attributes = '', $required = FALSE, $javascript = NULL )
Mine is:
$form->add( 'Select', $customField['element_name'], $customField['label'], NULL, TRUE);

I could investigate why the customFieldOptions is not being called for a custom value in a profile, but after a few hours of trying to figure it out have given up for now. If I had some idea about where it should be called for a profile I might be able to figure it out. I tried a simple test profile with one select box and had the same problem.

Jere

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: validateForm hook not working for select field
October 28, 2012, 04:57:43 pm

Is your hook code adding the select form element for the "custom field"? I think so

in that case, civi is not aware of that custom field being on the form and hence will not call any hooks. Civi only calls the hooks for the form elements that it renders onto the page

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: validateForm hook not working for select field
October 29, 2012, 08:53:25 am
Yes, I am adding all the custom fields to the form in the buildForm hook using the $form->add for the elements and $form-> assign for the custom_field information I need for the display (like pre_help, post_help, data_type, html_type, etc.) and then adding all the smarty/html to the template to put it in place on the page.

Civicrm actually works quite well for this. When I add all the elements to the form this way (hidden fields too), using the same format as you use in the standard custom field templates (e.g. "custom_xx_xx" names, etc.) civi saves it all for me. How does it know?  I have not explored yet how it does it, but it is cool.

For the table in the form where I am pulling in multiple rows from another customGroup to the same profile, it saves only one row of the table, but I was surprised it did that. I marked all of those fields view-only in the administration page where I initially add the custom fields and then save the rows myself to the database in the postProcess hook. Or rather I am writing that code now, but all the data shows up in the $_POST and from there I can add it using either apis or writing the INSERTS. I haven't done the UPDATE's for this form yet, but I did a similar for a smaller profile and it worked well.

I initiate all the url calls for different profiles from a table I created using a view profile. I first examine the database to see if records exist or not to determine what url parameters to use in the links in the table.

I also followed the code you have in the standard templates that allows the user to add additional new rows using javascript. It all shows up in the postProcess hook where I can write the SQL to save it. It is all quite cool.

When I get to it, I think I can add the validation error message to the side of the custom select using jQuery/Javascript. This whole process has been slow since there were so many things that are new, but I am learning a lot and will have some upcoming projects where I will use it a lot more.

Jere

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: validateForm hook not working for select field
October 30, 2012, 03:50:53 pm
If you want to get the list of values for an option-group -- and run the proper hooks -- then I think you can use CRM_Core_OptionGroup::values(). For example, extending dlobo's snippet above, one might do this:

Code: [Select]
<?php
      $prefix 
= CRM_Core_OptionGroup::values('individual_prefix');
      if (!empty(
$prefix)) {
        
$form->addElement('select', 'prefix_id', ts('Prefix'), array('' => '') + $prefix);
      }

Notes:
 * I haven't actually used that function in a while, and it doesn't have any documentation/comments, so you may need to experiment/read a bit in order to get the return format right. The goal of calling it is produce an array in which array-keys are 'option values' (aka civicrm_option_value.value) and array-values are 'option labels' (aka civicrm_option_value.label).
 * The $name which you pass into CRM_Core_OptionGroup::values() is hard to predict. To figure out the group name for a custom field, you can do something like:

Code: [Select]
select ccf.id as field_id, cog.name as group_name
from civicrm_custom_field ccf
inner join civicrm_option_group cog
  on ccf.option_group_id = cog.id;

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: validateForm hook not working for select field
October 30, 2012, 05:29:18 pm

i commented a few of the functions in that file for 4.3. I'll go in and finish it off later tonite. You might want to take a look in svn if you want more details about the various parameters

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jere

  • I post occasionally
  • **
  • Posts: 41
  • Karma: 1
  • CiviCRM version: 4.3.3
  • CMS version: Drupal 7.22
  • MySQL version: 5.5.31
  • PHP version: 5.3.10
Re: validateForm hook not working for select field
October 30, 2012, 07:52:12 pm
Thanks to both of you so much for your help. It works. Thanks for being patient. I am a little slow to catch on sometimes when I am focused down a wrong path. 

I assume this would also work if I had a select item from a custom_group. I have another one where the select will be one where the user is selecting an id that is the key to a custom_value_xxx table (where the entity for the custom value set is the sponsoring organization not the current user). I assume if I have an api or query to get the keys and labels from that table and then put them in the ( id => label)  order for the addElement function call it would work the same.


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • validateForm hook not working for select field

This forum was archived on 2017-11-26.