if (is_array($fieldValue)) {
foreach ($fieldValue as &$value) {
if (!is_array($value)) {
- _civicrm_api3_api_match_pseudoconstant_value($value, $options, $fieldName);
+ _civicrm_api3_api_match_pseudoconstant_value($value, $options, $fieldName, CRM_Utils_Array::value('api.required', $fieldInfo));
}
}
// TODO: unwrap the call to implodePadded from the conditional and do it always
}
}
else {
- _civicrm_api3_api_match_pseudoconstant_value($fieldValue, $options, $fieldName);
+ _civicrm_api3_api_match_pseudoconstant_value($fieldValue, $options, $fieldName, CRM_Utils_Array::value('api.required', $fieldInfo));
}
}
* @param string $value field value
* @param array $options array of options for this field
* @param string $fieldName field name used in api call (not necessarily the canonical name)
+ * @param bool $isRequired
+ * Is this a required field or is 'null' an acceptable option. We allow 'null' last
+ * in case we have the weird situation of it being a valid option in which case our
+ * brains will probably explode.
*
* @throws API_Exception
*/
-function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldName) {
+function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldName, $isRequired) {
// If option is a key, no need to translate
// or if no options are avaiable for pseudoconstant 'table' property
if (array_key_exists($value, $options) || !$options) {
$options = array_map("strtolower", $options);
$newValue = array_search($newValue, $options);
if ($newValue === FALSE) {
+ if ($value === 'null' && !$isRequired) {
+ // CiviMagic syntax for Nulling out the field - let it through.
+ return;
+ }
throw new API_Exception("'$value' is not a valid option for field $fieldName", 2001, array('error_field' => $fieldName));
}
$value = $newValue;
$contact = $this->callAPISuccessGetSingle('Contact', array('id' => $contact['id'], 'return' => 'postal_greeting'));
$this->assertEquals('Dear Alan Mice', $contact['postal_greeting_display']);
+ // Set contact to have no postal greeting & check it is correct.
+ $this->callAPISuccess('Contact', 'create', array('id' => $contact['id'], 'postal_greeting_id' => 'null'));
+ $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $contact['id'], 'return' => 'postal_greeting'));
+ $this->assertEquals('', $contact['postal_greeting_display']);
+
//Cleanup
$this->callAPISuccess('OptionValue', 'create', array('id' => $postalOption['id'], 'name' => 'Dear {contact.first_name}'));
$this->customFieldDelete($ids['custom_field_id']);