@@ -16,264 +16,264 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Field extends FormerObject implements FieldInterface |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The IoC Container |
|
| 21 | - * |
|
| 22 | - * @var Container |
|
| 23 | - */ |
|
| 24 | - protected $app; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The Form instance |
|
| 28 | - * |
|
| 29 | - * @var Former\Form |
|
| 30 | - */ |
|
| 31 | - protected $form; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A label for the field (if not using Bootstrap) |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $label; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The field's group |
|
| 42 | - * |
|
| 43 | - * @var Group |
|
| 44 | - */ |
|
| 45 | - protected $group; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The field's default element |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $element = 'input'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Whether the Field is self-closing or not |
|
| 56 | - * |
|
| 57 | - * @var boolean |
|
| 58 | - */ |
|
| 59 | - protected $isSelfClosing = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The field's bind destination |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $bind; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get the current framework instance |
|
| 70 | - * |
|
| 71 | - * @return Framework |
|
| 72 | - */ |
|
| 73 | - protected function currentFramework() |
|
| 74 | - { |
|
| 75 | - if ($this->app->bound('former.form.framework')) { |
|
| 76 | - return $this->app['former.form.framework']; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $this->app['former.framework']; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - ///////////////////////////// INTERFACE //////////////////////////// |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Set up a Field instance |
|
| 88 | - * |
|
| 89 | - * @param string $type A field type |
|
| 90 | - */ |
|
| 91 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 92 | - { |
|
| 93 | - // Set base parameters |
|
| 94 | - $this->app = $app; |
|
| 95 | - $this->type = $type; |
|
| 96 | - $this->value = $value; |
|
| 97 | - $this->setAttributes($attributes); |
|
| 98 | - $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 99 | - |
|
| 100 | - // Compute and translate label |
|
| 101 | - $this->automaticLabels($name, $label); |
|
| 102 | - |
|
| 103 | - // Repopulate field |
|
| 104 | - if ($type != 'password' && $name !== '_token') { |
|
| 105 | - $this->value = $this->repopulate(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Apply Live validation rules |
|
| 109 | - if ($this->app['former']->getOption('live_validation')) { |
|
| 110 | - $rules = new LiveValidation($this); |
|
| 111 | - $rules->apply($this->getRules()); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Bind the Group class |
|
| 115 | - $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 116 | - $groupClass = Former::FORMSPACE.$groupClass; |
|
| 117 | - |
|
| 118 | - $this->group = new $groupClass($this->app, $this->label); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Redirect calls to the group if necessary |
|
| 123 | - * |
|
| 124 | - * @param string $method |
|
| 125 | - */ |
|
| 126 | - public function __call($method, $parameters) |
|
| 127 | - { |
|
| 128 | - // Translate attributes |
|
| 129 | - $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 130 | - if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 131 | - $parameters[0] = Helpers::translate($parameters[0]); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Redirect calls to the Control Group |
|
| 135 | - if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) { |
|
| 136 | - $method = str_replace('onGroup', '', $method); |
|
| 137 | - $method = lcfirst($method); |
|
| 138 | - |
|
| 139 | - call_user_func_array(array($this->group, $method), $parameters); |
|
| 140 | - |
|
| 141 | - return $this; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return parent::__call($method, $parameters); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Prints out the field, wrapped in its group |
|
| 149 | - * |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function wrapAndRender() |
|
| 153 | - { |
|
| 154 | - // Dry syntax (hidden fields, plain fields) |
|
| 155 | - if ($this->isUnwrappable()) { |
|
| 156 | - $html = $this->render(); |
|
| 157 | - // Control group syntax |
|
| 158 | - } elseif (Form::hasInstanceOpened()) { |
|
| 159 | - $html = $this->group->wrapField($this); |
|
| 160 | - // Classic syntax |
|
| 161 | - } else { |
|
| 162 | - $html = $this->currentFramework()->createLabelOf($this); |
|
| 163 | - $html .= $this->render(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return $html; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Prints out the field |
|
| 171 | - * |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public function __toString() |
|
| 175 | - { |
|
| 176 | - return $this->wrapAndRender(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - //////////////////////////////////////////////////////////////////// |
|
| 180 | - ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 181 | - //////////////////////////////////////////////////////////////////// |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Whether the current field is required or not |
|
| 185 | - * |
|
| 186 | - * @return boolean |
|
| 187 | - */ |
|
| 188 | - public function isRequired() |
|
| 189 | - { |
|
| 190 | - return isset($this->attributes['required']); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Set required live validation attribute |
|
| 195 | - * |
|
| 196 | - * @param boolean $isRequired |
|
| 197 | - * @return $this |
|
| 198 | - */ |
|
| 199 | - public function required($isRequired=true) |
|
| 200 | - { |
|
| 201 | - if ($isRequired) { |
|
| 202 | - $this->attributes['required'] = true; |
|
| 203 | - } else { |
|
| 204 | - unset($this->attributes['required']); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $this; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Check if a field is unwrappable (no label) |
|
| 212 | - * |
|
| 213 | - * @return boolean |
|
| 214 | - */ |
|
| 215 | - public function isUnwrappable() |
|
| 216 | - { |
|
| 217 | - return |
|
| 218 | - ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 219 | - ($this->form and $this->isOfType('inline')) or |
|
| 220 | - $this->isButton() or |
|
| 221 | - $this->isOfType('hidden') or |
|
| 222 | - \Former\Form\Group::$opened or |
|
| 223 | - $this->group and $this->group->isRaw(); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Check if field is a checkbox or a radio |
|
| 228 | - * |
|
| 229 | - * @return boolean |
|
| 230 | - */ |
|
| 231 | - public function isCheckable() |
|
| 232 | - { |
|
| 233 | - return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios'); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Check if the field is a button |
|
| 238 | - * |
|
| 239 | - * @return boolean |
|
| 240 | - */ |
|
| 241 | - public function isButton() |
|
| 242 | - { |
|
| 243 | - return false; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the rules applied to the current field |
|
| 248 | - * |
|
| 249 | - * @return array An array of rules |
|
| 250 | - */ |
|
| 251 | - public function getRules() |
|
| 252 | - { |
|
| 253 | - return $this->app['former']->getRules($this->name); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - //////////////////////////////////////////////////////////////////// |
|
| 257 | - //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 258 | - //////////////////////////////////////////////////////////////////// |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Apply a Live Validation rule by chaining |
|
| 262 | - * |
|
| 263 | - * @param string $rule The rule |
|
| 264 | - */ |
|
| 265 | - public function rule($rule) |
|
| 266 | - { |
|
| 267 | - $parameters = func_get_args(); |
|
| 268 | - array_shift($parameters); |
|
| 269 | - |
|
| 270 | - $live = new LiveValidation($this); |
|
| 271 | - $live->apply(array( |
|
| 272 | - $rule => $parameters, |
|
| 273 | - )); |
|
| 274 | - |
|
| 275 | - return $this; |
|
| 276 | - } |
|
| 19 | + /** |
|
| 20 | + * The IoC Container |
|
| 21 | + * |
|
| 22 | + * @var Container |
|
| 23 | + */ |
|
| 24 | + protected $app; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The Form instance |
|
| 28 | + * |
|
| 29 | + * @var Former\Form |
|
| 30 | + */ |
|
| 31 | + protected $form; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A label for the field (if not using Bootstrap) |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $label; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The field's group |
|
| 42 | + * |
|
| 43 | + * @var Group |
|
| 44 | + */ |
|
| 45 | + protected $group; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The field's default element |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $element = 'input'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Whether the Field is self-closing or not |
|
| 56 | + * |
|
| 57 | + * @var boolean |
|
| 58 | + */ |
|
| 59 | + protected $isSelfClosing = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The field's bind destination |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $bind; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get the current framework instance |
|
| 70 | + * |
|
| 71 | + * @return Framework |
|
| 72 | + */ |
|
| 73 | + protected function currentFramework() |
|
| 74 | + { |
|
| 75 | + if ($this->app->bound('former.form.framework')) { |
|
| 76 | + return $this->app['former.form.framework']; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $this->app['former.framework']; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + ///////////////////////////// INTERFACE //////////////////////////// |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Set up a Field instance |
|
| 88 | + * |
|
| 89 | + * @param string $type A field type |
|
| 90 | + */ |
|
| 91 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 92 | + { |
|
| 93 | + // Set base parameters |
|
| 94 | + $this->app = $app; |
|
| 95 | + $this->type = $type; |
|
| 96 | + $this->value = $value; |
|
| 97 | + $this->setAttributes($attributes); |
|
| 98 | + $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 99 | + |
|
| 100 | + // Compute and translate label |
|
| 101 | + $this->automaticLabels($name, $label); |
|
| 102 | + |
|
| 103 | + // Repopulate field |
|
| 104 | + if ($type != 'password' && $name !== '_token') { |
|
| 105 | + $this->value = $this->repopulate(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Apply Live validation rules |
|
| 109 | + if ($this->app['former']->getOption('live_validation')) { |
|
| 110 | + $rules = new LiveValidation($this); |
|
| 111 | + $rules->apply($this->getRules()); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Bind the Group class |
|
| 115 | + $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 116 | + $groupClass = Former::FORMSPACE.$groupClass; |
|
| 117 | + |
|
| 118 | + $this->group = new $groupClass($this->app, $this->label); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Redirect calls to the group if necessary |
|
| 123 | + * |
|
| 124 | + * @param string $method |
|
| 125 | + */ |
|
| 126 | + public function __call($method, $parameters) |
|
| 127 | + { |
|
| 128 | + // Translate attributes |
|
| 129 | + $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 130 | + if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 131 | + $parameters[0] = Helpers::translate($parameters[0]); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Redirect calls to the Control Group |
|
| 135 | + if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) { |
|
| 136 | + $method = str_replace('onGroup', '', $method); |
|
| 137 | + $method = lcfirst($method); |
|
| 138 | + |
|
| 139 | + call_user_func_array(array($this->group, $method), $parameters); |
|
| 140 | + |
|
| 141 | + return $this; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return parent::__call($method, $parameters); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Prints out the field, wrapped in its group |
|
| 149 | + * |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function wrapAndRender() |
|
| 153 | + { |
|
| 154 | + // Dry syntax (hidden fields, plain fields) |
|
| 155 | + if ($this->isUnwrappable()) { |
|
| 156 | + $html = $this->render(); |
|
| 157 | + // Control group syntax |
|
| 158 | + } elseif (Form::hasInstanceOpened()) { |
|
| 159 | + $html = $this->group->wrapField($this); |
|
| 160 | + // Classic syntax |
|
| 161 | + } else { |
|
| 162 | + $html = $this->currentFramework()->createLabelOf($this); |
|
| 163 | + $html .= $this->render(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return $html; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Prints out the field |
|
| 171 | + * |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public function __toString() |
|
| 175 | + { |
|
| 176 | + return $this->wrapAndRender(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + //////////////////////////////////////////////////////////////////// |
|
| 180 | + ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 181 | + //////////////////////////////////////////////////////////////////// |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Whether the current field is required or not |
|
| 185 | + * |
|
| 186 | + * @return boolean |
|
| 187 | + */ |
|
| 188 | + public function isRequired() |
|
| 189 | + { |
|
| 190 | + return isset($this->attributes['required']); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Set required live validation attribute |
|
| 195 | + * |
|
| 196 | + * @param boolean $isRequired |
|
| 197 | + * @return $this |
|
| 198 | + */ |
|
| 199 | + public function required($isRequired=true) |
|
| 200 | + { |
|
| 201 | + if ($isRequired) { |
|
| 202 | + $this->attributes['required'] = true; |
|
| 203 | + } else { |
|
| 204 | + unset($this->attributes['required']); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $this; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Check if a field is unwrappable (no label) |
|
| 212 | + * |
|
| 213 | + * @return boolean |
|
| 214 | + */ |
|
| 215 | + public function isUnwrappable() |
|
| 216 | + { |
|
| 217 | + return |
|
| 218 | + ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 219 | + ($this->form and $this->isOfType('inline')) or |
|
| 220 | + $this->isButton() or |
|
| 221 | + $this->isOfType('hidden') or |
|
| 222 | + \Former\Form\Group::$opened or |
|
| 223 | + $this->group and $this->group->isRaw(); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Check if field is a checkbox or a radio |
|
| 228 | + * |
|
| 229 | + * @return boolean |
|
| 230 | + */ |
|
| 231 | + public function isCheckable() |
|
| 232 | + { |
|
| 233 | + return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios'); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Check if the field is a button |
|
| 238 | + * |
|
| 239 | + * @return boolean |
|
| 240 | + */ |
|
| 241 | + public function isButton() |
|
| 242 | + { |
|
| 243 | + return false; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the rules applied to the current field |
|
| 248 | + * |
|
| 249 | + * @return array An array of rules |
|
| 250 | + */ |
|
| 251 | + public function getRules() |
|
| 252 | + { |
|
| 253 | + return $this->app['former']->getRules($this->name); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + //////////////////////////////////////////////////////////////////// |
|
| 257 | + //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 258 | + //////////////////////////////////////////////////////////////////// |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Apply a Live Validation rule by chaining |
|
| 262 | + * |
|
| 263 | + * @param string $rule The rule |
|
| 264 | + */ |
|
| 265 | + public function rule($rule) |
|
| 266 | + { |
|
| 267 | + $parameters = func_get_args(); |
|
| 268 | + array_shift($parameters); |
|
| 269 | + |
|
| 270 | + $live = new LiveValidation($this); |
|
| 271 | + $live->apply(array( |
|
| 272 | + $rule => $parameters, |
|
| 273 | + )); |
|
| 274 | + |
|
| 275 | + return $this; |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | 278 | /** |
| 279 | 279 | * Apply multiple rules passed as a string. |
@@ -295,9 +295,9 @@ discard block |
||
| 295 | 295 | $rulename = substr($rule, 0, $colon) ; |
| 296 | 296 | |
| 297 | 297 | /** |
| 298 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 299 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 300 | - */ |
|
| 298 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 299 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 300 | + */ |
|
| 301 | 301 | if ($rulename !== 'regex') { |
| 302 | 302 | $parameters = str_getcsv(substr($rule, $colon + 1)); |
| 303 | 303 | } else { |
@@ -319,149 +319,149 @@ discard block |
||
| 319 | 319 | return $this; |
| 320 | 320 | } |
| 321 | 321 | |
| 322 | - /** |
|
| 323 | - * Adds a label to the group/field |
|
| 324 | - * |
|
| 325 | - * @param string $text A label |
|
| 326 | - * @param array $attributes The label's attributes |
|
| 327 | - * |
|
| 328 | - * @return Field A field |
|
| 329 | - */ |
|
| 330 | - public function label($text, $attributes = array()) |
|
| 331 | - { |
|
| 332 | - // Create the Label element |
|
| 333 | - $for = $this->id ?: $this->name; |
|
| 334 | - $label = $this->app['former']->label($text, $for, $attributes); |
|
| 335 | - |
|
| 336 | - // Set label |
|
| 337 | - $this->label = $label; |
|
| 338 | - if ($this->group) { |
|
| 339 | - $this->group->setLabel($label); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $this; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Set the Field value no matter what |
|
| 347 | - * |
|
| 348 | - * @param string $value A new value |
|
| 349 | - */ |
|
| 350 | - public function forceValue($value) |
|
| 351 | - { |
|
| 352 | - $this->value = $value; |
|
| 353 | - |
|
| 354 | - return $this; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 359 | - * |
|
| 360 | - * @param string $value A new value |
|
| 361 | - */ |
|
| 362 | - public function value($value) |
|
| 363 | - { |
|
| 364 | - // Check if we already have a value stored for this field or in POST data |
|
| 365 | - $already = $this->repopulate(); |
|
| 366 | - |
|
| 367 | - if (!$already) { |
|
| 368 | - $this->value = $value; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Change the field's name |
|
| 376 | - * |
|
| 377 | - * @param string $name The new name |
|
| 378 | - */ |
|
| 379 | - public function name($name) |
|
| 380 | - { |
|
| 381 | - $this->name = $name; |
|
| 382 | - |
|
| 383 | - // Also relink the label to the new name |
|
| 384 | - $this->label($name); |
|
| 385 | - |
|
| 386 | - return $this; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Get the field's labels |
|
| 391 | - * |
|
| 392 | - * @return string |
|
| 393 | - */ |
|
| 394 | - public function getLabel() |
|
| 395 | - { |
|
| 396 | - return $this->label; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Change the field's bind destination |
|
| 401 | - * |
|
| 402 | - * @param $destination |
|
| 403 | - */ |
|
| 404 | - public function bind($destination) { |
|
| 405 | - $this->bind = $destination; |
|
| 406 | - if ($this->type != 'password') { |
|
| 407 | - $this->value = $this->repopulate(); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - return $this; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - //////////////////////////////////////////////////////////////////// |
|
| 414 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 415 | - //////////////////////////////////////////////////////////////////// |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Use values stored in Former to populate the current field |
|
| 419 | - */ |
|
| 420 | - private function repopulate($fallback = null) |
|
| 421 | - { |
|
| 422 | - // Get values from POST, populated, and manually set value |
|
| 423 | - $post = $this->app['former']->getPost($this->name); |
|
| 424 | - $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 425 | - $populate = $populator->get($this->bind ?: $this->name); |
|
| 426 | - |
|
| 427 | - // Assign a priority to each |
|
| 428 | - if (!is_null($post)) { |
|
| 429 | - return $post; |
|
| 430 | - } |
|
| 431 | - if (!is_null($populate)) { |
|
| 432 | - return $populate; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - return $fallback ?: $this->value; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Ponders a label and a field name, and tries to get the best out of it |
|
| 440 | - * |
|
| 441 | - * @param string $label A label |
|
| 442 | - * @param string $name A field name |
|
| 443 | - * |
|
| 444 | - * @return false|null A label and a field name |
|
| 445 | - */ |
|
| 446 | - private function automaticLabels($name, $label) |
|
| 447 | - { |
|
| 448 | - // Disabled automatic labels |
|
| 449 | - if (!$this->app['former']->getOption('automatic_label')) { |
|
| 450 | - $this->name = $name; |
|
| 451 | - $this->label($label); |
|
| 452 | - |
|
| 453 | - return false; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // Check for the two possibilities |
|
| 457 | - if ($label and is_null($name)) { |
|
| 458 | - $name = Str::slug($label); |
|
| 459 | - } elseif (is_null($label) and $name) { |
|
| 460 | - $label = preg_replace('/\[\]$/', '', $name); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - // Save values |
|
| 464 | - $this->name = $name; |
|
| 465 | - $this->label($label); |
|
| 466 | - } |
|
| 322 | + /** |
|
| 323 | + * Adds a label to the group/field |
|
| 324 | + * |
|
| 325 | + * @param string $text A label |
|
| 326 | + * @param array $attributes The label's attributes |
|
| 327 | + * |
|
| 328 | + * @return Field A field |
|
| 329 | + */ |
|
| 330 | + public function label($text, $attributes = array()) |
|
| 331 | + { |
|
| 332 | + // Create the Label element |
|
| 333 | + $for = $this->id ?: $this->name; |
|
| 334 | + $label = $this->app['former']->label($text, $for, $attributes); |
|
| 335 | + |
|
| 336 | + // Set label |
|
| 337 | + $this->label = $label; |
|
| 338 | + if ($this->group) { |
|
| 339 | + $this->group->setLabel($label); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $this; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Set the Field value no matter what |
|
| 347 | + * |
|
| 348 | + * @param string $value A new value |
|
| 349 | + */ |
|
| 350 | + public function forceValue($value) |
|
| 351 | + { |
|
| 352 | + $this->value = $value; |
|
| 353 | + |
|
| 354 | + return $this; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 359 | + * |
|
| 360 | + * @param string $value A new value |
|
| 361 | + */ |
|
| 362 | + public function value($value) |
|
| 363 | + { |
|
| 364 | + // Check if we already have a value stored for this field or in POST data |
|
| 365 | + $already = $this->repopulate(); |
|
| 366 | + |
|
| 367 | + if (!$already) { |
|
| 368 | + $this->value = $value; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Change the field's name |
|
| 376 | + * |
|
| 377 | + * @param string $name The new name |
|
| 378 | + */ |
|
| 379 | + public function name($name) |
|
| 380 | + { |
|
| 381 | + $this->name = $name; |
|
| 382 | + |
|
| 383 | + // Also relink the label to the new name |
|
| 384 | + $this->label($name); |
|
| 385 | + |
|
| 386 | + return $this; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Get the field's labels |
|
| 391 | + * |
|
| 392 | + * @return string |
|
| 393 | + */ |
|
| 394 | + public function getLabel() |
|
| 395 | + { |
|
| 396 | + return $this->label; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Change the field's bind destination |
|
| 401 | + * |
|
| 402 | + * @param $destination |
|
| 403 | + */ |
|
| 404 | + public function bind($destination) { |
|
| 405 | + $this->bind = $destination; |
|
| 406 | + if ($this->type != 'password') { |
|
| 407 | + $this->value = $this->repopulate(); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + return $this; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + //////////////////////////////////////////////////////////////////// |
|
| 414 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 415 | + //////////////////////////////////////////////////////////////////// |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Use values stored in Former to populate the current field |
|
| 419 | + */ |
|
| 420 | + private function repopulate($fallback = null) |
|
| 421 | + { |
|
| 422 | + // Get values from POST, populated, and manually set value |
|
| 423 | + $post = $this->app['former']->getPost($this->name); |
|
| 424 | + $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 425 | + $populate = $populator->get($this->bind ?: $this->name); |
|
| 426 | + |
|
| 427 | + // Assign a priority to each |
|
| 428 | + if (!is_null($post)) { |
|
| 429 | + return $post; |
|
| 430 | + } |
|
| 431 | + if (!is_null($populate)) { |
|
| 432 | + return $populate; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + return $fallback ?: $this->value; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Ponders a label and a field name, and tries to get the best out of it |
|
| 440 | + * |
|
| 441 | + * @param string $label A label |
|
| 442 | + * @param string $name A field name |
|
| 443 | + * |
|
| 444 | + * @return false|null A label and a field name |
|
| 445 | + */ |
|
| 446 | + private function automaticLabels($name, $label) |
|
| 447 | + { |
|
| 448 | + // Disabled automatic labels |
|
| 449 | + if (!$this->app['former']->getOption('automatic_label')) { |
|
| 450 | + $this->name = $name; |
|
| 451 | + $this->label($label); |
|
| 452 | + |
|
| 453 | + return false; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // Check for the two possibilities |
|
| 457 | + if ($label and is_null($name)) { |
|
| 458 | + $name = Str::slug($label); |
|
| 459 | + } elseif (is_null($label) and $name) { |
|
| 460 | + $label = preg_replace('/\[\]$/', '', $name); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + // Save values |
|
| 464 | + $this->name = $name; |
|
| 465 | + $this->label($label); |
|
| 466 | + } |
|
| 467 | 467 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * @param boolean $isRequired |
| 197 | 197 | * @return $this |
| 198 | 198 | */ |
| 199 | - public function required($isRequired=true) |
|
| 199 | + public function required($isRequired = true) |
|
| 200 | 200 | { |
| 201 | 201 | if ($isRequired) { |
| 202 | 202 | $this->attributes['required'] = true; |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | |
| 293 | 293 | // If we have a rule with a value |
| 294 | 294 | if (($colon = strpos($rule, ':')) !== false) { |
| 295 | - $rulename = substr($rule, 0, $colon) ; |
|
| 295 | + $rulename = substr($rule, 0, $colon); |
|
| 296 | 296 | |
| 297 | 297 | /** |
| 298 | 298 | * Regular expressions may contain commas and should not be divided by str_getcsv. |