I have a custom field on contact in radio type format, not required field. I'd like to set up custom error on this field by using hook_civicrm_validateForm
when the field is null.
// hook in extension
function hook_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {}
$fields
array doesn't contain a key for this custom field. After setting $error['custom_xx']
the form is reloaded without displaying the error.
What I found:
$form->_elementIndex
contains all needed keys, including this for radio. So I suppose this is not a problem.
I think that problem is caused by using _submitValues[]
as a list of fields because _submitValues doesn't contain null radio custom fields:
// CRM_Core_Form->validate():
CRM_Utils_Hook::validateForm(
get_class($this),
$this->_submitValues,
$this->_submitFiles,
$this,
$hookErrors
);
// my hook
function example_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName == 'CRM_Contact_Form_Contact') {
if (in_array('AA', $fields['contact_sub_type'])) {
$fieldIndex = 'custom_70-1';
if (!in_array($fieldIndex, array_keys($fields)) || !$fields[$fieldIndex]) {
$errors[$fieldIndex] = 'For AA contact type this field is required';
}
}
}
}
Edit:
Here is my workaround - add missing keys to _submitValues[] array https://github.com/caltha-eu/civicrm-core/commit/b4ee607334f3f091bffc2c32a87d862e4a3d7493
foreach ($this->_elementIndex as $elementIndex => $index) {
if (!in_array($elementIndex, array_keys($this->_submitValues))) {
$this->_submitValues[$elementIndex] = NULL;
}
}