/** * global form rule * * @param array $fields the input form values * * @return true if no errors, else array of errors * @access public * @static */ static function formRule(&$fields, $files, $self) { $errors = array(); if ($self->_id) { $contactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $self->_id, 'name'); } else { $contactName = ucfirst(CRM_Utils_String::munge($fields['label'])); } if (!CRM_Core_DAO::objectExists($contactName, 'CRM_Contact_DAO_ContactType', $self->_id)) { $errors['label'] = ts('This contact type name already exists in database. Contact type names must be unique.'); } return empty($errors) ? true : $errors; }
/** * Check if there is a record with the same name in the db * * @param string $value the value of the field we are checking * @param array $options the daoName and fieldName (optional ) * * @return boolean true if object exists * @access public * @static */ static function objectExists($value, $options) { $name = 'name'; if (isset($options[2])) { $name = $options[2]; } return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name)); }
/** * Global form rule. * * @param array $fields * The input form values. * * @param $files * @param $self * * @return bool|array * true if no errors, else array of errors */ public static function formRule($fields, $files, $self) { $errors = array(); if ($self->_id) { $contactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $self->_id, 'name'); } else { $contactName = ucfirst(CRM_Utils_String::munge($fields['label'])); } if (!CRM_Core_DAO::objectExists($contactName, 'CRM_Contact_DAO_ContactType', $self->_id)) { $errors['label'] = ts('This contact type name already exists in database. Contact type names must be unique.'); } $reservedKeyWords = CRM_Core_SelectValues::customGroupExtends(); //restrict "name" from being a reserved keyword when a new contact subtype is created if (!$self->_id && in_array($contactName, array_keys($reservedKeyWords))) { $errors['label'] = ts('Contact type names should not use reserved keywords.'); } return empty($errors) ? TRUE : $errors; }
/** * Global validation rules for the form. * * @param array $values * @param $files * @param $self * * @return array * list of errors to be posted back to the form */ public static function formRule($values, $files, $self) { $errors = array(); if (!empty($values['contact_name']) && !is_numeric($values['created_id'])) { $errors['contact_name'] = ts('Please select a valid contact.'); } if ($values['item_count'] && (!is_numeric($values['item_count']) || $values['item_count'] < 1)) { $errors['item_count'] = ts('Number of Transactions should a positive number'); } if ($values['total'] && (!is_numeric($values['total']) || $values['total'] <= 0)) { $errors['total'] = ts('Total Amount should be a positive number'); } if (!empty($values['created_date']) && date('Y-m-d') < date('Y-m-d', strtotime($values['created_date']))) { $errors['created_date'] = ts('Created date cannot be greater than current date'); } $batchName = $values['title']; if (!CRM_Core_DAO::objectExists($batchName, 'CRM_Batch_DAO_Batch', $self->_id)) { $errors['title'] = ts('This name already exists in database. Batch names must be unique.'); } return CRM_Utils_Array::crmIsEmptyArray($errors) ? TRUE : $errors; }
/** * Check if there is a record with the same name in the db * * @param string $value the value of the field we are checking * @param array $options the daoName and fieldName (optional ) * * @return boolean true if object exists * @access public * @static */ function objectExists($value, $options) { return CRM_Core_DAO::objectExists($value, $options[0], $options[1], CRM_Utils_Array::value(2, $options, 'name')); }