ENHANCEMENT Added Form->MessageType() to Form->extraClass() to make sure every tag in a formfield HTML container can be styled based on the validation results (previously it was only possible to style a <span class="message"> tag inside the container)

MINOR FormFied documentation

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73228 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-03-17 11:38:52 +00:00
parent 67f4e76cee
commit b6e115a874

View File

@ -96,6 +96,8 @@ class FormField extends RequestHandler {
/**
* Returns the field name - used by templates.
*
* @return string
*/
function Name() {
return $this->name;
@ -106,14 +108,22 @@ class FormField extends RequestHandler {
}
/**
* Returns the field message, used by form validation
* Returns the field message, used by form validation.
* Use {@link setError()} to set this property.
*
* @return string
*/
function Message(){
return $this->message;
}
/**
* Returns the field message type, used by form validation
* Returns the field message type, used by form validation.
* Arbitrary value which is mostly used for CSS classes
* in the rendered HTML, e.g. "required".
* Use {@link setError()} to set this property.
*
* @return string
*/
function MessageType(){
return $this->messageType;
@ -203,6 +213,8 @@ class FormField extends RequestHandler {
/**
* Compiles all CSS-classes. Optionally includes a "nolabel"-class
* if no title was set on the formfield.
* Uses {@link Message()} and {@link MessageType()} to add validatoin
* error classes which can be used to style the contained tags.
*
* @return String CSS-classnames
*/
@ -211,8 +223,16 @@ class FormField extends RequestHandler {
if(is_array($this->extraClasses)) {
$output = " " . implode($this->extraClasses, " ");
}
// Allow customization of label and field tag positioning
if(!$this->Title()) $output .= " nolabel";
// Allow custom styling of any element in the container based
// on validation errors, e.g. red borders on input tags.
// CSS-Class needs to be different from the one rendered
// through {@link FieldHolder()}
if($this->Message()) $output .= " holder-" . $this->MessageType();
return $output;
}
@ -341,10 +361,13 @@ class FormField extends RequestHandler {
$Name = $this->XML_val('Name');
$Field = $this->XML_val('Field');
// Only of the the following titles should apply
$titleBlock = (!empty($Title)) ? "<label class=\"left\" for=\"{$this->id()}\">$Title</label>" : "";
$messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : "";
$rightTitleBlock = (!empty($RightTitle)) ? "<label class=\"right\" for=\"{$this->id()}\">$RightTitle</label>" : "";
// $MessageType is also used in {@link extraClass()} with a "holder-" prefix
$messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : "";
return <<<HTML
<div id="$Name" class="field $Type $extraClass">$titleBlock<div class="middleColumn">$Field</div>$rightTitleBlock$messageBlock</div>
HTML;
@ -462,7 +485,9 @@ HTML;
function Type() {return strtolower(ereg_replace('Field$','',$this->class)); }
/**
* Construct and return HTML tag
* Construct and return HTML tag.
*
* @todo Transform to static helper method.
*/
function createTag($tag, $attributes, $content = null) {
$preparedAttributes = '';
@ -516,6 +541,10 @@ HTML;
return $this->Field();
}
/**
* @uses Validator->fieldIsRequired()
* @return boolean
*/
function Required() {
if($this->form && ($validator = $this->form->Validator)) {
return $validator->fieldIsRequired($this->name);