Skip to content
This repository

Multi value support for form fields etc. #233

Merged
merged 5 commits into from over 1 year ago

2 participants

Imants Horsts Romans Malinovskis
Imants Horsts

Bug report:
https://groups.google.com/forum/?fromgroups=#!topic/agile-toolkit-devel/DE8U4oxz278

Test case:

class page_test extends Page {
    function init(){
        parent::init();

        // Main content
        $f = $this->add('Form');
        $f->addSubmit('Test');

        $f->add('H3')->set('ValueList');
            $f->addField('ValueList','list_single','Single');
            $f->addField('ValueList','list_multi','Multi')->setAttr('multiple');
        $f->add('H3')->set('DropDown');
            $f->addField('DropDown','dd_single','Single');
            $f->addField('DropDown','dd_multi','Multi')->setAttr('multiple');
        $f->add('H3')->set('CheckboxList');
            $f->addField('CheckboxList','cbox_single','Single');
            $f->addField('CheckboxList','cbox_multi','Multi')->setAttr('multiple');
        $f->add('H3')->set('Radio');
            $f->addField('Radio','radio_single','Single');
            $f->addField('Radio','radio_multi','Multi')->setAttr('multiple');

        $f->onSubmit(function($form){
            $form->js()
                ->append('<h3>Data POSTed</h3><pre>'.var_export($form->get(),true).'</pre>')
                ->execute();
        });
    }
}
Romans Malinovskis romaninsh merged commit 172e3f1 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
3  lib/Form/Basic.php
@@ -430,7 +430,4 @@ function setFieldError($field, $name){
430 430 }
431 431 $this->errors[$field] = $existing . $name;
432 432 }
433   - /**
434   - * Makes field's value set to null if empty value has been specified
435   - */
436 433 }
17 lib/Form/Field/CheckboxList.php
@@ -33,11 +33,6 @@ class Form_Field_CheckboxList extends Form_Field_ValueList
33 33 */
34 34 var $columns=2;
35 35
36   - function validate()
37   - {
38   - return true;
39   - }
40   -
41 36 function getInput($attr=array())
42 37 {
43 38 $output='<table class="atk-checkboxlist" border="0" id="'.$this->name.'"><tbody>';
@@ -73,16 +68,4 @@ function getInput($attr=array())
73 68
74 69 return $output;
75 70 }
76   -
77   - function loadPOST()
78   - {
79   - $data=$_POST[$this->name];
80   - $data = is_array($data) ? join(',',$data) : '';
81   -
82   - if (get_magic_quotes_gpc()){
83   - $this->set(stripslashes($data));
84   - } else {
85   - $this->set($data);
86   - }
87   - }
88 71 }
19 lib/Form/Field/DropDown.php
@@ -16,24 +16,11 @@
16 16 =====================================================ATK4=*/
17 17 class Form_Field_DropDown extends Form_Field_ValueList {
18 18
19   - function validate(){
20   - if(!$this->value)return parent::validate();
21   - $this->getValueList(); //otherwise not preloaded?
22   - if(!isset($this->value_list[$this->value])){
23   - /*
24   - if($this->api->isAjaxOutput()){
25   - $this->ajax()->displayAlert($this->short_name.": This is not one of the offered values")
26   - ->execute();
27   - }
28   - */
29   - $this->form->errors[$this->short_name]="This is not one of the offered values";
30   - }
31   - return parent::validate();
32   - }
33 19 function getInput($attr=array()){
  20 + $multi = isset($this->attr['multiple']);
34 21 $output=$this->getTag('select',array_merge(array(
35   - 'name'=>$this->name,
36   - 'data-shortname'=>$this->short_name,
  22 + 'name'=>$this->name . ($multi?'[]':''),
  23 + 'data-shortname'=>$this->short_name,
37 24 'id'=>$this->name,
38 25 ),
39 26 $attr,
7 lib/Form/Field/Radio.php
@@ -17,12 +17,7 @@
17 17 class Form_Field_Radio extends Form_Field_ValueList {
18 18 function validate(){
19 19 if(!isset($this->value_list[$this->value]) && (!$this->value && $this->empty_value)){
20   - /*
21   - if($this->api->isAjaxOutput()){
22   - echo $this->ajax()->displayAlert($this->short_name.":"."This is not one of offered values")->execute();
23   - }
24   - */
25   - $this->displayFieldError("This is not one of offered values");
  20 + $this->displayFieldError("Value ".$this->value." is not one of offered values");
26 21 }
27 22 return parent::validate();
28 23 }
31 lib/Form/Field/ValueList.php
@@ -28,9 +28,21 @@ class Form_Field_ValueList extends Form_Field {
28 28 public $empty_text=null;
29 29 protected $empty_value=''; // don't change this value
30 30
  31 + function validate(){
  32 + if(!$this->value)return parent::validate();
  33 + $this->getValueList(); //otherwise not preloaded?
  34 +
  35 + $values = explode(',',$this->value);
  36 + foreach($values as $v){
  37 + if(!isset($this->value_list[$v])){
  38 + $this->displayFieldError("Value $v is not one of the offered values");
  39 + return parent::validate();
  40 + }
  41 + }
  42 + return parent::validate();
  43 + }
31 44 function setModel($m){
32 45 $ret=parent::setModel($m);
33   -
34 46 $this->setValueList(array('foo','bar'));
35 47 return $ret;
36 48 }
@@ -41,7 +53,6 @@ function setEmptyText($empty_text){
41 53 }
42 54
43 55 function getValueList(){
44   -
45 56 if($this->model){
46 57 $title=$this->model->getTitleField();
47 58 $id=$this->model->id_field;
@@ -66,13 +77,15 @@ function setValueList($list){
66 77 return $this;
67 78 }
68 79 function loadPOST(){
69   - $data=$_POST[$this->name];
70   - if(is_array($data))$data=join(',',$data);
71   - $gpc = get_magic_quotes_gpc();
72   - if ($gpc){
73   - if(isset($_POST[$this->name]))$this->set(stripslashes($data));
74   - } else {
75   - if(isset($_POST[$this->name]))$this->set($data);
  80 + if(isset($_POST[$this->name])){
  81 + $data=$_POST[$this->name];
  82 + if(is_array($data))$data=implode(',',$data);
  83 +
  84 + if (get_magic_quotes_gpc()){
  85 + $this->set(stripslashes($data));
  86 + } else {
  87 + $this->set($data);
  88 + }
76 89 }
77 90 }
78 91 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.