Changeset 1414:ce67e9c592b7 for inc/admin/class.dc.form.php
- Timestamp:
- 08/16/13 15:48:37 (12 years ago)
- Branch:
- twig
- File:
-
- 1 edited
-
inc/admin/class.dc.form.php (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.form.php
r1319 r1414 277 277 public function renderField($name,$attributes=array(),$extra=array()) 278 278 { 279 $field = $this->currentForm-> $name;279 $field = $this->currentForm->getField($name); 280 280 if ($field) { 281 281 $attr = $field->getAttributes($attributes); … … 396 396 /** @var array(dcField) list of form errors */ 397 397 protected $errors; 398 398 /** @var array() list of form properties */ 399 protected $properties; 400 401 399 402 /** 400 403 * Class constructor … … 420 423 $this->hiddenfields = array(); 421 424 $this->errors = array(); 425 $this->properties = array(); 422 426 if ($method == 'POST') { 423 427 $this->addNonce(); … … 425 429 } 426 430 431 432 /** 433 * setProperty - sets form property 434 * 435 * @param string $name the property name. 436 * @param mixed $value the property value. 437 * 438 * @access public 439 */ 440 public function setProperty($prop,$value) { 441 $this->properties[$prop]=$value; 442 } 443 444 /** 445 * getProperty - gets form property 446 * 447 * @param string $name the property name. 448 * 449 * @return mixed the property value, null if no property found. 450 * @access public 451 */ 452 public function getProperty($prop) { 453 if (isset($this->properties[$prop])) { 454 return $this->properties[$prop]; 455 } else { 456 return null; 457 } 458 } 427 459 /** 428 460 * addTemplate - Adds a template file to enrich form fields … … 532 564 } 533 565 566 /** 567 * getField - retrieves a field form form 568 * 569 * @param string the field name 570 * 571 * @access public 572 * 573 * @return dcForm the requested field 574 */ 575 public function getField($name) { 576 if (isset($this->fields[$name])) { 577 return $this->fields[$name]; 578 } else { 579 return null; 580 } 581 } 582 534 583 /** 535 584 * removeField - removes a field … … 634 683 public function __get($name) 635 684 { 636 return isset($this->fields[$name]) ? 637 $this->fields[$name] : null; 685 if (isset($this->fields[$name])) { 686 $f = $this->fields[$name]; 687 if ($f->isMultiple()) { 688 return $f->getValues(); 689 } else { 690 return $f->getValue(); 691 } 692 } else { 693 return $this->getProperty($name); 694 } 638 695 } 639 696 … … 650 707 { 651 708 if (isset($this->fields[$name])) { 652 $this->fields[$name]->setValue($value); 653 } 654 } 655 656 /* public function isSubmitted() 657 { 658 $from = $this->method == 'POST' ? $_POST : $_GET; 659 } 660 */ 709 $f = $this->fields[$name]; 710 if ($f->isMultiple()) { 711 $this->fields[$name]->setValues($value); 712 } else { 713 $this->fields[$name]->setValue($value); 714 } 715 } else { 716 $this->setProperty($name,$value); 717 } 718 } 661 719 662 720 /** … … 667 725 protected function setupFields() { 668 726 $from = $this->method == 'POST' ? $_POST : $_GET; 669 foreach ($this->fields as $f) { 670 $f->setup($from); 727 if (!empty($from)) { 728 foreach ($this->fields as $f) { 729 $f->setup($from); 730 } 671 731 } 672 732 } … … 680 740 */ 681 741 protected function handleActions($submitted) { 742 $hasActions = false; 682 743 foreach ($submitted as $f) { 683 744 $action = $f->getAction(); 684 745 if ($action != NULL) { 746 if (!$hasActions) { 747 $this->core->callBehavior('coreBeforeFormSubmit',$this); 748 } 749 $hasActions = true; 685 750 $ret = call_user_func($action,$this); 686 751 } 752 } 753 if ($hasActions) { 754 $this->core->callBehavior('coreAfterFormSubmit',$this); 687 755 } 688 756 } … … 706 774 707 775 /** 776 * isSubmitted - returns whether form has been submitted or not 777 * 778 * @access public 779 * 780 * @return boolean true if the form has been submitted. 781 */ 782 public function isSubmitted() { 783 foreach ($this->submitfields as $f) { 784 if ($f->isDefined()) { 785 return true; 786 } 787 } 788 return false; 789 } 790 791 /** 708 792 * setup - sets up the form, given the parameters given to the page 709 793 * should be called after fields have been defined. … … 726 810 * @access public 727 811 */ 728 public function check() 729 { 812 public function check(dcAdminContext $ctx) 813 { 814 $valid = true; 730 815 foreach ($this->fields as $f) { 731 816 try { … … 733 818 } 734 819 catch (InvalidFieldException $e) { 735 $this->errors[] = $e->getMessage(); 820 $valid = false; 821 $ctx->addError($e->getMessage()); 736 822 } 823 } 824 if (!$valid) { 825 throw new InvalidFieldException ("Some fields are missing"); 737 826 } 738 827 } … … 796 885 797 886 /** 887 * defines whether a field is multiple or not 888 * 889 * @param boolean true if the field is multiple 890 * 891 * @access public 892 */ 893 public function setMultiple($m=true) { 894 $this->multiple = $m; 895 } 896 897 /** 898 * Returns whether can have multiple values or not 899 * 900 * @return boolean true if the field has multiple values 901 * 902 * @access public 903 */ 904 public function isMultiple($m=true) { 905 return $this->multiple; 906 } 907 908 /** 798 909 * setNID - defines fiels name & id 799 910 * … … 905 1016 abstract public function getWidgetBlock(); 906 1017 1018 public function isEmpty() { 1019 return (count($this->values) == 0) || empty($this->values[0]); 1020 } 907 1021 908 1022 /** … … 977 1091 public function check() 978 1092 { 979 if (!$this->defined && $this->options['mandatory']) { 980 throw new InvalidFieldException(sprintf( 981 'Field "%s" is mandatory', 982 $this->attributes['label']) 983 ); 1093 if (isset($this->options ['required']) && $this->options['required']) { 1094 if (!$this->defined || $this->isEmpty()) { 1095 throw new InvalidFieldException(sprintf( 1096 'Field "%s" is mandatory', 1097 $this->options['label']) 1098 ); 1099 } 984 1100 } 985 1101 } … … 1088 1204 class dcFieldCheckbox extends dcField 1089 1205 { 1206 protected $checked; 1207 1208 public function __construct($name,$values,$options=array()) 1209 { 1210 $val = array(); 1211 if (!is_array($values)) { 1212 $values = array("1" => !empty($values)); 1213 } 1214 $this->checked = $values; 1215 parent::__construct($name,array_keys($values),$options); 1216 } 1217 1218 /** 1219 * setValue - sets field value 1220 * 1221 * @param mixed $value field value. 1222 * @param int $offset value offset to define (default 0). 1223 * 1224 * @access public 1225 */ 1226 public function setValue($value,$offset=0) { 1227 $this->checked[$this->values[0]] = $value; 1228 } 1229 1230 public function getValue($offset=0) { 1231 $val = parent::getValue($offset); 1232 if (isset($this->checked[$val])) { 1233 return $this->checked[$val]?$val:false; 1234 } else { 1235 return false; 1236 } 1237 } 1238 1239 public function getAttributes($options) 1240 { 1241 $a = parent::getAttributes($options); 1242 1243 $val = $a['value']; 1244 if (isset($this->checked[$val]) && $this->checked[$val]) { 1245 $a['checked']='checked'; 1246 } 1247 return $a; 1248 } 1249 1250 public function setup($from) 1251 { 1252 $values = $this->parseValues($from); 1253 foreach ($this->checked as $k=>&$v) { 1254 $v=false; 1255 } 1256 foreach ($values as $v) { 1257 $this->checked[$v] = true; 1258 } 1259 $this->setValues(array_keys($this->checked)); 1260 } 1261 1090 1262 public function getWidgetBlock() 1091 1263 { … … 1094 1266 1095 1267 public function getDefaultValue() { 1096 return 0;1268 return false; 1097 1269 } 1098 1270 } … … 1139 1311 { 1140 1312 protected $combo; 1313 protected $combo_values; 1141 1314 1142 1315 public function __construct($name,$value,$combo,$options=array()) 1143 1316 { 1144 1317 $this->combo = $combo; 1318 $this->combo_values = $combo; 1319 foreach ($combo as $k=>$v) { 1320 if (is_array($v)) { 1321 unset($this->combo_values[$k]); 1322 $this->combo_values = array_merge($v,$this->combo_values); 1323 } 1324 } 1145 1325 parent::__construct($name,$value,$options); 1146 1326 } … … 1161 1341 } 1162 1342 foreach ($values as &$v) { 1163 if (!isset($this->combo[$v])) 1164 $v = $this->getDefaultValue(); 1343 if (!isset($this->combo_values[$v])) { 1344 $v = $this->getDefaultValue(); 1345 } 1165 1346 } 1166 1347 return $values;
Note: See TracChangeset
for help on using the changeset viewer.
