/** * Simple easy to use wrapper around addElement. Deal with * simple validation rules * * @param string $type * @param string $name * @param string $label * @param string|array $attributes (options for select elements) * @param bool $required * @param array $extra * (attributes for select elements). * * @return HTML_QuickForm_Element could be an error object */ public function &add($type, $name, $label = '', $attributes = '', $required = FALSE, $extra = NULL) { if ($type == 'wysiwyg') { $attributes = ($attributes ? $attributes : array()) + array('class' => ''); $attributes['class'] .= ' crm-form-wysiwyg'; $type = "textarea"; } if ($type == 'select' && is_array($extra)) { // Normalize this property if (!empty($extra['multiple'])) { $extra['multiple'] = 'multiple'; } else { unset($extra['multiple']); } // Add placeholder option for select if (isset($extra['placeholder'])) { if ($extra['placeholder'] === TRUE) { $extra['placeholder'] = $required ? ts('- select -') : ts('- none -'); } if (($extra['placeholder'] || $extra['placeholder'] === '') && empty($extra['multiple']) && is_array($attributes) && !isset($attributes[''])) { $attributes = array('' => $extra['placeholder']) + $attributes; } } } $element = $this->addElement($type, $name, $label, $attributes, $extra); if (HTML_QuickForm::isError($element)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } if ($required) { if ($type == 'file') { $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'uploadedfile'); } else { $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'required'); } if (HTML_QuickForm::isError($error)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } } return $element; }
/** * Simple easy to use wrapper around addElement. * * Deal with simple validation rules. * * @param string $type * @param string $name * @param string $label * @param string|array $attributes (options for select elements) * @param bool $required * @param array $extra * (attributes for select elements). * * @return HTML_QuickForm_Element * Could be an error object */ public function &add($type, $name, $label = '', $attributes = '', $required = FALSE, $extra = NULL) { // Fudge some extra types that quickform doesn't support if ($type == 'wysiwyg' || in_array($type, self::$html5Types)) { $attributes = ($attributes ? $attributes : array()) + array('class' => ''); $attributes['class'] = ltrim($attributes['class'] . " crm-form-{$type}"); $type = $type == 'wysiwyg' ? 'textarea' : 'text'; } // @see http://wiki.civicrm.org/confluence/display/CRMDOC/crmDatepicker if ($type == 'datepicker') { $attributes = $attributes ? $attributes : array(); $attributes['data-crm-datepicker'] = json_encode((array) $extra); $type = "text"; } if ($type == 'select' && is_array($extra)) { // Normalize this property if (!empty($extra['multiple'])) { $extra['multiple'] = 'multiple'; } else { unset($extra['multiple']); } unset($extra['size'], $extra['maxlength']); // Add placeholder option for select if (isset($extra['placeholder'])) { if ($extra['placeholder'] === TRUE) { $extra['placeholder'] = $required ? ts('- select -') : ts('- none -'); } if (($extra['placeholder'] || $extra['placeholder'] === '') && empty($extra['multiple']) && is_array($attributes) && !isset($attributes[''])) { $attributes = array('' => $extra['placeholder']) + $attributes; } } } $element = $this->addElement($type, $name, $label, $attributes, $extra); if (HTML_QuickForm::isError($element)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } if ($required) { if ($type == 'file') { $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'uploadedfile'); } else { $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'required'); } if (HTML_QuickForm::isError($error)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } } return $element; }
/** * Return a textual error message for an QuickForm error code * * @access public * @param int error code * @return string error message * @static */ function errorMessage($value) { // make the variable static so that it only has to do the defining on the first call static $errorMessages; // define the varies error messages if (!isset($errorMessages)) { $errorMessages = array(QUICKFORM_OK => 'no error', QUICKFORM_ERROR => 'unknown error', QUICKFORM_INVALID_RULE => 'the rule does not exist as a registered rule', QUICKFORM_NONEXIST_ELEMENT => 'nonexistent html element', QUICKFORM_INVALID_FILTER => 'invalid filter', QUICKFORM_UNREGISTERED_ELEMENT => 'unregistered element', QUICKFORM_INVALID_ELEMENT_NAME => 'element already exists', QUICKFORM_INVALID_PROCESS => 'process callback does not exist', QUICKFORM_DEPRECATED => 'method is deprecated', QUICKFORM_INVALID_DATASOURCE => 'datasource is not an object'); } // If this is an error object, then grab the corresponding error code if (HTML_QuickForm::isError($value)) { $value = $value->getCode(); } // return the textual error message corresponding to the code return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[QUICKFORM_ERROR]; }
/** * Simple easy to use wrapper around addElement. Deal with * simple validation rules * * @param string type of html element to be added * @param string name of the html element * @param string display label for the html element * @param string attributes used for this element. * These are not default values * @param bool is this a required field * * @return object html element, could be an error object * @access public * */ function &add($type, $name, $label = '', $attributes = '', $required = false, $javascript = null) { $element =& $this->addElement($type, $name, $label, $attributes, $javascript); if (HTML_QuickForm::isError($element)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } if ($required) { $error = $this->addRule($name, ts('%1 is a required field.', array(1 => $label)), 'required'); if (HTML_QuickForm::isError($error)) { CRM_Core_Error::fatal(HTML_QuickForm::errorMessage($element)); } } return $element; }