From b6e115a874d9881e08654ba86e4b83d5772c5a5a Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 17 Mar 2009 11:38:52 +0000 Subject: [PATCH] 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 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 --- forms/FormField.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/forms/FormField.php b/forms/FormField.php index a103d22c6..86679665c 100644 --- a/forms/FormField.php +++ b/forms/FormField.php @@ -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)) ? "" : ""; - $messageBlock = (!empty($Message)) ? "$Message" : ""; $rightTitleBlock = (!empty($RightTitle)) ? "" : ""; + // $MessageType is also used in {@link extraClass()} with a "holder-" prefix + $messageBlock = (!empty($Message)) ? "$Message" : ""; + return <<$titleBlock
$Field
$rightTitleBlock$messageBlock 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);