@@ -14,490 +14,490 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Former |
| 16 | 16 | { |
| 17 | - // Instances |
|
| 18 | - //////////////////////////////////////////////////////////////////// |
|
| 19 | - |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The current environment |
|
| 23 | - * |
|
| 24 | - * @var \Illuminate\Container\Container |
|
| 25 | - */ |
|
| 26 | - protected $app; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Method Dispatcher |
|
| 30 | - * |
|
| 31 | - * @var MethodDispatcher |
|
| 32 | - */ |
|
| 33 | - protected $dispatch; |
|
| 34 | - |
|
| 35 | - // Informations |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The form's errors |
|
| 40 | - * |
|
| 41 | - * @var Message |
|
| 42 | - */ |
|
| 43 | - protected $errors; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * An array of rules to use |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - */ |
|
| 50 | - protected $rules = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * An array of field macros |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $macros = array(); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The labels created so far |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - public $labels = array(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The IDs created so far |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - public $ids = array(); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * A lookup table where the key is the input name, |
|
| 75 | - * and the value is number of times seen. This is |
|
| 76 | - * used to calculate unique ids. |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - public $names = array(); |
|
| 81 | - |
|
| 82 | - // Namespaces |
|
| 83 | - //////////////////////////////////////////////////////////////////// |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * The namespace of Form elements |
|
| 87 | - */ |
|
| 88 | - const FORMSPACE = 'Former\Form\\'; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * The namespace of fields |
|
| 92 | - */ |
|
| 93 | - const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Build a new Former instance |
|
| 97 | - * |
|
| 98 | - * @param Container $app |
|
| 99 | - * @param MethodDispatcher $dispatcher |
|
| 100 | - */ |
|
| 101 | - public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 102 | - { |
|
| 103 | - $this->app = $app; |
|
| 104 | - $this->dispatch = $dispatcher; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - //////////////////////////// INTERFACE ///////////////////////////// |
|
| 109 | - //////////////////////////////////////////////////////////////////// |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Acts as a router that redirects methods to all of Former classes |
|
| 113 | - * |
|
| 114 | - * @param string $method The method called |
|
| 115 | - * @param array $parameters An array of parameters |
|
| 116 | - * |
|
| 117 | - * @return mixed |
|
| 118 | - */ |
|
| 119 | - public function __call($method, $parameters) |
|
| 120 | - { |
|
| 121 | - // Dispatch to Form\Elements |
|
| 122 | - // Explicitly check false since closeGroup() may return an empty string |
|
| 123 | - if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 124 | - return $element; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // Dispatch to Form\Form |
|
| 128 | - if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 129 | - $this->app->instance('former.form', $form); |
|
| 130 | - |
|
| 131 | - return $this->app['former.form']; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Dispatch to Form\Group |
|
| 135 | - if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 136 | - return $group; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Dispatch to Form\Actions |
|
| 140 | - if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 141 | - return $actions; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // Dispatch to macros |
|
| 145 | - if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 146 | - return $macro; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // Checking for any supplementary classes |
|
| 150 | - $modifiers = explode('_', $method); |
|
| 151 | - $method = array_pop($modifiers); |
|
| 152 | - |
|
| 153 | - // Dispatch to the different Form\Fields |
|
| 154 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 155 | - $field->setModifiers($modifiers); |
|
| 156 | - $field->addClass(''); |
|
| 157 | - |
|
| 158 | - // Else bind field |
|
| 159 | - $this->app->instance('former.field', $field); |
|
| 160 | - |
|
| 161 | - return $this->app['former.field']; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - //////////////////////////////////////////////////////////////////// |
|
| 165 | - //////////////////////////////// MACROS //////////////////////////// |
|
| 166 | - //////////////////////////////////////////////////////////////////// |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Register a macro with Former |
|
| 170 | - * |
|
| 171 | - * @param string $name The name of the macro |
|
| 172 | - * @param Callable $macro The macro itself |
|
| 173 | - * |
|
| 174 | - * @return mixed |
|
| 175 | - */ |
|
| 176 | - public function macro($name, $macro) |
|
| 177 | - { |
|
| 178 | - $this->macros[$name] = $macro; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Check if a macro exists |
|
| 183 | - * |
|
| 184 | - * @param string $name |
|
| 185 | - * |
|
| 186 | - * @return boolean |
|
| 187 | - */ |
|
| 188 | - public function hasMacro($name) |
|
| 189 | - { |
|
| 190 | - return isset($this->macros[$name]); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Get a registered macro |
|
| 195 | - * |
|
| 196 | - * @param string $name |
|
| 197 | - * |
|
| 198 | - * @return Closure |
|
| 199 | - */ |
|
| 200 | - public function getMacro($name) |
|
| 201 | - { |
|
| 202 | - return $this->macros[$name]; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - //////////////////////////////////////////////////////////////////// |
|
| 206 | - ///////////////////////////// POPULATOR //////////////////////////// |
|
| 207 | - //////////////////////////////////////////////////////////////////// |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Add values to populate the array |
|
| 211 | - * |
|
| 212 | - * @param mixed $values Can be an Eloquent object or an array |
|
| 213 | - */ |
|
| 214 | - public function populate($values) |
|
| 215 | - { |
|
| 216 | - $this->app['former.populator']->replace($values); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Set the value of a particular field |
|
| 221 | - * |
|
| 222 | - * @param string $field The field's name |
|
| 223 | - * @param mixed $value Its new value |
|
| 224 | - */ |
|
| 225 | - public function populateField($field, $value) |
|
| 226 | - { |
|
| 227 | - $this->app['former.populator']->put($field, $value); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the value of a field |
|
| 232 | - * |
|
| 233 | - * @param string $field The field's name |
|
| 234 | - * @param null $fallback |
|
| 235 | - * |
|
| 236 | - * @return mixed |
|
| 237 | - */ |
|
| 238 | - public function getValue($field, $fallback = null) |
|
| 239 | - { |
|
| 240 | - return $this->app['former.populator']->get($field, $fallback); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Fetch a field value from both the new and old POST array |
|
| 245 | - * |
|
| 246 | - * @param string $name A field name |
|
| 247 | - * @param string $fallback A fallback if nothing was found |
|
| 248 | - * |
|
| 249 | - * @return string The results |
|
| 250 | - */ |
|
| 251 | - public function getPost($name, $fallback = null) |
|
| 252 | - { |
|
| 253 | - $name = str_replace(array('[', ']'), array('.', ''), $name ?? ''); |
|
| 254 | - $name = trim($name, '.'); |
|
| 255 | - $oldValue = $this->app['request']->old($name, $fallback); |
|
| 256 | - |
|
| 257 | - return $this->app['request']->input($name, $oldValue, true); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - //////////////////////////////////////////////////////////////////// |
|
| 261 | - ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 262 | - //////////////////////////////////////////////////////////////////// |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Set the errors to use for validations |
|
| 266 | - * |
|
| 267 | - * @param Message $validator The result from a validation |
|
| 268 | - * |
|
| 269 | - * @return void |
|
| 270 | - */ |
|
| 271 | - public function withErrors($validator = null) |
|
| 272 | - { |
|
| 273 | - // Try to get the errors form the session |
|
| 274 | - if ($this->app['session']->has('errors')) { |
|
| 275 | - $this->errors = $this->app['session']->get('errors'); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // If we're given a raw Validator, go fetch the errors in it |
|
| 279 | - if ($validator instanceof Validator) { |
|
| 280 | - $this->errors = $validator->getMessageBag(); |
|
| 281 | - } else { |
|
| 282 | - if ($validator instanceof MessageBag) { |
|
| 283 | - $this->errors = $validator; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return $this->errors; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Add live validation rules |
|
| 292 | - * |
|
| 293 | - * @param array *$rules An array of Laravel rules |
|
| 294 | - * |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - public function withRules() |
|
| 298 | - { |
|
| 299 | - $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 300 | - |
|
| 301 | - // Parse the rules according to Laravel conventions |
|
| 302 | - foreach ($rules as $name => $fieldRules) { |
|
| 303 | - $expFieldRules = $fieldRules; |
|
| 304 | - if (!is_array($expFieldRules)) { |
|
| 305 | - if (is_object($expFieldRules)) { |
|
| 306 | - continue; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - $expFieldRules = explode('|', $expFieldRules); |
|
| 310 | - $expFieldRules = array_map('trim', $expFieldRules); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - foreach ($expFieldRules as $rule) { |
|
| 314 | - if (is_object($rule)) { |
|
| 315 | - continue; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - $parameters = null; |
|
| 319 | - |
|
| 320 | - if (($colon = strpos($rule, ':')) !== false) { |
|
| 321 | - $rulename = substr($rule, 0, $colon); |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 325 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 326 | - */ |
|
| 327 | - if ($rulename !== 'regex') { |
|
| 328 | - $parameters = str_getcsv(substr($rule, $colon + 1), ',', '"', "\\"); |
|
| 329 | - } else { |
|
| 330 | - $parameters = [substr($rule, $colon + 1)]; |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - // Exclude unsupported rules |
|
| 335 | - $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 336 | - |
|
| 337 | - // Store processed rule in Former's array |
|
| 338 | - if (!isset($parameters)) { |
|
| 339 | - $parameters = array(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - $this->rules[$name][$rule] = $parameters; |
|
| 343 | - } |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Switch the framework used by Former |
|
| 349 | - * |
|
| 350 | - * @param string $framework The name of the framework to use |
|
| 351 | - */ |
|
| 352 | - public function framework($framework = null) |
|
| 353 | - { |
|
| 354 | - if (!$framework) { |
|
| 355 | - return $this->app['former.framework']->current(); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - $this->setOption('framework', $framework); |
|
| 359 | - |
|
| 360 | - $framework = $this->getFrameworkInstance($framework); |
|
| 361 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 362 | - return $framework; |
|
| 363 | - }); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Get a new framework instance |
|
| 368 | - * |
|
| 369 | - * @param string $framework |
|
| 370 | - * |
|
| 371 | - * @throws Exceptions\InvalidFrameworkException |
|
| 372 | - * @return \Former\Interfaces\FrameworkInterface |
|
| 373 | - */ |
|
| 374 | - public function getFrameworkInstance($framework) |
|
| 375 | - { |
|
| 376 | - $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 377 | - |
|
| 378 | - //get interfaces of the given framework |
|
| 379 | - $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 380 | - |
|
| 381 | - if(class_exists($formerClass)) { |
|
| 382 | - $returnClass = $formerClass; |
|
| 383 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 384 | - // We have some outside class, lets return it. |
|
| 385 | - $returnClass = $framework; |
|
| 386 | - } else { |
|
| 387 | - throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - return new $returnClass($this->app); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Get an option from the config |
|
| 395 | - * |
|
| 396 | - * @param string $option The option |
|
| 397 | - * @param mixed $default Optional fallback |
|
| 398 | - * |
|
| 399 | - * @return mixed |
|
| 400 | - */ |
|
| 401 | - public function getOption($option, $default = null) |
|
| 402 | - { |
|
| 403 | - return $this->app['config']->get('former.'.$option, $default); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Set an option on the config |
|
| 408 | - * |
|
| 409 | - * @param string $option |
|
| 410 | - * @param string $value |
|
| 411 | - */ |
|
| 412 | - public function setOption($option, $value) |
|
| 413 | - { |
|
| 414 | - return $this->app['config']->set('former.'.$option, $value); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - //////////////////////////////////////////////////////////////////// |
|
| 418 | - ////////////////////////////// BUILDERS //////////////////////////// |
|
| 419 | - //////////////////////////////////////////////////////////////////// |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Closes a form |
|
| 423 | - * |
|
| 424 | - * @return string A form closing tag |
|
| 425 | - */ |
|
| 426 | - public function close() |
|
| 427 | - { |
|
| 428 | - if ($this->app->bound('former.form')) { |
|
| 429 | - $closing = $this->app['former.form']->close(); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - // Destroy instances |
|
| 433 | - $instances = array('former.form', 'former.form.framework'); |
|
| 434 | - foreach ($instances as $instance) { |
|
| 435 | - $this->app[$instance] = null; |
|
| 436 | - unset($this->app[$instance]); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // Reset populator |
|
| 440 | - $this->app['former.populator']->reset(); |
|
| 441 | - |
|
| 442 | - // Reset all values |
|
| 443 | - $this->errors = null; |
|
| 444 | - $this->rules = array(); |
|
| 445 | - |
|
| 446 | - return isset($closing) ? $closing : null; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - //////////////////////////////////////////////////////////////////// |
|
| 450 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 451 | - //////////////////////////////////////////////////////////////////// |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Get the errors for the current field |
|
| 455 | - * |
|
| 456 | - * @param string $name A field name |
|
| 457 | - * |
|
| 458 | - * @return string An error message |
|
| 459 | - */ |
|
| 460 | - public function getErrors($name = null) |
|
| 461 | - { |
|
| 462 | - // Get name and translate array notation |
|
| 463 | - if (!$name and $this->app['former.field']) { |
|
| 464 | - $name = $this->app['former.field']->getName(); |
|
| 465 | - |
|
| 466 | - // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 467 | - if (!$name) { |
|
| 468 | - return ''; |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if ($this->errors and $name) { |
|
| 473 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 474 | - $name = trim($name, '.'); |
|
| 475 | - |
|
| 476 | - return $this->errors->first($name); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - return $this->errors; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Get a rule from the Rules array |
|
| 484 | - * |
|
| 485 | - * @param string $name The field to fetch |
|
| 486 | - * |
|
| 487 | - * @return array An array of rules |
|
| 488 | - */ |
|
| 489 | - public function getRules($name) |
|
| 490 | - { |
|
| 491 | - // Check the rules for the name as given |
|
| 492 | - $ruleset = Arr::get($this->rules, $name); |
|
| 493 | - |
|
| 494 | - // If no rules found, convert to dot notation and try again |
|
| 495 | - if (is_null($ruleset)) { |
|
| 496 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 497 | - $name = trim($name, '.'); |
|
| 498 | - $ruleset = Arr::get($this->rules, $name); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - return $ruleset; |
|
| 502 | - } |
|
| 17 | + // Instances |
|
| 18 | + //////////////////////////////////////////////////////////////////// |
|
| 19 | + |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The current environment |
|
| 23 | + * |
|
| 24 | + * @var \Illuminate\Container\Container |
|
| 25 | + */ |
|
| 26 | + protected $app; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Method Dispatcher |
|
| 30 | + * |
|
| 31 | + * @var MethodDispatcher |
|
| 32 | + */ |
|
| 33 | + protected $dispatch; |
|
| 34 | + |
|
| 35 | + // Informations |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The form's errors |
|
| 40 | + * |
|
| 41 | + * @var Message |
|
| 42 | + */ |
|
| 43 | + protected $errors; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * An array of rules to use |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + */ |
|
| 50 | + protected $rules = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * An array of field macros |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $macros = array(); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The labels created so far |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + public $labels = array(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The IDs created so far |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + public $ids = array(); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * A lookup table where the key is the input name, |
|
| 75 | + * and the value is number of times seen. This is |
|
| 76 | + * used to calculate unique ids. |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + public $names = array(); |
|
| 81 | + |
|
| 82 | + // Namespaces |
|
| 83 | + //////////////////////////////////////////////////////////////////// |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * The namespace of Form elements |
|
| 87 | + */ |
|
| 88 | + const FORMSPACE = 'Former\Form\\'; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * The namespace of fields |
|
| 92 | + */ |
|
| 93 | + const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Build a new Former instance |
|
| 97 | + * |
|
| 98 | + * @param Container $app |
|
| 99 | + * @param MethodDispatcher $dispatcher |
|
| 100 | + */ |
|
| 101 | + public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 102 | + { |
|
| 103 | + $this->app = $app; |
|
| 104 | + $this->dispatch = $dispatcher; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + //////////////////////////// INTERFACE ///////////////////////////// |
|
| 109 | + //////////////////////////////////////////////////////////////////// |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Acts as a router that redirects methods to all of Former classes |
|
| 113 | + * |
|
| 114 | + * @param string $method The method called |
|
| 115 | + * @param array $parameters An array of parameters |
|
| 116 | + * |
|
| 117 | + * @return mixed |
|
| 118 | + */ |
|
| 119 | + public function __call($method, $parameters) |
|
| 120 | + { |
|
| 121 | + // Dispatch to Form\Elements |
|
| 122 | + // Explicitly check false since closeGroup() may return an empty string |
|
| 123 | + if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 124 | + return $element; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // Dispatch to Form\Form |
|
| 128 | + if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 129 | + $this->app->instance('former.form', $form); |
|
| 130 | + |
|
| 131 | + return $this->app['former.form']; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Dispatch to Form\Group |
|
| 135 | + if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 136 | + return $group; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Dispatch to Form\Actions |
|
| 140 | + if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 141 | + return $actions; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // Dispatch to macros |
|
| 145 | + if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 146 | + return $macro; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // Checking for any supplementary classes |
|
| 150 | + $modifiers = explode('_', $method); |
|
| 151 | + $method = array_pop($modifiers); |
|
| 152 | + |
|
| 153 | + // Dispatch to the different Form\Fields |
|
| 154 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 155 | + $field->setModifiers($modifiers); |
|
| 156 | + $field->addClass(''); |
|
| 157 | + |
|
| 158 | + // Else bind field |
|
| 159 | + $this->app->instance('former.field', $field); |
|
| 160 | + |
|
| 161 | + return $this->app['former.field']; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + //////////////////////////////////////////////////////////////////// |
|
| 165 | + //////////////////////////////// MACROS //////////////////////////// |
|
| 166 | + //////////////////////////////////////////////////////////////////// |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Register a macro with Former |
|
| 170 | + * |
|
| 171 | + * @param string $name The name of the macro |
|
| 172 | + * @param Callable $macro The macro itself |
|
| 173 | + * |
|
| 174 | + * @return mixed |
|
| 175 | + */ |
|
| 176 | + public function macro($name, $macro) |
|
| 177 | + { |
|
| 178 | + $this->macros[$name] = $macro; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Check if a macro exists |
|
| 183 | + * |
|
| 184 | + * @param string $name |
|
| 185 | + * |
|
| 186 | + * @return boolean |
|
| 187 | + */ |
|
| 188 | + public function hasMacro($name) |
|
| 189 | + { |
|
| 190 | + return isset($this->macros[$name]); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Get a registered macro |
|
| 195 | + * |
|
| 196 | + * @param string $name |
|
| 197 | + * |
|
| 198 | + * @return Closure |
|
| 199 | + */ |
|
| 200 | + public function getMacro($name) |
|
| 201 | + { |
|
| 202 | + return $this->macros[$name]; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + //////////////////////////////////////////////////////////////////// |
|
| 206 | + ///////////////////////////// POPULATOR //////////////////////////// |
|
| 207 | + //////////////////////////////////////////////////////////////////// |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Add values to populate the array |
|
| 211 | + * |
|
| 212 | + * @param mixed $values Can be an Eloquent object or an array |
|
| 213 | + */ |
|
| 214 | + public function populate($values) |
|
| 215 | + { |
|
| 216 | + $this->app['former.populator']->replace($values); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Set the value of a particular field |
|
| 221 | + * |
|
| 222 | + * @param string $field The field's name |
|
| 223 | + * @param mixed $value Its new value |
|
| 224 | + */ |
|
| 225 | + public function populateField($field, $value) |
|
| 226 | + { |
|
| 227 | + $this->app['former.populator']->put($field, $value); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the value of a field |
|
| 232 | + * |
|
| 233 | + * @param string $field The field's name |
|
| 234 | + * @param null $fallback |
|
| 235 | + * |
|
| 236 | + * @return mixed |
|
| 237 | + */ |
|
| 238 | + public function getValue($field, $fallback = null) |
|
| 239 | + { |
|
| 240 | + return $this->app['former.populator']->get($field, $fallback); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Fetch a field value from both the new and old POST array |
|
| 245 | + * |
|
| 246 | + * @param string $name A field name |
|
| 247 | + * @param string $fallback A fallback if nothing was found |
|
| 248 | + * |
|
| 249 | + * @return string The results |
|
| 250 | + */ |
|
| 251 | + public function getPost($name, $fallback = null) |
|
| 252 | + { |
|
| 253 | + $name = str_replace(array('[', ']'), array('.', ''), $name ?? ''); |
|
| 254 | + $name = trim($name, '.'); |
|
| 255 | + $oldValue = $this->app['request']->old($name, $fallback); |
|
| 256 | + |
|
| 257 | + return $this->app['request']->input($name, $oldValue, true); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + //////////////////////////////////////////////////////////////////// |
|
| 261 | + ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 262 | + //////////////////////////////////////////////////////////////////// |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Set the errors to use for validations |
|
| 266 | + * |
|
| 267 | + * @param Message $validator The result from a validation |
|
| 268 | + * |
|
| 269 | + * @return void |
|
| 270 | + */ |
|
| 271 | + public function withErrors($validator = null) |
|
| 272 | + { |
|
| 273 | + // Try to get the errors form the session |
|
| 274 | + if ($this->app['session']->has('errors')) { |
|
| 275 | + $this->errors = $this->app['session']->get('errors'); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // If we're given a raw Validator, go fetch the errors in it |
|
| 279 | + if ($validator instanceof Validator) { |
|
| 280 | + $this->errors = $validator->getMessageBag(); |
|
| 281 | + } else { |
|
| 282 | + if ($validator instanceof MessageBag) { |
|
| 283 | + $this->errors = $validator; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return $this->errors; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Add live validation rules |
|
| 292 | + * |
|
| 293 | + * @param array *$rules An array of Laravel rules |
|
| 294 | + * |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + public function withRules() |
|
| 298 | + { |
|
| 299 | + $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 300 | + |
|
| 301 | + // Parse the rules according to Laravel conventions |
|
| 302 | + foreach ($rules as $name => $fieldRules) { |
|
| 303 | + $expFieldRules = $fieldRules; |
|
| 304 | + if (!is_array($expFieldRules)) { |
|
| 305 | + if (is_object($expFieldRules)) { |
|
| 306 | + continue; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $expFieldRules = explode('|', $expFieldRules); |
|
| 310 | + $expFieldRules = array_map('trim', $expFieldRules); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + foreach ($expFieldRules as $rule) { |
|
| 314 | + if (is_object($rule)) { |
|
| 315 | + continue; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + $parameters = null; |
|
| 319 | + |
|
| 320 | + if (($colon = strpos($rule, ':')) !== false) { |
|
| 321 | + $rulename = substr($rule, 0, $colon); |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 325 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 326 | + */ |
|
| 327 | + if ($rulename !== 'regex') { |
|
| 328 | + $parameters = str_getcsv(substr($rule, $colon + 1), ',', '"', "\\"); |
|
| 329 | + } else { |
|
| 330 | + $parameters = [substr($rule, $colon + 1)]; |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + // Exclude unsupported rules |
|
| 335 | + $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 336 | + |
|
| 337 | + // Store processed rule in Former's array |
|
| 338 | + if (!isset($parameters)) { |
|
| 339 | + $parameters = array(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + $this->rules[$name][$rule] = $parameters; |
|
| 343 | + } |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Switch the framework used by Former |
|
| 349 | + * |
|
| 350 | + * @param string $framework The name of the framework to use |
|
| 351 | + */ |
|
| 352 | + public function framework($framework = null) |
|
| 353 | + { |
|
| 354 | + if (!$framework) { |
|
| 355 | + return $this->app['former.framework']->current(); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + $this->setOption('framework', $framework); |
|
| 359 | + |
|
| 360 | + $framework = $this->getFrameworkInstance($framework); |
|
| 361 | + $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 362 | + return $framework; |
|
| 363 | + }); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Get a new framework instance |
|
| 368 | + * |
|
| 369 | + * @param string $framework |
|
| 370 | + * |
|
| 371 | + * @throws Exceptions\InvalidFrameworkException |
|
| 372 | + * @return \Former\Interfaces\FrameworkInterface |
|
| 373 | + */ |
|
| 374 | + public function getFrameworkInstance($framework) |
|
| 375 | + { |
|
| 376 | + $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 377 | + |
|
| 378 | + //get interfaces of the given framework |
|
| 379 | + $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 380 | + |
|
| 381 | + if(class_exists($formerClass)) { |
|
| 382 | + $returnClass = $formerClass; |
|
| 383 | + } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 384 | + // We have some outside class, lets return it. |
|
| 385 | + $returnClass = $framework; |
|
| 386 | + } else { |
|
| 387 | + throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + return new $returnClass($this->app); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Get an option from the config |
|
| 395 | + * |
|
| 396 | + * @param string $option The option |
|
| 397 | + * @param mixed $default Optional fallback |
|
| 398 | + * |
|
| 399 | + * @return mixed |
|
| 400 | + */ |
|
| 401 | + public function getOption($option, $default = null) |
|
| 402 | + { |
|
| 403 | + return $this->app['config']->get('former.'.$option, $default); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Set an option on the config |
|
| 408 | + * |
|
| 409 | + * @param string $option |
|
| 410 | + * @param string $value |
|
| 411 | + */ |
|
| 412 | + public function setOption($option, $value) |
|
| 413 | + { |
|
| 414 | + return $this->app['config']->set('former.'.$option, $value); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + //////////////////////////////////////////////////////////////////// |
|
| 418 | + ////////////////////////////// BUILDERS //////////////////////////// |
|
| 419 | + //////////////////////////////////////////////////////////////////// |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Closes a form |
|
| 423 | + * |
|
| 424 | + * @return string A form closing tag |
|
| 425 | + */ |
|
| 426 | + public function close() |
|
| 427 | + { |
|
| 428 | + if ($this->app->bound('former.form')) { |
|
| 429 | + $closing = $this->app['former.form']->close(); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + // Destroy instances |
|
| 433 | + $instances = array('former.form', 'former.form.framework'); |
|
| 434 | + foreach ($instances as $instance) { |
|
| 435 | + $this->app[$instance] = null; |
|
| 436 | + unset($this->app[$instance]); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // Reset populator |
|
| 440 | + $this->app['former.populator']->reset(); |
|
| 441 | + |
|
| 442 | + // Reset all values |
|
| 443 | + $this->errors = null; |
|
| 444 | + $this->rules = array(); |
|
| 445 | + |
|
| 446 | + return isset($closing) ? $closing : null; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + //////////////////////////////////////////////////////////////////// |
|
| 450 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 451 | + //////////////////////////////////////////////////////////////////// |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Get the errors for the current field |
|
| 455 | + * |
|
| 456 | + * @param string $name A field name |
|
| 457 | + * |
|
| 458 | + * @return string An error message |
|
| 459 | + */ |
|
| 460 | + public function getErrors($name = null) |
|
| 461 | + { |
|
| 462 | + // Get name and translate array notation |
|
| 463 | + if (!$name and $this->app['former.field']) { |
|
| 464 | + $name = $this->app['former.field']->getName(); |
|
| 465 | + |
|
| 466 | + // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 467 | + if (!$name) { |
|
| 468 | + return ''; |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if ($this->errors and $name) { |
|
| 473 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 474 | + $name = trim($name, '.'); |
|
| 475 | + |
|
| 476 | + return $this->errors->first($name); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return $this->errors; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Get a rule from the Rules array |
|
| 484 | + * |
|
| 485 | + * @param string $name The field to fetch |
|
| 486 | + * |
|
| 487 | + * @return array An array of rules |
|
| 488 | + */ |
|
| 489 | + public function getRules($name) |
|
| 490 | + { |
|
| 491 | + // Check the rules for the name as given |
|
| 492 | + $ruleset = Arr::get($this->rules, $name); |
|
| 493 | + |
|
| 494 | + // If no rules found, convert to dot notation and try again |
|
| 495 | + if (is_null($ruleset)) { |
|
| 496 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 497 | + $name = trim($name, '.'); |
|
| 498 | + $ruleset = Arr::get($this->rules, $name); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + return $ruleset; |
|
| 502 | + } |
|
| 503 | 503 | } |
@@ -10,322 +10,322 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class Framework implements FrameworkInterface |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The Container |
|
| 15 | - * |
|
| 16 | - * @var Container |
|
| 17 | - */ |
|
| 18 | - protected $app; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Form types that trigger special styling |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $availableTypes = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The field states available |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $states = array(); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The default label width (for horizontal forms) |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $labelWidth; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * The default field width (for horizontal forms) |
|
| 43 | - * |
|
| 44 | - * @var string |
|
| 45 | - */ |
|
| 46 | - protected $fieldWidth; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * The default offset for fields (for horizontal form fields |
|
| 50 | - * with no label, so usually equal to the default label width) |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $fieldOffset; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The default HTML tag used for icons |
|
| 58 | - * |
|
| 59 | - * @var string |
|
| 60 | - */ |
|
| 61 | - protected $iconTag; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The default set for icon fonts |
|
| 65 | - * |
|
| 66 | - * @var string |
|
| 67 | - */ |
|
| 68 | - protected $iconSet; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * The default prefix icon names |
|
| 72 | - * |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - protected $iconPrefix; |
|
| 76 | - |
|
| 77 | - //////////////////////////////////////////////////////////////////// |
|
| 78 | - //////////////////////// CURRENT FRAMEWORK ///////////////////////// |
|
| 79 | - //////////////////////////////////////////////////////////////////// |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the name of the current framework |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function current() |
|
| 87 | - { |
|
| 88 | - return basename(str_replace('\\', '/', get_class($this))); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Check if the current framework matches something |
|
| 93 | - * |
|
| 94 | - * @param string $framework |
|
| 95 | - * |
|
| 96 | - * @return boolean |
|
| 97 | - */ |
|
| 98 | - public function is($framework) |
|
| 99 | - { |
|
| 100 | - return $framework == $this->current(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Check if the current framework doesn't match something |
|
| 105 | - * |
|
| 106 | - * @param string $framework |
|
| 107 | - * |
|
| 108 | - * @return boolean |
|
| 109 | - */ |
|
| 110 | - public function isnt($framework) |
|
| 111 | - { |
|
| 112 | - return $framework != $this->current(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - //////////////////////////////////////////////////////////////////// |
|
| 116 | - /////////////////////////// COMMON METHODS ///////////////////////// |
|
| 117 | - //////////////////////////////////////////////////////////////////// |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * List form types triggered special styling form current framework |
|
| 121 | - * |
|
| 122 | - * @return array |
|
| 123 | - */ |
|
| 124 | - public function availableTypes() |
|
| 125 | - { |
|
| 126 | - return $this->availableTypes; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Filter a field state |
|
| 131 | - * |
|
| 132 | - * @param string $state |
|
| 133 | - * |
|
| 134 | - * @return string |
|
| 135 | - */ |
|
| 136 | - public function filterState($state) |
|
| 137 | - { |
|
| 138 | - // Filter out wrong states |
|
| 139 | - return in_array($state, $this->states) ? $state : null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Framework error state |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - public function errorState() |
|
| 148 | - { |
|
| 149 | - return 'error'; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Returns corresponding inline class of a field |
|
| 154 | - * |
|
| 155 | - * @param Field $field |
|
| 156 | - * |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function getInlineLabelClass($field) |
|
| 160 | - { |
|
| 161 | - return 'inline'; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Set framework defaults from its config file |
|
| 166 | - */ |
|
| 167 | - protected function setFrameworkDefaults() |
|
| 168 | - { |
|
| 169 | - $this->setFieldWidths($this->getFrameworkOption('labelWidths')); |
|
| 170 | - $this->setIconDefaults(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param string $widths |
|
| 175 | - */ |
|
| 176 | - protected function setFieldWidths($widths) |
|
| 177 | - { |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Override framework defaults for icons with config values where set |
|
| 182 | - */ |
|
| 183 | - protected function setIconDefaults() |
|
| 184 | - { |
|
| 185 | - $this->iconTag = $this->getFrameworkOption('icon.tag'); |
|
| 186 | - $this->iconSet = $this->getFrameworkOption('icon.set'); |
|
| 187 | - $this->iconPrefix = $this->getFrameworkOption('icon.prefix'); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Render an icon |
|
| 192 | - * |
|
| 193 | - * @param array $attributes Its general attributes |
|
| 194 | - * |
|
| 195 | - * @return string |
|
| 196 | - */ |
|
| 197 | - public function createIcon($iconType, $attributes = array(), $iconSettings = array()) |
|
| 198 | - { |
|
| 199 | - // Check for empty icons |
|
| 200 | - if (!$iconType) { |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - // icon settings can be overridden for a specific icon |
|
| 205 | - $tag = Arr::get($iconSettings, 'tag', $this->iconTag); |
|
| 206 | - $set = Arr::get($iconSettings, 'set', $this->iconSet); |
|
| 207 | - $prefix = Arr::get($iconSettings, 'prefix', $this->iconPrefix); |
|
| 208 | - |
|
| 209 | - return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType"); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - //////////////////////////////////////////////////////////////////// |
|
| 213 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 214 | - //////////////////////////////////////////////////////////////////// |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Add classes to a field |
|
| 218 | - * |
|
| 219 | - * @param Field $field |
|
| 220 | - * @param array $classes |
|
| 221 | - * |
|
| 222 | - * @return \Former\Traits\Field |
|
| 223 | - */ |
|
| 224 | - protected function addClassesToField($field, $classes) |
|
| 225 | - { |
|
| 226 | - // If we found any class, add them |
|
| 227 | - if ($classes) { |
|
| 228 | - $field->addClass(implode(' ', $classes)); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return $field; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Prepend an array of classes with a string |
|
| 236 | - * |
|
| 237 | - * @param array $classes The classes to prepend |
|
| 238 | - * @param string $with The string to prepend them with |
|
| 239 | - * |
|
| 240 | - * @return array A prepended array |
|
| 241 | - */ |
|
| 242 | - protected function prependWith($classes, $with) |
|
| 243 | - { |
|
| 244 | - return array_map(function ($class) use ($with) { |
|
| 245 | - return $with.$class; |
|
| 246 | - }, $classes); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Create a label for a field |
|
| 251 | - * |
|
| 252 | - * @param Field $field |
|
| 253 | - * @param Element $label The field label if non provided |
|
| 254 | - * |
|
| 255 | - * @return string A label |
|
| 256 | - */ |
|
| 257 | - public function createLabelOf(Field $field, ?Element $label = null) |
|
| 258 | - { |
|
| 259 | - // Get the label and its informations |
|
| 260 | - if (!$label) { |
|
| 261 | - $label = $field->getLabel(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - // Get label "for" |
|
| 265 | - $for = $field->id ?: $field->getName(); |
|
| 266 | - |
|
| 267 | - // Get label text |
|
| 268 | - $text = $label->getValue(); |
|
| 269 | - if (!$text) { |
|
| 270 | - return false; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - // Append required text |
|
| 274 | - if ($field->isRequired()) { |
|
| 275 | - $text .= $this->app['former']->getOption('required_text'); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // Render plain label if checkable, else a classic one |
|
| 279 | - $label->setValue($text); |
|
| 280 | - if (!$field->isCheckable()) { |
|
| 281 | - $label->for($for); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return $label; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Get an option for the current framework |
|
| 289 | - * |
|
| 290 | - * @param string $option |
|
| 291 | - * |
|
| 292 | - * @return string |
|
| 293 | - */ |
|
| 294 | - protected function getFrameworkOption($option) |
|
| 295 | - { |
|
| 296 | - return $this->app['config']->get("former.{$this->current()}.$option"); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - //////////////////////////////////////////////////////////////////// |
|
| 300 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 301 | - //////////////////////////////////////////////////////////////////// |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Wraps all label contents with potential additional tags. |
|
| 305 | - * |
|
| 306 | - * @param string $label |
|
| 307 | - * |
|
| 308 | - * @return string A wrapped label |
|
| 309 | - */ |
|
| 310 | - public function wrapLabel($label) |
|
| 311 | - { |
|
| 312 | - return $label; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - //////////////////////////////////////////////////////////////////// |
|
| 316 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 317 | - //////////////////////////////////////////////////////////////////// |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Render an validation error text |
|
| 321 | - * |
|
| 322 | - * @param string $text |
|
| 323 | - * @param array $attributes |
|
| 324 | - * |
|
| 325 | - * @return string |
|
| 326 | - */ |
|
| 327 | - public function createValidationError($text, $attributes = array()) |
|
| 328 | - { |
|
| 329 | - return $this->createHelp($text, $attributes); |
|
| 330 | - } |
|
| 13 | + /** |
|
| 14 | + * The Container |
|
| 15 | + * |
|
| 16 | + * @var Container |
|
| 17 | + */ |
|
| 18 | + protected $app; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Form types that trigger special styling |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $availableTypes = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The field states available |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $states = array(); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The default label width (for horizontal forms) |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $labelWidth; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * The default field width (for horizontal forms) |
|
| 43 | + * |
|
| 44 | + * @var string |
|
| 45 | + */ |
|
| 46 | + protected $fieldWidth; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * The default offset for fields (for horizontal form fields |
|
| 50 | + * with no label, so usually equal to the default label width) |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $fieldOffset; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The default HTML tag used for icons |
|
| 58 | + * |
|
| 59 | + * @var string |
|
| 60 | + */ |
|
| 61 | + protected $iconTag; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The default set for icon fonts |
|
| 65 | + * |
|
| 66 | + * @var string |
|
| 67 | + */ |
|
| 68 | + protected $iconSet; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * The default prefix icon names |
|
| 72 | + * |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + protected $iconPrefix; |
|
| 76 | + |
|
| 77 | + //////////////////////////////////////////////////////////////////// |
|
| 78 | + //////////////////////// CURRENT FRAMEWORK ///////////////////////// |
|
| 79 | + //////////////////////////////////////////////////////////////////// |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the name of the current framework |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function current() |
|
| 87 | + { |
|
| 88 | + return basename(str_replace('\\', '/', get_class($this))); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Check if the current framework matches something |
|
| 93 | + * |
|
| 94 | + * @param string $framework |
|
| 95 | + * |
|
| 96 | + * @return boolean |
|
| 97 | + */ |
|
| 98 | + public function is($framework) |
|
| 99 | + { |
|
| 100 | + return $framework == $this->current(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Check if the current framework doesn't match something |
|
| 105 | + * |
|
| 106 | + * @param string $framework |
|
| 107 | + * |
|
| 108 | + * @return boolean |
|
| 109 | + */ |
|
| 110 | + public function isnt($framework) |
|
| 111 | + { |
|
| 112 | + return $framework != $this->current(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + //////////////////////////////////////////////////////////////////// |
|
| 116 | + /////////////////////////// COMMON METHODS ///////////////////////// |
|
| 117 | + //////////////////////////////////////////////////////////////////// |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * List form types triggered special styling form current framework |
|
| 121 | + * |
|
| 122 | + * @return array |
|
| 123 | + */ |
|
| 124 | + public function availableTypes() |
|
| 125 | + { |
|
| 126 | + return $this->availableTypes; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Filter a field state |
|
| 131 | + * |
|
| 132 | + * @param string $state |
|
| 133 | + * |
|
| 134 | + * @return string |
|
| 135 | + */ |
|
| 136 | + public function filterState($state) |
|
| 137 | + { |
|
| 138 | + // Filter out wrong states |
|
| 139 | + return in_array($state, $this->states) ? $state : null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Framework error state |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + public function errorState() |
|
| 148 | + { |
|
| 149 | + return 'error'; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Returns corresponding inline class of a field |
|
| 154 | + * |
|
| 155 | + * @param Field $field |
|
| 156 | + * |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function getInlineLabelClass($field) |
|
| 160 | + { |
|
| 161 | + return 'inline'; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Set framework defaults from its config file |
|
| 166 | + */ |
|
| 167 | + protected function setFrameworkDefaults() |
|
| 168 | + { |
|
| 169 | + $this->setFieldWidths($this->getFrameworkOption('labelWidths')); |
|
| 170 | + $this->setIconDefaults(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param string $widths |
|
| 175 | + */ |
|
| 176 | + protected function setFieldWidths($widths) |
|
| 177 | + { |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Override framework defaults for icons with config values where set |
|
| 182 | + */ |
|
| 183 | + protected function setIconDefaults() |
|
| 184 | + { |
|
| 185 | + $this->iconTag = $this->getFrameworkOption('icon.tag'); |
|
| 186 | + $this->iconSet = $this->getFrameworkOption('icon.set'); |
|
| 187 | + $this->iconPrefix = $this->getFrameworkOption('icon.prefix'); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Render an icon |
|
| 192 | + * |
|
| 193 | + * @param array $attributes Its general attributes |
|
| 194 | + * |
|
| 195 | + * @return string |
|
| 196 | + */ |
|
| 197 | + public function createIcon($iconType, $attributes = array(), $iconSettings = array()) |
|
| 198 | + { |
|
| 199 | + // Check for empty icons |
|
| 200 | + if (!$iconType) { |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + // icon settings can be overridden for a specific icon |
|
| 205 | + $tag = Arr::get($iconSettings, 'tag', $this->iconTag); |
|
| 206 | + $set = Arr::get($iconSettings, 'set', $this->iconSet); |
|
| 207 | + $prefix = Arr::get($iconSettings, 'prefix', $this->iconPrefix); |
|
| 208 | + |
|
| 209 | + return Element::create($tag, null, $attributes)->addClass("$set $prefix-$iconType"); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + //////////////////////////////////////////////////////////////////// |
|
| 213 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 214 | + //////////////////////////////////////////////////////////////////// |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Add classes to a field |
|
| 218 | + * |
|
| 219 | + * @param Field $field |
|
| 220 | + * @param array $classes |
|
| 221 | + * |
|
| 222 | + * @return \Former\Traits\Field |
|
| 223 | + */ |
|
| 224 | + protected function addClassesToField($field, $classes) |
|
| 225 | + { |
|
| 226 | + // If we found any class, add them |
|
| 227 | + if ($classes) { |
|
| 228 | + $field->addClass(implode(' ', $classes)); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return $field; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Prepend an array of classes with a string |
|
| 236 | + * |
|
| 237 | + * @param array $classes The classes to prepend |
|
| 238 | + * @param string $with The string to prepend them with |
|
| 239 | + * |
|
| 240 | + * @return array A prepended array |
|
| 241 | + */ |
|
| 242 | + protected function prependWith($classes, $with) |
|
| 243 | + { |
|
| 244 | + return array_map(function ($class) use ($with) { |
|
| 245 | + return $with.$class; |
|
| 246 | + }, $classes); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Create a label for a field |
|
| 251 | + * |
|
| 252 | + * @param Field $field |
|
| 253 | + * @param Element $label The field label if non provided |
|
| 254 | + * |
|
| 255 | + * @return string A label |
|
| 256 | + */ |
|
| 257 | + public function createLabelOf(Field $field, ?Element $label = null) |
|
| 258 | + { |
|
| 259 | + // Get the label and its informations |
|
| 260 | + if (!$label) { |
|
| 261 | + $label = $field->getLabel(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + // Get label "for" |
|
| 265 | + $for = $field->id ?: $field->getName(); |
|
| 266 | + |
|
| 267 | + // Get label text |
|
| 268 | + $text = $label->getValue(); |
|
| 269 | + if (!$text) { |
|
| 270 | + return false; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + // Append required text |
|
| 274 | + if ($field->isRequired()) { |
|
| 275 | + $text .= $this->app['former']->getOption('required_text'); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // Render plain label if checkable, else a classic one |
|
| 279 | + $label->setValue($text); |
|
| 280 | + if (!$field->isCheckable()) { |
|
| 281 | + $label->for($for); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return $label; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Get an option for the current framework |
|
| 289 | + * |
|
| 290 | + * @param string $option |
|
| 291 | + * |
|
| 292 | + * @return string |
|
| 293 | + */ |
|
| 294 | + protected function getFrameworkOption($option) |
|
| 295 | + { |
|
| 296 | + return $this->app['config']->get("former.{$this->current()}.$option"); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + //////////////////////////////////////////////////////////////////// |
|
| 300 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 301 | + //////////////////////////////////////////////////////////////////// |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Wraps all label contents with potential additional tags. |
|
| 305 | + * |
|
| 306 | + * @param string $label |
|
| 307 | + * |
|
| 308 | + * @return string A wrapped label |
|
| 309 | + */ |
|
| 310 | + public function wrapLabel($label) |
|
| 311 | + { |
|
| 312 | + return $label; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + //////////////////////////////////////////////////////////////////// |
|
| 316 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 317 | + //////////////////////////////////////////////////////////////////// |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Render an validation error text |
|
| 321 | + * |
|
| 322 | + * @param string $text |
|
| 323 | + * @param array $attributes |
|
| 324 | + * |
|
| 325 | + * @return string |
|
| 326 | + */ |
|
| 327 | + public function createValidationError($text, $attributes = array()) |
|
| 328 | + { |
|
| 329 | + return $this->createHelp($text, $attributes); |
|
| 330 | + } |
|
| 331 | 331 | } |
@@ -16,281 +16,281 @@ 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 | - * Renders with floating label |
|
| 70 | - * |
|
| 71 | - * @var boolean |
|
| 72 | - */ |
|
| 73 | - protected $floatingLabel = false; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get the current framework instance |
|
| 77 | - * |
|
| 78 | - * @return Framework |
|
| 79 | - */ |
|
| 80 | - protected function currentFramework() |
|
| 81 | - { |
|
| 82 | - if ($this->app->bound('former.form.framework')) { |
|
| 83 | - return $this->app['former.form.framework']; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $this->app['former.framework']; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - //////////////////////////////////////////////////////////////////// |
|
| 90 | - ///////////////////////////// INTERFACE //////////////////////////// |
|
| 91 | - //////////////////////////////////////////////////////////////////// |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Set up a Field instance |
|
| 95 | - * |
|
| 96 | - * @param string $type A field type |
|
| 97 | - */ |
|
| 98 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 99 | - { |
|
| 100 | - // Set base parameters |
|
| 101 | - $this->app = $app; |
|
| 102 | - $this->type = $type; |
|
| 103 | - $this->value = $value; |
|
| 104 | - $this->setAttributes($attributes); |
|
| 105 | - $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 106 | - |
|
| 107 | - // Compute and translate label |
|
| 108 | - $this->automaticLabels($name, $label); |
|
| 109 | - |
|
| 110 | - // Repopulate field |
|
| 111 | - if ($type != 'password' && $name !== '_token') { |
|
| 112 | - $this->value = $this->repopulate(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // Apply Live validation rules |
|
| 116 | - if ($this->app['former']->getOption('live_validation')) { |
|
| 117 | - $rules = new LiveValidation($this); |
|
| 118 | - $rules->apply($this->getRules()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Bind the Group class |
|
| 122 | - $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 123 | - $groupClass = Former::FORMSPACE.$groupClass; |
|
| 124 | - |
|
| 125 | - $this->group = new $groupClass($this->app, $this->label); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Redirect calls to the group if necessary |
|
| 130 | - * |
|
| 131 | - * @param string $method |
|
| 132 | - */ |
|
| 133 | - public function __call($method, $parameters) |
|
| 134 | - { |
|
| 135 | - // Translate attributes |
|
| 136 | - $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 137 | - if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 138 | - $parameters[0] = Helpers::translate($parameters[0]); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // Redirect calls to the Control Group |
|
| 142 | - if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) { |
|
| 143 | - $method = str_replace('onGroup', '', $method); |
|
| 144 | - $method = lcfirst($method); |
|
| 145 | - |
|
| 146 | - call_user_func_array(array($this->group, $method), $parameters); |
|
| 147 | - |
|
| 148 | - return $this; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return parent::__call($method, $parameters); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Prints out the field, wrapped in its group |
|
| 156 | - * |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function wrapAndRender() |
|
| 160 | - { |
|
| 161 | - // Dry syntax (hidden fields, plain fields) |
|
| 162 | - if ($this->isUnwrappable()) { |
|
| 163 | - $html = $this->render(); |
|
| 164 | - // Control group syntax |
|
| 165 | - } elseif (Form::hasInstanceOpened()) { |
|
| 166 | - $html = $this->group->wrapField($this); |
|
| 167 | - // Classic syntax |
|
| 168 | - } else { |
|
| 169 | - $html = $this->currentFramework()->createLabelOf($this); |
|
| 170 | - $html .= $this->render(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $html; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Prints out the field |
|
| 178 | - * |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function __toString() |
|
| 182 | - { |
|
| 183 | - return $this->wrapAndRender(); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - //////////////////////////////////////////////////////////////////// |
|
| 187 | - ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 188 | - //////////////////////////////////////////////////////////////////// |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Whether the current field is required or not |
|
| 192 | - * |
|
| 193 | - * @return boolean |
|
| 194 | - */ |
|
| 195 | - public function isRequired() |
|
| 196 | - { |
|
| 197 | - return isset($this->attributes['required']); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Set required live validation attribute |
|
| 202 | - * |
|
| 203 | - * @param boolean $isRequired |
|
| 204 | - * @return $this |
|
| 205 | - */ |
|
| 206 | - public function required($isRequired=true) |
|
| 207 | - { |
|
| 208 | - if ($isRequired) { |
|
| 209 | - $this->attributes['required'] = true; |
|
| 210 | - } else { |
|
| 211 | - unset($this->attributes['required']); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return $this; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Check if a field is unwrappable (no label) |
|
| 219 | - * |
|
| 220 | - * @return boolean |
|
| 221 | - */ |
|
| 222 | - public function isUnwrappable() |
|
| 223 | - { |
|
| 224 | - return |
|
| 225 | - ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 226 | - ($this->form and $this->isOfType('inline')) or |
|
| 227 | - $this->isButton() or |
|
| 228 | - $this->isOfType('hidden') or |
|
| 229 | - \Former\Form\Group::$opened or |
|
| 230 | - $this->group and $this->group->isRaw(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Check if field is a checkbox or a radio |
|
| 235 | - * |
|
| 236 | - * @return boolean |
|
| 237 | - */ |
|
| 238 | - public function isCheckable() |
|
| 239 | - { |
|
| 240 | - return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios', 'switch', 'switches'); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Check if the field is a button |
|
| 245 | - * |
|
| 246 | - * @return boolean |
|
| 247 | - */ |
|
| 248 | - public function isButton() |
|
| 249 | - { |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Check if the field get a floating label |
|
| 255 | - * |
|
| 256 | - * @return boolean |
|
| 257 | - */ |
|
| 258 | - public function withFloatingLabel() |
|
| 259 | - { |
|
| 260 | - return $this->floatingLabel; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Get the rules applied to the current field |
|
| 265 | - * |
|
| 266 | - * @return array An array of rules |
|
| 267 | - */ |
|
| 268 | - public function getRules() |
|
| 269 | - { |
|
| 270 | - return $this->app['former']->getRules($this->name); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - //////////////////////////////////////////////////////////////////// |
|
| 274 | - //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 275 | - //////////////////////////////////////////////////////////////////// |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Apply a Live Validation rule by chaining |
|
| 279 | - * |
|
| 280 | - * @param string $rule The rule |
|
| 281 | - */ |
|
| 282 | - public function rule($rule) |
|
| 283 | - { |
|
| 284 | - $parameters = func_get_args(); |
|
| 285 | - array_shift($parameters); |
|
| 286 | - |
|
| 287 | - $live = new LiveValidation($this); |
|
| 288 | - $live->apply(array( |
|
| 289 | - $rule => $parameters, |
|
| 290 | - )); |
|
| 291 | - |
|
| 292 | - return $this; |
|
| 293 | - } |
|
| 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 | + * Renders with floating label |
|
| 70 | + * |
|
| 71 | + * @var boolean |
|
| 72 | + */ |
|
| 73 | + protected $floatingLabel = false; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get the current framework instance |
|
| 77 | + * |
|
| 78 | + * @return Framework |
|
| 79 | + */ |
|
| 80 | + protected function currentFramework() |
|
| 81 | + { |
|
| 82 | + if ($this->app->bound('former.form.framework')) { |
|
| 83 | + return $this->app['former.form.framework']; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $this->app['former.framework']; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + //////////////////////////////////////////////////////////////////// |
|
| 90 | + ///////////////////////////// INTERFACE //////////////////////////// |
|
| 91 | + //////////////////////////////////////////////////////////////////// |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Set up a Field instance |
|
| 95 | + * |
|
| 96 | + * @param string $type A field type |
|
| 97 | + */ |
|
| 98 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 99 | + { |
|
| 100 | + // Set base parameters |
|
| 101 | + $this->app = $app; |
|
| 102 | + $this->type = $type; |
|
| 103 | + $this->value = $value; |
|
| 104 | + $this->setAttributes($attributes); |
|
| 105 | + $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 106 | + |
|
| 107 | + // Compute and translate label |
|
| 108 | + $this->automaticLabels($name, $label); |
|
| 109 | + |
|
| 110 | + // Repopulate field |
|
| 111 | + if ($type != 'password' && $name !== '_token') { |
|
| 112 | + $this->value = $this->repopulate(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // Apply Live validation rules |
|
| 116 | + if ($this->app['former']->getOption('live_validation')) { |
|
| 117 | + $rules = new LiveValidation($this); |
|
| 118 | + $rules->apply($this->getRules()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Bind the Group class |
|
| 122 | + $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 123 | + $groupClass = Former::FORMSPACE.$groupClass; |
|
| 124 | + |
|
| 125 | + $this->group = new $groupClass($this->app, $this->label); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Redirect calls to the group if necessary |
|
| 130 | + * |
|
| 131 | + * @param string $method |
|
| 132 | + */ |
|
| 133 | + public function __call($method, $parameters) |
|
| 134 | + { |
|
| 135 | + // Translate attributes |
|
| 136 | + $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 137 | + if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 138 | + $parameters[0] = Helpers::translate($parameters[0]); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // Redirect calls to the Control Group |
|
| 142 | + if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) { |
|
| 143 | + $method = str_replace('onGroup', '', $method); |
|
| 144 | + $method = lcfirst($method); |
|
| 145 | + |
|
| 146 | + call_user_func_array(array($this->group, $method), $parameters); |
|
| 147 | + |
|
| 148 | + return $this; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return parent::__call($method, $parameters); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Prints out the field, wrapped in its group |
|
| 156 | + * |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function wrapAndRender() |
|
| 160 | + { |
|
| 161 | + // Dry syntax (hidden fields, plain fields) |
|
| 162 | + if ($this->isUnwrappable()) { |
|
| 163 | + $html = $this->render(); |
|
| 164 | + // Control group syntax |
|
| 165 | + } elseif (Form::hasInstanceOpened()) { |
|
| 166 | + $html = $this->group->wrapField($this); |
|
| 167 | + // Classic syntax |
|
| 168 | + } else { |
|
| 169 | + $html = $this->currentFramework()->createLabelOf($this); |
|
| 170 | + $html .= $this->render(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $html; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Prints out the field |
|
| 178 | + * |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function __toString() |
|
| 182 | + { |
|
| 183 | + return $this->wrapAndRender(); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + //////////////////////////////////////////////////////////////////// |
|
| 187 | + ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 188 | + //////////////////////////////////////////////////////////////////// |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Whether the current field is required or not |
|
| 192 | + * |
|
| 193 | + * @return boolean |
|
| 194 | + */ |
|
| 195 | + public function isRequired() |
|
| 196 | + { |
|
| 197 | + return isset($this->attributes['required']); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Set required live validation attribute |
|
| 202 | + * |
|
| 203 | + * @param boolean $isRequired |
|
| 204 | + * @return $this |
|
| 205 | + */ |
|
| 206 | + public function required($isRequired=true) |
|
| 207 | + { |
|
| 208 | + if ($isRequired) { |
|
| 209 | + $this->attributes['required'] = true; |
|
| 210 | + } else { |
|
| 211 | + unset($this->attributes['required']); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return $this; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Check if a field is unwrappable (no label) |
|
| 219 | + * |
|
| 220 | + * @return boolean |
|
| 221 | + */ |
|
| 222 | + public function isUnwrappable() |
|
| 223 | + { |
|
| 224 | + return |
|
| 225 | + ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 226 | + ($this->form and $this->isOfType('inline')) or |
|
| 227 | + $this->isButton() or |
|
| 228 | + $this->isOfType('hidden') or |
|
| 229 | + \Former\Form\Group::$opened or |
|
| 230 | + $this->group and $this->group->isRaw(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Check if field is a checkbox or a radio |
|
| 235 | + * |
|
| 236 | + * @return boolean |
|
| 237 | + */ |
|
| 238 | + public function isCheckable() |
|
| 239 | + { |
|
| 240 | + return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios', 'switch', 'switches'); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Check if the field is a button |
|
| 245 | + * |
|
| 246 | + * @return boolean |
|
| 247 | + */ |
|
| 248 | + public function isButton() |
|
| 249 | + { |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Check if the field get a floating label |
|
| 255 | + * |
|
| 256 | + * @return boolean |
|
| 257 | + */ |
|
| 258 | + public function withFloatingLabel() |
|
| 259 | + { |
|
| 260 | + return $this->floatingLabel; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Get the rules applied to the current field |
|
| 265 | + * |
|
| 266 | + * @return array An array of rules |
|
| 267 | + */ |
|
| 268 | + public function getRules() |
|
| 269 | + { |
|
| 270 | + return $this->app['former']->getRules($this->name); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + //////////////////////////////////////////////////////////////////// |
|
| 274 | + //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 275 | + //////////////////////////////////////////////////////////////////// |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Apply a Live Validation rule by chaining |
|
| 279 | + * |
|
| 280 | + * @param string $rule The rule |
|
| 281 | + */ |
|
| 282 | + public function rule($rule) |
|
| 283 | + { |
|
| 284 | + $parameters = func_get_args(); |
|
| 285 | + array_shift($parameters); |
|
| 286 | + |
|
| 287 | + $live = new LiveValidation($this); |
|
| 288 | + $live->apply(array( |
|
| 289 | + $rule => $parameters, |
|
| 290 | + )); |
|
| 291 | + |
|
| 292 | + return $this; |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | 295 | /** |
| 296 | 296 | * Apply multiple rules passed as a string. |
@@ -312,9 +312,9 @@ discard block |
||
| 312 | 312 | $rulename = substr($rule, 0, $colon) ; |
| 313 | 313 | |
| 314 | 314 | /** |
| 315 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 316 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 317 | - */ |
|
| 315 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 316 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 317 | + */ |
|
| 318 | 318 | if ($rulename !== 'regex') { |
| 319 | 319 | $parameters = str_getcsv(substr($rule, $colon + 1), ',', '"', "\\"); |
| 320 | 320 | } else { |
@@ -336,159 +336,159 @@ discard block |
||
| 336 | 336 | return $this; |
| 337 | 337 | } |
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Adds a label to the group/field |
|
| 341 | - * |
|
| 342 | - * @param string $text A label |
|
| 343 | - * @param array $attributes The label's attributes |
|
| 344 | - * |
|
| 345 | - * @return Field A field |
|
| 346 | - */ |
|
| 347 | - public function label($text, $attributes = array()) |
|
| 348 | - { |
|
| 349 | - // Create the Label element |
|
| 350 | - $for = $this->id ?: $this->name; |
|
| 351 | - $label = $this->app['former']->label($text, $for, $attributes); |
|
| 352 | - |
|
| 353 | - // Set label |
|
| 354 | - $this->label = $label; |
|
| 355 | - if ($this->group) { |
|
| 356 | - $this->group->setLabel($label); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return $this; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Set the Field value no matter what |
|
| 364 | - * |
|
| 365 | - * @param string $value A new value |
|
| 366 | - */ |
|
| 367 | - public function forceValue($value) |
|
| 368 | - { |
|
| 369 | - $this->value = $value; |
|
| 370 | - |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 376 | - * |
|
| 377 | - * @param string $value A new value |
|
| 378 | - */ |
|
| 379 | - public function value($value) |
|
| 380 | - { |
|
| 381 | - // Check if we already have a value stored for this field or in POST data |
|
| 382 | - $already = $this->repopulate(); |
|
| 383 | - |
|
| 384 | - if (!$already) { |
|
| 385 | - $this->value = $value; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - return $this; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Change the field's name |
|
| 393 | - * |
|
| 394 | - * @param string $name The new name |
|
| 395 | - */ |
|
| 396 | - public function name($name) |
|
| 397 | - { |
|
| 398 | - $this->name = $name; |
|
| 399 | - |
|
| 400 | - // Also relink the label to the new name |
|
| 401 | - $this->label($name); |
|
| 402 | - |
|
| 403 | - return $this; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Set the field as floating label |
|
| 408 | - */ |
|
| 409 | - public function floatingLabel($isFloatingLabel = true) |
|
| 410 | - { |
|
| 411 | - $this->floatingLabel = $isFloatingLabel; |
|
| 412 | - |
|
| 413 | - return $this; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Get the field's labels |
|
| 418 | - * |
|
| 419 | - * @return string |
|
| 420 | - */ |
|
| 421 | - public function getLabel() |
|
| 422 | - { |
|
| 423 | - return $this->label; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Change the field's bind destination |
|
| 428 | - * |
|
| 429 | - * @param $destination |
|
| 430 | - */ |
|
| 431 | - public function bind($destination) { |
|
| 432 | - $this->bind = $destination; |
|
| 433 | - if ($this->type != 'password') { |
|
| 434 | - $this->value = $this->repopulate(); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - return $this; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - //////////////////////////////////////////////////////////////////// |
|
| 441 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 442 | - //////////////////////////////////////////////////////////////////// |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Use values stored in Former to populate the current field |
|
| 446 | - */ |
|
| 447 | - private function repopulate($fallback = null) |
|
| 448 | - { |
|
| 449 | - // Get values from POST, populated, and manually set value |
|
| 450 | - $post = $this->app['former']->getPost($this->name); |
|
| 451 | - $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 452 | - $populate = $populator->get($this->bind ?: $this->name); |
|
| 453 | - |
|
| 454 | - // Assign a priority to each |
|
| 455 | - if (!is_null($post)) { |
|
| 456 | - return $post; |
|
| 457 | - } |
|
| 458 | - if (!is_null($populate)) { |
|
| 459 | - return $populate; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - return $fallback ?: $this->value; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Ponders a label and a field name, and tries to get the best out of it |
|
| 467 | - * |
|
| 468 | - * @param string $label A label |
|
| 469 | - * @param string $name A field name |
|
| 470 | - * |
|
| 471 | - * @return false|null A label and a field name |
|
| 472 | - */ |
|
| 473 | - private function automaticLabels($name, $label) |
|
| 474 | - { |
|
| 475 | - // Disabled automatic labels |
|
| 476 | - if (!$this->app['former']->getOption('automatic_label')) { |
|
| 477 | - $this->name = $name; |
|
| 478 | - $this->label($label); |
|
| 479 | - |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - // Check for the two possibilities |
|
| 484 | - if ($label and is_null($name)) { |
|
| 485 | - $name = Str::slug($label); |
|
| 486 | - } elseif (is_null($label) and $name) { |
|
| 487 | - $label = preg_replace('/\[\]$/', '', $name); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - // Save values |
|
| 491 | - $this->name = $name; |
|
| 492 | - $this->label($label); |
|
| 493 | - } |
|
| 339 | + /** |
|
| 340 | + * Adds a label to the group/field |
|
| 341 | + * |
|
| 342 | + * @param string $text A label |
|
| 343 | + * @param array $attributes The label's attributes |
|
| 344 | + * |
|
| 345 | + * @return Field A field |
|
| 346 | + */ |
|
| 347 | + public function label($text, $attributes = array()) |
|
| 348 | + { |
|
| 349 | + // Create the Label element |
|
| 350 | + $for = $this->id ?: $this->name; |
|
| 351 | + $label = $this->app['former']->label($text, $for, $attributes); |
|
| 352 | + |
|
| 353 | + // Set label |
|
| 354 | + $this->label = $label; |
|
| 355 | + if ($this->group) { |
|
| 356 | + $this->group->setLabel($label); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return $this; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Set the Field value no matter what |
|
| 364 | + * |
|
| 365 | + * @param string $value A new value |
|
| 366 | + */ |
|
| 367 | + public function forceValue($value) |
|
| 368 | + { |
|
| 369 | + $this->value = $value; |
|
| 370 | + |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 376 | + * |
|
| 377 | + * @param string $value A new value |
|
| 378 | + */ |
|
| 379 | + public function value($value) |
|
| 380 | + { |
|
| 381 | + // Check if we already have a value stored for this field or in POST data |
|
| 382 | + $already = $this->repopulate(); |
|
| 383 | + |
|
| 384 | + if (!$already) { |
|
| 385 | + $this->value = $value; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + return $this; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Change the field's name |
|
| 393 | + * |
|
| 394 | + * @param string $name The new name |
|
| 395 | + */ |
|
| 396 | + public function name($name) |
|
| 397 | + { |
|
| 398 | + $this->name = $name; |
|
| 399 | + |
|
| 400 | + // Also relink the label to the new name |
|
| 401 | + $this->label($name); |
|
| 402 | + |
|
| 403 | + return $this; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Set the field as floating label |
|
| 408 | + */ |
|
| 409 | + public function floatingLabel($isFloatingLabel = true) |
|
| 410 | + { |
|
| 411 | + $this->floatingLabel = $isFloatingLabel; |
|
| 412 | + |
|
| 413 | + return $this; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Get the field's labels |
|
| 418 | + * |
|
| 419 | + * @return string |
|
| 420 | + */ |
|
| 421 | + public function getLabel() |
|
| 422 | + { |
|
| 423 | + return $this->label; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Change the field's bind destination |
|
| 428 | + * |
|
| 429 | + * @param $destination |
|
| 430 | + */ |
|
| 431 | + public function bind($destination) { |
|
| 432 | + $this->bind = $destination; |
|
| 433 | + if ($this->type != 'password') { |
|
| 434 | + $this->value = $this->repopulate(); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + return $this; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + //////////////////////////////////////////////////////////////////// |
|
| 441 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 442 | + //////////////////////////////////////////////////////////////////// |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Use values stored in Former to populate the current field |
|
| 446 | + */ |
|
| 447 | + private function repopulate($fallback = null) |
|
| 448 | + { |
|
| 449 | + // Get values from POST, populated, and manually set value |
|
| 450 | + $post = $this->app['former']->getPost($this->name); |
|
| 451 | + $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 452 | + $populate = $populator->get($this->bind ?: $this->name); |
|
| 453 | + |
|
| 454 | + // Assign a priority to each |
|
| 455 | + if (!is_null($post)) { |
|
| 456 | + return $post; |
|
| 457 | + } |
|
| 458 | + if (!is_null($populate)) { |
|
| 459 | + return $populate; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + return $fallback ?: $this->value; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Ponders a label and a field name, and tries to get the best out of it |
|
| 467 | + * |
|
| 468 | + * @param string $label A label |
|
| 469 | + * @param string $name A field name |
|
| 470 | + * |
|
| 471 | + * @return false|null A label and a field name |
|
| 472 | + */ |
|
| 473 | + private function automaticLabels($name, $label) |
|
| 474 | + { |
|
| 475 | + // Disabled automatic labels |
|
| 476 | + if (!$this->app['former']->getOption('automatic_label')) { |
|
| 477 | + $this->name = $name; |
|
| 478 | + $this->label($label); |
|
| 479 | + |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + // Check for the two possibilities |
|
| 484 | + if ($label and is_null($name)) { |
|
| 485 | + $name = Str::slug($label); |
|
| 486 | + } elseif (is_null($label) and $name) { |
|
| 487 | + $label = preg_replace('/\[\]$/', '', $name); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + // Save values |
|
| 491 | + $this->name = $name; |
|
| 492 | + $this->label($label); |
|
| 493 | + } |
|
| 494 | 494 | } |
@@ -13,484 +13,484 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap5 extends Framework implements FrameworkInterface |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Form types that trigger special styling for this Framework |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'lg', |
|
| 30 | - 'sm', |
|
| 31 | - 'xs', |
|
| 32 | - 'block', |
|
| 33 | - 'link', |
|
| 34 | - 'primary', |
|
| 35 | - 'secondary', |
|
| 36 | - 'warning', |
|
| 37 | - 'danger', |
|
| 38 | - 'success', |
|
| 39 | - 'info', |
|
| 40 | - 'light', |
|
| 41 | - 'dark', |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The field sizes available |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - private $fields = array( |
|
| 50 | - 'lg', |
|
| 51 | - 'sm', |
|
| 52 | - // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | - // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | - // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | - // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | - // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | - // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | - // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | - // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | - ); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The field states available |
|
| 64 | - * |
|
| 65 | - * @var array |
|
| 66 | - */ |
|
| 67 | - protected $states = array( |
|
| 68 | - 'is-invalid', |
|
| 69 | - ); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The default HTML tag used for icons |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $iconTag = 'i'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * The default set for icon fonts |
|
| 80 | - * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | - * |
|
| 82 | - * @var string |
|
| 83 | - */ |
|
| 84 | - protected $iconSet = 'fa'; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * The default prefix icon names |
|
| 88 | - * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | - * |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 92 | - protected $iconPrefix = 'fa'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Create a new TwitterBootstrap instance |
|
| 96 | - * |
|
| 97 | - * @param \Illuminate\Container\Container $app |
|
| 98 | - */ |
|
| 99 | - public function __construct(Container $app) |
|
| 100 | - { |
|
| 101 | - $this->app = $app; |
|
| 102 | - $this->setFrameworkDefaults(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - //////////////////////////////////////////////////////////////////// |
|
| 106 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Filter buttons classes |
|
| 111 | - * |
|
| 112 | - * @param array $classes An array of classes |
|
| 113 | - * |
|
| 114 | - * @return string[] A filtered array |
|
| 115 | - */ |
|
| 116 | - public function filterButtonClasses($classes) |
|
| 117 | - { |
|
| 118 | - // Filter classes |
|
| 119 | - // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | - |
|
| 121 | - // Prepend button type |
|
| 122 | - $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | - $classes[] = 'btn'; |
|
| 124 | - |
|
| 125 | - return $classes; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Filter field classes |
|
| 130 | - * |
|
| 131 | - * @param array $classes An array of classes |
|
| 132 | - * |
|
| 133 | - * @return array A filtered array |
|
| 134 | - */ |
|
| 135 | - public function filterFieldClasses($classes) |
|
| 136 | - { |
|
| 137 | - // Filter classes |
|
| 138 | - $classes = array_intersect($classes, $this->fields); |
|
| 139 | - |
|
| 140 | - // Prepend field type |
|
| 141 | - $classes = array_map(function ($class) { |
|
| 142 | - return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | - }, $classes); |
|
| 144 | - |
|
| 145 | - return $classes; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | - //////////////////////////////////////////////////////////////////// |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Framework error state |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function errorState() |
|
| 158 | - { |
|
| 159 | - return 'is-invalid'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns corresponding inline class of a field |
|
| 164 | - * |
|
| 165 | - * @param Field $field |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function getInlineLabelClass($field) |
|
| 170 | - { |
|
| 171 | - $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | - if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | - $inlineClass = 'form-check-label'; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $inlineClass; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Set the fields width from a label width |
|
| 181 | - * |
|
| 182 | - * @param array $labelWidths |
|
| 183 | - */ |
|
| 184 | - protected function setFieldWidths($labelWidths) |
|
| 185 | - { |
|
| 186 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | - |
|
| 188 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | - if ($viewport) { |
|
| 191 | - $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | - $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | - $fieldOffsetClass .= " offset-$viewports[$viewport]-$columns"; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | - $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | - $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - //////////////////////////////////////////////////////////////////// |
|
| 203 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Add classes to a field |
|
| 208 | - * |
|
| 209 | - * @param Field $field |
|
| 210 | - * @param array $classes The possible classes to add |
|
| 211 | - * |
|
| 212 | - * @return Field |
|
| 213 | - */ |
|
| 214 | - public function getFieldClasses(Field $field, $classes) |
|
| 215 | - { |
|
| 216 | - // Add inline class for checkables |
|
| 217 | - if ($field->isCheckable()) { |
|
| 218 | - // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | - $field->addClass('form-check-input'); |
|
| 220 | - $classes[] = 'form-check'; |
|
| 221 | - |
|
| 222 | - if (in_array('inline', $classes)) { |
|
| 223 | - $field->inline(); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Filter classes according to field type |
|
| 228 | - if ($field->isButton()) { |
|
| 229 | - $classes = $this->filterButtonClasses($classes); |
|
| 230 | - } else { |
|
| 231 | - $classes = $this->filterFieldClasses($classes); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Add form-control class for text-type, textarea and file fields |
|
| 235 | - // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | - if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [ |
|
| 237 | - 'plaintext', |
|
| 238 | - 'select', |
|
| 239 | - ]) && !in_array('form-control', $classes) |
|
| 240 | - ) { |
|
| 241 | - $classes[] = 'form-control'; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // Add form-select class for select fields |
|
| 245 | - if ($field->getType() === 'select' && !in_array('form-select', $classes)) { |
|
| 246 | - $classes[] = 'form-select'; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ($this->app['former']->getErrors($field->getName())) { |
|
| 250 | - $classes[] = $this->errorState(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this->addClassesToField($field, $classes); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Add group classes |
|
| 258 | - * |
|
| 259 | - * @return string A list of group classes |
|
| 260 | - */ |
|
| 261 | - public function getGroupClasses() |
|
| 262 | - { |
|
| 263 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 264 | - return 'mb-3 row'; |
|
| 265 | - } else { |
|
| 266 | - return 'mb-3'; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Add label classes |
|
| 272 | - * |
|
| 273 | - * @return string[] An array of attributes with the label class |
|
| 274 | - */ |
|
| 275 | - public function getLabelClasses() |
|
| 276 | - { |
|
| 277 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | - return array('col-form-label', $this->labelWidth); |
|
| 279 | - } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 280 | - return array('visually-hidden'); |
|
| 281 | - } else { |
|
| 282 | - return array('form-label'); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Add uneditable field classes |
|
| 288 | - * |
|
| 289 | - * @return string An array of attributes with the uneditable class |
|
| 290 | - */ |
|
| 291 | - public function getUneditableClasses() |
|
| 292 | - { |
|
| 293 | - return ''; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Add plain text field classes |
|
| 298 | - * |
|
| 299 | - * @return string An array of attributes with the plain text class |
|
| 300 | - */ |
|
| 301 | - public function getPlainTextClasses() |
|
| 302 | - { |
|
| 303 | - return 'form-control-plaintext'; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Add form class |
|
| 308 | - * |
|
| 309 | - * @param string $type The type of form to add |
|
| 310 | - * |
|
| 311 | - * @return string|null |
|
| 312 | - */ |
|
| 313 | - public function getFormClasses($type) |
|
| 314 | - { |
|
| 315 | - return $type ? 'form-'.$type : null; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Add actions block class |
|
| 320 | - * |
|
| 321 | - * @return string|null |
|
| 322 | - */ |
|
| 323 | - public function getActionClasses() |
|
| 324 | - { |
|
| 325 | - if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 326 | - return 'mb-3 row'; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - return null; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Add floating label class |
|
| 334 | - * |
|
| 335 | - * @return string Get the floating label class |
|
| 336 | - */ |
|
| 337 | - public function getFloatingLabelClass() |
|
| 338 | - { |
|
| 339 | - return 'form-floating'; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - //////////////////////////////////////////////////////////////////// |
|
| 343 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 344 | - //////////////////////////////////////////////////////////////////// |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Render an help text |
|
| 348 | - * |
|
| 349 | - * @param string $text |
|
| 350 | - * @param array $attributes |
|
| 351 | - * |
|
| 352 | - * @return Element |
|
| 353 | - */ |
|
| 354 | - public function createHelp($text, $attributes = array()) |
|
| 355 | - { |
|
| 356 | - return Element::create('span', $text, $attributes)->addClass('form-text'); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Render an validation error text |
|
| 361 | - * |
|
| 362 | - * @param string $text |
|
| 363 | - * @param array $attributes |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - public function createValidationError($text, $attributes = array()) |
|
| 368 | - { |
|
| 369 | - return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Render an help text |
|
| 374 | - * |
|
| 375 | - * @param string $text |
|
| 376 | - * @param array $attributes |
|
| 377 | - * |
|
| 378 | - * @return Element |
|
| 379 | - */ |
|
| 380 | - public function createBlockHelp($text, $attributes = array()) |
|
| 381 | - { |
|
| 382 | - return Element::create('div', $text, $attributes)->addClass('form-text'); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Render a disabled field |
|
| 387 | - * |
|
| 388 | - * @param Field $field |
|
| 389 | - * |
|
| 390 | - * @return Element |
|
| 391 | - */ |
|
| 392 | - public function createDisabledField(Field $field) |
|
| 393 | - { |
|
| 394 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Render a plain text field |
|
| 399 | - * |
|
| 400 | - * @param Field $field |
|
| 401 | - * |
|
| 402 | - * @return Element |
|
| 403 | - */ |
|
| 404 | - public function createPlainTextField(Field $field) |
|
| 405 | - { |
|
| 406 | - $label = $field->getLabel(); |
|
| 407 | - if ($label) { |
|
| 408 | - $label->for(''); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - //////////////////////////////////////////////////////////////////// |
|
| 415 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 416 | - //////////////////////////////////////////////////////////////////// |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Wrap an item to be prepended or appended to the current field |
|
| 420 | - * |
|
| 421 | - * @return Element A wrapped item |
|
| 422 | - */ |
|
| 423 | - public function placeAround($item, $place = null) |
|
| 424 | - { |
|
| 425 | - // Render object |
|
| 426 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 427 | - $item = $item->__toString(); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - $items = (array) $item; |
|
| 431 | - $element = ''; |
|
| 432 | - foreach ($items as $item) { |
|
| 433 | - $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 434 | - |
|
| 435 | - // Get class to use |
|
| 436 | - $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 437 | - |
|
| 438 | - $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - return $element; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Wrap a field with prepended and appended items |
|
| 446 | - * |
|
| 447 | - * @param Field $field |
|
| 448 | - * @param array $prepend |
|
| 449 | - * @param array $append |
|
| 450 | - * |
|
| 451 | - * @return string A field concatented with prepended and/or appended items |
|
| 452 | - */ |
|
| 453 | - public function prependAppend($field, $prepend, $append) |
|
| 454 | - { |
|
| 455 | - $return = '<div class="input-group">'; |
|
| 456 | - $return .= implode('', $prepend); |
|
| 457 | - $return .= $field->render(); |
|
| 458 | - $return .= implode('', $append); |
|
| 459 | - $return .= '</div>'; |
|
| 460 | - |
|
| 461 | - return $return; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Wrap a field with potential additional tags |
|
| 466 | - * |
|
| 467 | - * @param Field $field |
|
| 468 | - * |
|
| 469 | - * @return Element A wrapped field |
|
| 470 | - */ |
|
| 471 | - public function wrapField($field) |
|
| 472 | - { |
|
| 473 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 474 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - return $field; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Wrap actions block with potential additional tags |
|
| 482 | - * |
|
| 483 | - * @param Actions $actions |
|
| 484 | - * |
|
| 485 | - * @return string A wrapped actions block |
|
| 486 | - */ |
|
| 487 | - public function wrapActions($actions) |
|
| 488 | - { |
|
| 489 | - // For horizontal forms, we wrap the actions in a div |
|
| 490 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 491 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - return $actions; |
|
| 495 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'lg', |
|
| 30 | + 'sm', |
|
| 31 | + 'xs', |
|
| 32 | + 'block', |
|
| 33 | + 'link', |
|
| 34 | + 'primary', |
|
| 35 | + 'secondary', |
|
| 36 | + 'warning', |
|
| 37 | + 'danger', |
|
| 38 | + 'success', |
|
| 39 | + 'info', |
|
| 40 | + 'light', |
|
| 41 | + 'dark', |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The field sizes available |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + private $fields = array( |
|
| 50 | + 'lg', |
|
| 51 | + 'sm', |
|
| 52 | + // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | + // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | + // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | + // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | + // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | + // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | + // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | + // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | + ); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The field states available |
|
| 64 | + * |
|
| 65 | + * @var array |
|
| 66 | + */ |
|
| 67 | + protected $states = array( |
|
| 68 | + 'is-invalid', |
|
| 69 | + ); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The default HTML tag used for icons |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $iconTag = 'i'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * The default set for icon fonts |
|
| 80 | + * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | + * |
|
| 82 | + * @var string |
|
| 83 | + */ |
|
| 84 | + protected $iconSet = 'fa'; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * The default prefix icon names |
|
| 88 | + * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | + * |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | + protected $iconPrefix = 'fa'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Create a new TwitterBootstrap instance |
|
| 96 | + * |
|
| 97 | + * @param \Illuminate\Container\Container $app |
|
| 98 | + */ |
|
| 99 | + public function __construct(Container $app) |
|
| 100 | + { |
|
| 101 | + $this->app = $app; |
|
| 102 | + $this->setFrameworkDefaults(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + //////////////////////////////////////////////////////////////////// |
|
| 106 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Filter buttons classes |
|
| 111 | + * |
|
| 112 | + * @param array $classes An array of classes |
|
| 113 | + * |
|
| 114 | + * @return string[] A filtered array |
|
| 115 | + */ |
|
| 116 | + public function filterButtonClasses($classes) |
|
| 117 | + { |
|
| 118 | + // Filter classes |
|
| 119 | + // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | + |
|
| 121 | + // Prepend button type |
|
| 122 | + $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | + $classes[] = 'btn'; |
|
| 124 | + |
|
| 125 | + return $classes; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Filter field classes |
|
| 130 | + * |
|
| 131 | + * @param array $classes An array of classes |
|
| 132 | + * |
|
| 133 | + * @return array A filtered array |
|
| 134 | + */ |
|
| 135 | + public function filterFieldClasses($classes) |
|
| 136 | + { |
|
| 137 | + // Filter classes |
|
| 138 | + $classes = array_intersect($classes, $this->fields); |
|
| 139 | + |
|
| 140 | + // Prepend field type |
|
| 141 | + $classes = array_map(function ($class) { |
|
| 142 | + return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | + }, $classes); |
|
| 144 | + |
|
| 145 | + return $classes; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | + //////////////////////////////////////////////////////////////////// |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Framework error state |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function errorState() |
|
| 158 | + { |
|
| 159 | + return 'is-invalid'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns corresponding inline class of a field |
|
| 164 | + * |
|
| 165 | + * @param Field $field |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function getInlineLabelClass($field) |
|
| 170 | + { |
|
| 171 | + $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | + if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | + $inlineClass = 'form-check-label'; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $inlineClass; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Set the fields width from a label width |
|
| 181 | + * |
|
| 182 | + * @param array $labelWidths |
|
| 183 | + */ |
|
| 184 | + protected function setFieldWidths($labelWidths) |
|
| 185 | + { |
|
| 186 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | + |
|
| 188 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | + if ($viewport) { |
|
| 191 | + $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | + $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | + $fieldOffsetClass .= " offset-$viewports[$viewport]-$columns"; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | + $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | + $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + //////////////////////////////////////////////////////////////////// |
|
| 203 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Add classes to a field |
|
| 208 | + * |
|
| 209 | + * @param Field $field |
|
| 210 | + * @param array $classes The possible classes to add |
|
| 211 | + * |
|
| 212 | + * @return Field |
|
| 213 | + */ |
|
| 214 | + public function getFieldClasses(Field $field, $classes) |
|
| 215 | + { |
|
| 216 | + // Add inline class for checkables |
|
| 217 | + if ($field->isCheckable()) { |
|
| 218 | + // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | + $field->addClass('form-check-input'); |
|
| 220 | + $classes[] = 'form-check'; |
|
| 221 | + |
|
| 222 | + if (in_array('inline', $classes)) { |
|
| 223 | + $field->inline(); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Filter classes according to field type |
|
| 228 | + if ($field->isButton()) { |
|
| 229 | + $classes = $this->filterButtonClasses($classes); |
|
| 230 | + } else { |
|
| 231 | + $classes = $this->filterFieldClasses($classes); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Add form-control class for text-type, textarea and file fields |
|
| 235 | + // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | + if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [ |
|
| 237 | + 'plaintext', |
|
| 238 | + 'select', |
|
| 239 | + ]) && !in_array('form-control', $classes) |
|
| 240 | + ) { |
|
| 241 | + $classes[] = 'form-control'; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // Add form-select class for select fields |
|
| 245 | + if ($field->getType() === 'select' && !in_array('form-select', $classes)) { |
|
| 246 | + $classes[] = 'form-select'; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ($this->app['former']->getErrors($field->getName())) { |
|
| 250 | + $classes[] = $this->errorState(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this->addClassesToField($field, $classes); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Add group classes |
|
| 258 | + * |
|
| 259 | + * @return string A list of group classes |
|
| 260 | + */ |
|
| 261 | + public function getGroupClasses() |
|
| 262 | + { |
|
| 263 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 264 | + return 'mb-3 row'; |
|
| 265 | + } else { |
|
| 266 | + return 'mb-3'; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Add label classes |
|
| 272 | + * |
|
| 273 | + * @return string[] An array of attributes with the label class |
|
| 274 | + */ |
|
| 275 | + public function getLabelClasses() |
|
| 276 | + { |
|
| 277 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | + return array('col-form-label', $this->labelWidth); |
|
| 279 | + } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 280 | + return array('visually-hidden'); |
|
| 281 | + } else { |
|
| 282 | + return array('form-label'); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Add uneditable field classes |
|
| 288 | + * |
|
| 289 | + * @return string An array of attributes with the uneditable class |
|
| 290 | + */ |
|
| 291 | + public function getUneditableClasses() |
|
| 292 | + { |
|
| 293 | + return ''; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Add plain text field classes |
|
| 298 | + * |
|
| 299 | + * @return string An array of attributes with the plain text class |
|
| 300 | + */ |
|
| 301 | + public function getPlainTextClasses() |
|
| 302 | + { |
|
| 303 | + return 'form-control-plaintext'; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Add form class |
|
| 308 | + * |
|
| 309 | + * @param string $type The type of form to add |
|
| 310 | + * |
|
| 311 | + * @return string|null |
|
| 312 | + */ |
|
| 313 | + public function getFormClasses($type) |
|
| 314 | + { |
|
| 315 | + return $type ? 'form-'.$type : null; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Add actions block class |
|
| 320 | + * |
|
| 321 | + * @return string|null |
|
| 322 | + */ |
|
| 323 | + public function getActionClasses() |
|
| 324 | + { |
|
| 325 | + if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 326 | + return 'mb-3 row'; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + return null; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Add floating label class |
|
| 334 | + * |
|
| 335 | + * @return string Get the floating label class |
|
| 336 | + */ |
|
| 337 | + public function getFloatingLabelClass() |
|
| 338 | + { |
|
| 339 | + return 'form-floating'; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + //////////////////////////////////////////////////////////////////// |
|
| 343 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 344 | + //////////////////////////////////////////////////////////////////// |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Render an help text |
|
| 348 | + * |
|
| 349 | + * @param string $text |
|
| 350 | + * @param array $attributes |
|
| 351 | + * |
|
| 352 | + * @return Element |
|
| 353 | + */ |
|
| 354 | + public function createHelp($text, $attributes = array()) |
|
| 355 | + { |
|
| 356 | + return Element::create('span', $text, $attributes)->addClass('form-text'); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Render an validation error text |
|
| 361 | + * |
|
| 362 | + * @param string $text |
|
| 363 | + * @param array $attributes |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + public function createValidationError($text, $attributes = array()) |
|
| 368 | + { |
|
| 369 | + return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Render an help text |
|
| 374 | + * |
|
| 375 | + * @param string $text |
|
| 376 | + * @param array $attributes |
|
| 377 | + * |
|
| 378 | + * @return Element |
|
| 379 | + */ |
|
| 380 | + public function createBlockHelp($text, $attributes = array()) |
|
| 381 | + { |
|
| 382 | + return Element::create('div', $text, $attributes)->addClass('form-text'); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Render a disabled field |
|
| 387 | + * |
|
| 388 | + * @param Field $field |
|
| 389 | + * |
|
| 390 | + * @return Element |
|
| 391 | + */ |
|
| 392 | + public function createDisabledField(Field $field) |
|
| 393 | + { |
|
| 394 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Render a plain text field |
|
| 399 | + * |
|
| 400 | + * @param Field $field |
|
| 401 | + * |
|
| 402 | + * @return Element |
|
| 403 | + */ |
|
| 404 | + public function createPlainTextField(Field $field) |
|
| 405 | + { |
|
| 406 | + $label = $field->getLabel(); |
|
| 407 | + if ($label) { |
|
| 408 | + $label->for(''); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + //////////////////////////////////////////////////////////////////// |
|
| 415 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 416 | + //////////////////////////////////////////////////////////////////// |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Wrap an item to be prepended or appended to the current field |
|
| 420 | + * |
|
| 421 | + * @return Element A wrapped item |
|
| 422 | + */ |
|
| 423 | + public function placeAround($item, $place = null) |
|
| 424 | + { |
|
| 425 | + // Render object |
|
| 426 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 427 | + $item = $item->__toString(); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + $items = (array) $item; |
|
| 431 | + $element = ''; |
|
| 432 | + foreach ($items as $item) { |
|
| 433 | + $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 434 | + |
|
| 435 | + // Get class to use |
|
| 436 | + $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 437 | + |
|
| 438 | + $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + return $element; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Wrap a field with prepended and appended items |
|
| 446 | + * |
|
| 447 | + * @param Field $field |
|
| 448 | + * @param array $prepend |
|
| 449 | + * @param array $append |
|
| 450 | + * |
|
| 451 | + * @return string A field concatented with prepended and/or appended items |
|
| 452 | + */ |
|
| 453 | + public function prependAppend($field, $prepend, $append) |
|
| 454 | + { |
|
| 455 | + $return = '<div class="input-group">'; |
|
| 456 | + $return .= implode('', $prepend); |
|
| 457 | + $return .= $field->render(); |
|
| 458 | + $return .= implode('', $append); |
|
| 459 | + $return .= '</div>'; |
|
| 460 | + |
|
| 461 | + return $return; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Wrap a field with potential additional tags |
|
| 466 | + * |
|
| 467 | + * @param Field $field |
|
| 468 | + * |
|
| 469 | + * @return Element A wrapped field |
|
| 470 | + */ |
|
| 471 | + public function wrapField($field) |
|
| 472 | + { |
|
| 473 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 474 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + return $field; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Wrap actions block with potential additional tags |
|
| 482 | + * |
|
| 483 | + * @param Actions $actions |
|
| 484 | + * |
|
| 485 | + * @return string A wrapped actions block |
|
| 486 | + */ |
|
| 487 | + public function wrapActions($actions) |
|
| 488 | + { |
|
| 489 | + // For horizontal forms, we wrap the actions in a div |
|
| 490 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 491 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + return $actions; |
|
| 495 | + } |
|
| 496 | 496 | } |