From 8d8b6688cbbb1febe92012ccc2a4158fa594fcb3 Mon Sep 17 00:00:00 2001 From: wei <> Date: Wed, 5 Jul 2006 04:23:43 +0000 Subject: Fixed #223 #225 --- framework/Web/Javascripts/js/validator.js | 8 +++- framework/Web/Javascripts/prado/validation3.js | 15 +++++++ framework/Web/UI/WebControls/TRadioButton.php | 36 ++++++++++++++++ .../Web/UI/WebControls/TRequiredFieldValidator.php | 49 +++++++++++++++++----- 4 files changed, 95 insertions(+), 13 deletions(-) (limited to 'framework/Web') diff --git a/framework/Web/Javascripts/js/validator.js b/framework/Web/Javascripts/js/validator.js index f77534a7..959dd193 100644 --- a/framework/Web/Javascripts/js/validator.js +++ b/framework/Web/Javascripts/js/validator.js @@ -128,9 +128,13 @@ switch(this.options.ControlType) return this.trim($F(control));else {this.observeDatePickerChanges();return Prado.WebUI.TDatePicker.getDropDownDate(control).getTime();} case'THtmlArea':if(typeof tinyMCE!="undefined") -tinyMCE.triggerSave();return this.trim($F(control));default:if(this.isListControlType()) +tinyMCE.triggerSave();return this.trim($F(control));case'TRadioButton':if(this.options.GroupName) +return this.getRadioButtonGroupValue();default:if(this.isListControlType()) return this.getFirstSelectedListValue();else -return this.trim($F(control));}},observeDatePickerChanges:function() +return this.trim($F(control));}},getRadioButtonGroupValue:function() +{name=this.control.name;value="";$A(document.getElementsByName(name)).each(function(el) +{if(el.checked) +value=el.value;});return value;},observeDatePickerChanges:function() {if(Prado.Browser().ie) {var DatePicker=Prado.WebUI.TDatePicker;this.observeChanges(DatePicker.getDayListControl(this.control));this.observeChanges(DatePicker.getMonthListControl(this.control));this.observeChanges(DatePicker.getYearListControl(this.control));}},getSelectedValuesAndChecks:function(elements,initialValue) {var checked=0;var values=[];var isSelected=this.isCheckBoxType(elements[0])?'checked':'selected';elements.each(function(element) diff --git a/framework/Web/Javascripts/prado/validation3.js b/framework/Web/Javascripts/prado/validation3.js index 396b4356..9ee7ed2f 100644 --- a/framework/Web/Javascripts/prado/validation3.js +++ b/framework/Web/Javascripts/prado/validation3.js @@ -778,6 +778,9 @@ Prado.WebUI.TBaseValidator.prototype = if(typeof tinyMCE != "undefined") tinyMCE.triggerSave(); return this.trim($F(control)); + case 'TRadioButton': + if(this.options.GroupName) + return this.getRadioButtonGroupValue(); default: if(this.isListControlType()) return this.getFirstSelectedListValue(); @@ -786,6 +789,18 @@ Prado.WebUI.TBaseValidator.prototype = } }, + getRadioButtonGroupValue : function() + { + name = this.control.name; + value = ""; + $A(document.getElementsByName(name)).each(function(el) + { + if(el.checked) + value = el.value; + }); + return value; + }, + /** * Observe changes in the drop down list date picker, IE only. */ diff --git a/framework/Web/UI/WebControls/TRadioButton.php b/framework/Web/UI/WebControls/TRadioButton.php index 9198be1b..6ddd7c96 100644 --- a/framework/Web/UI/WebControls/TRadioButton.php +++ b/framework/Web/UI/WebControls/TRadioButton.php @@ -37,6 +37,8 @@ Prado::using('System.Web.UI.WebControls.TRadioButtonList'); * between posts to the server. * * TRadioButton uses {@link setGroupName GroupName} to group together a set of radio buttons. + * Once the {@link setGroupName GroupName} is set, you can use the {@link getRadioButtonsInGroup} + * method to get an array of TRadioButtons having the same group name. * * If {@link setAutoPostBack AutoPostBack} is set true, changing the radio button state * will cause postback action. And if {@link setCausesValidation CausesValidation} @@ -53,6 +55,10 @@ Prado::using('System.Web.UI.WebControls.TRadioButtonList'); */ class TRadioButton extends TCheckBox { + /** + * @param array list of radio buttons registered. + */ + private static $_radiobuttons=array(); /** * @var string the name used to fetch radiobutton post data */ @@ -99,6 +105,36 @@ class TRadioButton extends TCheckBox public function setGroupName($value) { $this->setViewState('GroupName',$value,''); + $this->registerRadioButton($this); + } + + /** + * Register radio button control grouping. + * @param TRadioButton control to add + */ + private function registerRadioButton(TRadioButton $control) + { + $id = $control->getUniqueID(); + if(!isset(self::$_radiobuttons[$id])) + self::$_radiobuttons[$id] = $control; + } + + /** + * Gets an array of RadioButtons with same group name. + * Radio buttons are grouped when the GroupName property is set. + * This method will always return at least the current radio button in the array. + * @return array list of TRadioButton with the same group + */ + public function getRadioButtonsInGroup() + { + $group = $this->getUniqueGroupName(); + $buttons = array(); + foreach(self::$_radiobuttons as $control) + { + if($control->getUniqueGroupName() === $group) + $buttons[] = $control; + } + return count($buttons) > 0 ? $buttons : array($this); } /** diff --git a/framework/Web/UI/WebControls/TRequiredFieldValidator.php b/framework/Web/UI/WebControls/TRequiredFieldValidator.php index b8a939ad..c99d9c19 100644 --- a/framework/Web/UI/WebControls/TRequiredFieldValidator.php +++ b/framework/Web/UI/WebControls/TRequiredFieldValidator.php @@ -76,22 +76,47 @@ class TRequiredFieldValidator extends TBaseValidator protected function evaluateIsValid() { $control = $this->getValidationTarget(); - $initial = trim($this->getInitialValue()); if($control instanceof TListControl) + return $this->validateListControl($control); + else if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0) + return $this->validateRadioButtonGroup($control); + else + return $this->validateStandardControl($control); + } + + private function validateListControl($control) + { + $initial = trim($this->getInitialValue()); + $count = 0; + foreach($control->getItems() as $item) { - $count = 0; - foreach($control->getItems() as $item) - { - if($item->getSelected() && $item->getValue() != $initial) - $count++; - } - return $count > 0; + if($item->getSelected() && $item->getValue() != $initial) + $count++; } - else + return $count > 0; + } + + private function validateRadioButtonGroup($control) + { + $initial = trim($this->getInitialValue()); + foreach($control->getRadioButtonsInGroup() as $radio) { - $value=$this->getValidationValue($control); - return trim($value)!==$initial || (is_bool($value) && $value); + if($radio->getChecked()) + { + if(strlen($value = $radio->getValue()) > 0) + return $value !== $initial; + else + return true; + } } + return false; + } + + private function validateStandardControl($control) + { + $initial = trim($this->getInitialValue()); + $value=$this->getValidationValue($control); + return trim($value)!==$initial || (is_bool($value) && $value); } /** @@ -105,6 +130,8 @@ class TRequiredFieldValidator extends TBaseValidator $control = $this->getValidationTarget(); if($control instanceof TListControl) $options['TotalItems'] = $control->getItemCount(); + if($control instanceof TRadioButton && strlen($control->getGroupName()) > 0) + $options['GroupName'] = $control->getGroupName(); return $options; } } -- cgit v1.2.3