1

I added a text element to a New Event page using buildForm hook.

function mymodule_civicrm_buildForm($formName, &$form) {
 if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
    $form->add('text', 'custom_element', ts('Custom Element Label));
    CRM_Core_Region::instance('page-body')->add(array(
     'template' => 'CustomElement.tpl',
    ));
  }
}

And I added a validation code using validateForm hook which looks like this:

function mymodule_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
  if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
    if ( validation condition fails )
      $form->_errors['custom_element'] = 'Validation message';
    }
  }
}

But when validation fails, let alone error, the text element is not shown at all. How do I get it to show both the element and the error on the element?

2 Answers 2

2

It should be something like below

function mymodule_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
  if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
    if ( validation condition fails )
      $errors['custom_element'] = ts('Validation message');
    }
  }
}

HTH

1
1

Did you know that in your buildform hook - you can add a $form->addRule:

two real-life examples:

$form->addRule('bank_account_number', ts('%1 must contain only digits.', array(1 => ts('Bank Account Number'))), 'numeric');
$form->addRule('encrypted_credit_card_number', ts('%1 is a required field.', array(1 => ts('Encrypted'))), 'required');

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.