Changeset 1438
- Timestamp:
- 12/06/07 23:17:17 (7 months ago)
- Files:
-
- trunk/modules/forge/controllers/forge.php (deleted)
- trunk/modules/forge/controllers/forge_demo.php (added)
- trunk/modules/forge/helpers (added)
- trunk/modules/forge/helpers/locale_US.php (added)
- trunk/modules/forge/libraries/Forge.php (modified) (3 diffs)
- trunk/modules/forge/libraries/Form_Checklist.php (modified) (7 diffs)
- trunk/modules/forge/libraries/Form_Dropdown.php (modified) (4 diffs)
- trunk/modules/forge/libraries/Form_Input.php (modified) (8 diffs)
- trunk/modules/forge/libraries/Form_Submit.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/modules/forge/libraries/Forge.php
r1435 r1438 23 23 public function __get($key) 24 24 { 25 if ( $key == 'validated')25 if (isset($this->inputs[$key])) 26 26 { 27 foreach($this->inputs as $input) 28 { 29 if ( ! $input->is_valid) 30 return FALSE; 31 } 32 return TRUE; 33 } 34 elseif (isset($this->inputs[$key])) 35 { 36 return $this->inputs[$key]->value; 27 return $this->inputs[$key]; 37 28 } 38 29 } 39 30 40 public function add($input)31 public function __call($method, $args) 41 32 { 33 // Class name 34 $input = 'Form_'.ucfirst($method); 35 36 // Create the input 37 $input = new $input(empty($args) ? NULL : current($args)); 38 42 39 if ( ! ($input instanceof Form_Input)) 43 40 throw new Kohana_Exception('forge.invalid_input'); … … 45 42 if ($name = $input->name) 46 43 { 44 // Assign by name 47 45 $this->inputs[$name] = $input; 48 46 } … … 52 50 } 53 51 54 return $ this;52 return $input; 55 53 } 56 54 57 public function data() 55 public function validate() 56 { 57 $status = TRUE; 58 foreach($this->inputs as $input) 59 { 60 if ($input->validate() == FALSE) 61 { 62 $status = FALSE; 63 } 64 } 65 66 return $status; 67 } 68 69 public function as_array() 58 70 { 59 71 if (empty($_POST)) trunk/modules/forge/libraries/Form_Checklist.php
r1435 r1438 12 12 protected $protect = array('name', 'type'); 13 13 14 // Name of the list 15 protected $list_name = ''; 16 17 // Associative array of options: value => checked 18 protected $list_options = array(); 19 20 // List input data 21 protected $list_data = array(); 22 23 public function __construct($name, $data) 14 public function __construct($name) 24 15 { 25 $this->list_name = $this->data['name'] = $name; 26 27 $this->list_options = arr::remove('options', $data); 28 29 foreach($data as $key => $val) 30 { 31 $this->$key = $val; 32 } 16 $this->data['name'] = $name; 33 17 } 34 18 … … 37 21 if ($key == 'value') 38 22 { 39 return isset($_POST[$this->list_name]) ? $_POST[$this->list_name] : array(); 23 // Return the currently checked values 24 return array_keys($this->data['options'], TRUE); 40 25 } 41 26 … … 45 30 public function html() 46 31 { 47 // Load the submitted value48 $this->load_value();49 50 32 // Import base data 51 33 $base_data = $this->data; 34 35 // Make it an array 36 $base_data['name'] .= '[]'; 52 37 53 38 // Newline … … 55 40 56 41 $checklist = '<ul class="'.arr::remove('class', $base_data).'">'.$nl; 57 foreach( $this->list_optionsas $val => $checked)42 foreach(arr::remove('options', $base_data) as $val => $checked) 58 43 { 59 44 // New set of input data … … 61 46 62 47 // Set the name, value, and checked status 63 $data['name'] = $this->list_name.'[]';64 48 $data['value'] = $val; 65 49 $data['checked'] = $checked; … … 69 53 $checklist .= '</ul>'; 70 54 71 return $checklist ;55 return $checklist.$this->error_message(); 72 56 } 73 57 … … 77 61 return; 78 62 79 foreach($this-> list_optionsas $val => $checked)63 foreach($this->data['options'] as $val => $checked) 80 64 { 81 if (empty($_POST[$this-> list_name]))65 if (empty($_POST[$this->data['name']])) 82 66 { 83 $this-> list_options[$val] = FALSE;67 $this->data['options'][$val] = FALSE; 84 68 } 85 69 else 86 70 { 87 $this-> list_options[$val] = in_array($val, $_POST[$this->list_name]);71 $this->data['options'][$val] = in_array($val, $_POST[$this->data['name']]); 88 72 } 89 73 } trunk/modules/forge/libraries/Form_Dropdown.php
r1436 r1438 6 6 ( 7 7 'name' => '', 8 'type' => 'dropdown',9 8 'class' => 'dropdown', 10 9 ); 11 10 12 protected $protect = array('name', 'type'); 13 14 // Name of the list 15 protected $list_name = ''; 16 17 // Associative array of options: value => checked 18 protected $list_options = array(); 19 20 // List input data 21 protected $list_data = array(); 22 23 public function __construct($name, $data) 24 { 25 $this->list_name = $this->data['name'] = $name; 26 27 $this->list_options = arr::remove('options', $data); 28 29 foreach($data as $key => $val) 30 { 31 $this->$key = $val; 32 } 33 } 11 protected $protect = array('type'); 34 12 35 13 public function __get($key) … … 37 15 if ($key == 'value') 38 16 { 39 return isset($_POST[$this->list_name]) ? $_POST[$this->list_name] : array();17 return $this->default; 40 18 } 41 19 … … 45 23 public function html() 46 24 { 47 // Load the submitted value48 $ this->load_value();25 // Import base data 26 $base_data = $this->data; 49 27 50 // Import base data and options51 $ base_data = $this->data;52 $ options = $this->list_options;28 // Get the options and default selection 29 $options = arr::remove('options', $base_data); 30 $default = arr::remove('default', $base_data); 53 31 54 return form::dropdown(arr::remove('name', $base_data), $options, arr::remove('default', $base_data)); 32 // Add an empty option to the beginning of the options 33 arr::unshift_assoc($options, '', '--'); 34 35 return form::dropdown($base_data, $options, $default).$this->error_message(); 55 36 } 56 37 … … 60 41 return; 61 42 62 foreach($this->list_options as $val => $checked)43 if ($default = self::$input->post($this->name)) 63 44 { 64 if (empty($_POST[$this->list_name])) 65 { 66 $this->list_options[$val] = FALSE; 67 } 68 else 69 { 70 $this->list_options[$val] = in_array($val, $_POST[$this->list_name]); 71 } 45 $this->data['default'] = $default; 72 46 } 73 47 } 48 74 49 } // End Form Dropdown trunk/modules/forge/libraries/Form_Input.php
r1435 r1438 3 3 class Form_Input_Core { 4 4 5 // Input data 5 // Input instance 6 protected static $input; 7 8 // Element data 6 9 protected $data = array 7 10 ( … … 14 17 15 18 // Validtion rules 16 protected $rules = '';19 protected $rules = array(); 17 20 18 21 // Validation check … … 22 25 protected $errors = array(); 23 26 24 public function __construct($name, $data = array()) 25 { 26 if ( ! is_array($data)) 27 { 28 // Set the data to the value 29 $data = array('value' => $data); 30 } 31 32 if ( ! empty($name)) 33 { 34 // Set the name 35 $data['name'] = $name; 36 } 37 38 if ($rules = arr::remove('rules', $data)) 39 { 40 // Set the rules 41 $this->rules = $rules; 42 } 43 44 foreach($data as $key => $val) 45 { 46 // Load the data using __set, for protection 47 $this->$key = $val; 48 } 49 50 // Load the value 51 $this->load_value(); 52 } 53 54 public function __set($key, $val) 55 { 56 if ( ! in_array($key, $this->protect)) 57 { 58 $this->data[$key] = $val; 59 } 27 public function __construct($name) 28 { 29 if (self::$input === NULL) 30 { 31 self::$input = new Input; 32 } 33 34 $this->data['name'] = $name; 35 } 36 37 public function __call($method, $args) 38 { 39 if ($method == 'rules') 40 { 41 $this->add_rules(explode('|', $args[0])); 42 } 43 elseif ($method == 'name') 44 { 45 // Yup, do nothing. The name should stay static once it is set. 46 } 47 else 48 { 49 $this->data[$method] = $args[0]; 50 } 51 52 return $this; 60 53 } 61 54 62 55 public function __get($key) 63 56 { 64 if ($key === 'is_valid') 65 { 66 // Make sure validation runs 67 is_null($this->is_valid) and $this->validate(); 68 69 return $this->is_valid; 70 } 71 elseif (isset($this->data[$key])) 57 if (isset($this->data[$key])) 72 58 { 73 59 return $this->data[$key]; … … 75 61 } 76 62 77 public function label( )78 { 79 if ($ this->label != '')63 public function label($val = NULL) 64 { 65 if ($val === NULL) 80 66 { 81 67 return form::label($this->name, $this->label); 68 } 69 else 70 { 71 $this->label = ($val === TRUE) ? ucfirst($this->name) : $val; 72 return $this; 82 73 } 83 74 } … … 92 83 93 84 // Remove the label 94 unset($data['label'] );85 unset($data['label'], $data['options'], $data['default']); 95 86 96 87 return form::input($data).$this->error_message(); 88 } 89 90 protected function add_rules( array $rules) 91 { 92 foreach($rules as $rule) 93 { 94 if ( ! in_array($rule, $this->rules)) 95 { 96 $this->rules[] = $rule; 97 } 98 } 97 99 } 98 100 … … 113 115 protected function load_value() 114 116 { 115 if ($name = $this->name) 116 { 117 if (isset($_POST[$name])) 118 { 119 $this->data['value'] = $_POST[$name]; 120 } 121 } 122 } 123 124 protected function validate() 117 if ($value = self::$input->post($this->name)) 118 { 119 $this->data['value'] = $value; 120 } 121 } 122 123 public function validate() 125 124 { 126 125 // Validation has already run … … 141 140 if ( ! empty($this->rules)) 142 141 { 143 foreach( explode('|', $this->rules)as $rule)142 foreach($this->rules as $rule) 144 143 { 145 144 if (($offset = strpos($rule, '[')) !== FALSE) … … 191 190 protected function rule_required() 192 191 { 193 if ( empty($this->data['value']))192 if ($this->value == NULL) 194 193 { 195 194 $this->errors[] = 'This field is required.'; trunk/modules/forge/libraries/Form_Submit.php
r1435 r1438 24 24 } 25 25 26 p rotectedfunction validate()26 public function validate() 27 27 { 28 28 // Submit buttons do not need to be validated
