@@ -12,106 +12,106 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Elements |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * The Container |
|
| 17 | - * |
|
| 18 | - * @var Container |
|
| 19 | - */ |
|
| 20 | - protected $app; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The Session instance |
|
| 24 | - * |
|
| 25 | - * @var Session |
|
| 26 | - */ |
|
| 27 | - protected $session; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Build a new Element |
|
| 31 | - * |
|
| 32 | - * @param Container $app |
|
| 33 | - */ |
|
| 34 | - public function __construct(Container $app, $session) |
|
| 35 | - { |
|
| 36 | - $this->app = $app; |
|
| 37 | - $this->session = $session; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Generate a hidden field containing the current CSRF token. |
|
| 42 | - * |
|
| 43 | - * @return string |
|
| 44 | - */ |
|
| 45 | - public function token() |
|
| 46 | - { |
|
| 47 | - $csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token(); |
|
| 48 | - |
|
| 49 | - return (string) $this->app['former']->hidden('_token', $csrf); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Creates a label tag |
|
| 54 | - * |
|
| 55 | - * @param string $label The label content |
|
| 56 | - * @param string $for The field the label's for |
|
| 57 | - * @param array $attributes The label's attributes |
|
| 58 | - * |
|
| 59 | - * @return Element A <label> tag |
|
| 60 | - */ |
|
| 61 | - public function label($label, $for = null, $attributes = array()) |
|
| 62 | - { |
|
| 63 | - if (!$label instanceof Htmlable) { |
|
| 64 | - $oldLabel = (string) $label; |
|
| 65 | - $label = Helpers::translate($oldLabel); |
|
| 66 | - |
|
| 67 | - // If there was no change to the label, |
|
| 68 | - // then a Laravel translation did not occur |
|
| 69 | - if (lcfirst($label) == $oldLabel) { |
|
| 70 | - $label = str_replace('_', ' ', $label); |
|
| 71 | - } |
|
| 72 | - } else { |
|
| 73 | - $label = (string) $label->toHtml(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $attributes['for'] = $for; |
|
| 77 | - $this->app['former']->labels[] = $for; |
|
| 78 | - |
|
| 79 | - return Element::create('label', $label, $attributes); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Creates a form legend |
|
| 84 | - * |
|
| 85 | - * @param string $legend The text |
|
| 86 | - * @param array $attributes Its attributes |
|
| 87 | - * |
|
| 88 | - * @return Element A <legend> tag |
|
| 89 | - */ |
|
| 90 | - public function legend($legend, $attributes = array()) |
|
| 91 | - { |
|
| 92 | - $legend = Helpers::translate($legend); |
|
| 93 | - |
|
| 94 | - return Element::create('legend', $legend, $attributes); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Close a field group |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function closeGroup() |
|
| 103 | - { |
|
| 104 | - $closing = ''; |
|
| 105 | - if (Group::$opened && isset(Group::$openGroup)) { |
|
| 106 | - $closing = Group::$openGroup->close(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Close custom group |
|
| 110 | - Group::$opened = false; |
|
| 111 | - |
|
| 112 | - // Reset custom group reference |
|
| 113 | - Group::$openGroup = null; |
|
| 114 | - |
|
| 115 | - return $closing; |
|
| 116 | - } |
|
| 15 | + /** |
|
| 16 | + * The Container |
|
| 17 | + * |
|
| 18 | + * @var Container |
|
| 19 | + */ |
|
| 20 | + protected $app; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The Session instance |
|
| 24 | + * |
|
| 25 | + * @var Session |
|
| 26 | + */ |
|
| 27 | + protected $session; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Build a new Element |
|
| 31 | + * |
|
| 32 | + * @param Container $app |
|
| 33 | + */ |
|
| 34 | + public function __construct(Container $app, $session) |
|
| 35 | + { |
|
| 36 | + $this->app = $app; |
|
| 37 | + $this->session = $session; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Generate a hidden field containing the current CSRF token. |
|
| 42 | + * |
|
| 43 | + * @return string |
|
| 44 | + */ |
|
| 45 | + public function token() |
|
| 46 | + { |
|
| 47 | + $csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token(); |
|
| 48 | + |
|
| 49 | + return (string) $this->app['former']->hidden('_token', $csrf); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Creates a label tag |
|
| 54 | + * |
|
| 55 | + * @param string $label The label content |
|
| 56 | + * @param string $for The field the label's for |
|
| 57 | + * @param array $attributes The label's attributes |
|
| 58 | + * |
|
| 59 | + * @return Element A <label> tag |
|
| 60 | + */ |
|
| 61 | + public function label($label, $for = null, $attributes = array()) |
|
| 62 | + { |
|
| 63 | + if (!$label instanceof Htmlable) { |
|
| 64 | + $oldLabel = (string) $label; |
|
| 65 | + $label = Helpers::translate($oldLabel); |
|
| 66 | + |
|
| 67 | + // If there was no change to the label, |
|
| 68 | + // then a Laravel translation did not occur |
|
| 69 | + if (lcfirst($label) == $oldLabel) { |
|
| 70 | + $label = str_replace('_', ' ', $label); |
|
| 71 | + } |
|
| 72 | + } else { |
|
| 73 | + $label = (string) $label->toHtml(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $attributes['for'] = $for; |
|
| 77 | + $this->app['former']->labels[] = $for; |
|
| 78 | + |
|
| 79 | + return Element::create('label', $label, $attributes); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Creates a form legend |
|
| 84 | + * |
|
| 85 | + * @param string $legend The text |
|
| 86 | + * @param array $attributes Its attributes |
|
| 87 | + * |
|
| 88 | + * @return Element A <legend> tag |
|
| 89 | + */ |
|
| 90 | + public function legend($legend, $attributes = array()) |
|
| 91 | + { |
|
| 92 | + $legend = Helpers::translate($legend); |
|
| 93 | + |
|
| 94 | + return Element::create('legend', $legend, $attributes); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Close a field group |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function closeGroup() |
|
| 103 | + { |
|
| 104 | + $closing = ''; |
|
| 105 | + if (Group::$opened && isset(Group::$openGroup)) { |
|
| 106 | + $closing = Group::$openGroup->close(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Close custom group |
|
| 110 | + Group::$opened = false; |
|
| 111 | + |
|
| 112 | + // Reset custom group reference |
|
| 113 | + Group::$openGroup = null; |
|
| 114 | + |
|
| 115 | + return $closing; |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -16,264 +16,264 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Field extends FormerObject implements FieldInterface |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The IoC Container |
|
| 21 | - * |
|
| 22 | - * @var Container |
|
| 23 | - */ |
|
| 24 | - protected $app; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The Form instance |
|
| 28 | - * |
|
| 29 | - * @var Former\Form |
|
| 30 | - */ |
|
| 31 | - protected $form; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A label for the field (if not using Bootstrap) |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $label; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The field's group |
|
| 42 | - * |
|
| 43 | - * @var Group |
|
| 44 | - */ |
|
| 45 | - protected $group; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The field's default element |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $element = 'input'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Whether the Field is self-closing or not |
|
| 56 | - * |
|
| 57 | - * @var boolean |
|
| 58 | - */ |
|
| 59 | - protected $isSelfClosing = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The field's bind destination |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $bind; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Get the current framework instance |
|
| 70 | - * |
|
| 71 | - * @return Framework |
|
| 72 | - */ |
|
| 73 | - protected function currentFramework() |
|
| 74 | - { |
|
| 75 | - if ($this->app->bound('former.form.framework')) { |
|
| 76 | - return $this->app['former.form.framework']; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $this->app['former.framework']; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - ///////////////////////////// INTERFACE //////////////////////////// |
|
| 84 | - //////////////////////////////////////////////////////////////////// |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Set up a Field instance |
|
| 88 | - * |
|
| 89 | - * @param string $type A field type |
|
| 90 | - */ |
|
| 91 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 92 | - { |
|
| 93 | - // Set base parameters |
|
| 94 | - $this->app = $app; |
|
| 95 | - $this->type = $type; |
|
| 96 | - $this->value = $value; |
|
| 97 | - $this->setAttributes($attributes); |
|
| 98 | - $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 99 | - |
|
| 100 | - // Compute and translate label |
|
| 101 | - $this->automaticLabels($name, $label); |
|
| 102 | - |
|
| 103 | - // Repopulate field |
|
| 104 | - if ($type != 'password' && $name !== '_token') { |
|
| 105 | - $this->value = $this->repopulate(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Apply Live validation rules |
|
| 109 | - if ($this->app['former']->getOption('live_validation')) { |
|
| 110 | - $rules = new LiveValidation($this); |
|
| 111 | - $rules->apply($this->getRules()); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - // Bind the Group class |
|
| 115 | - $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 116 | - $groupClass = Former::FORMSPACE.$groupClass; |
|
| 117 | - |
|
| 118 | - $this->group = new $groupClass($this->app, $this->label); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Redirect calls to the group if necessary |
|
| 123 | - * |
|
| 124 | - * @param string $method |
|
| 125 | - */ |
|
| 126 | - public function __call($method, $parameters) |
|
| 127 | - { |
|
| 128 | - // Translate attributes |
|
| 129 | - $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 130 | - if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 131 | - $parameters[0] = Helpers::translate($parameters[0]); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Redirect calls to the Control Group |
|
| 135 | - if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) { |
|
| 136 | - $method = str_replace('onGroup', '', $method); |
|
| 137 | - $method = lcfirst($method); |
|
| 138 | - |
|
| 139 | - call_user_func_array(array($this->group, $method), $parameters); |
|
| 140 | - |
|
| 141 | - return $this; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return parent::__call($method, $parameters); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Prints out the field, wrapped in its group |
|
| 149 | - * |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function wrapAndRender() |
|
| 153 | - { |
|
| 154 | - // Dry syntax (hidden fields, plain fields) |
|
| 155 | - if ($this->isUnwrappable()) { |
|
| 156 | - $html = $this->render(); |
|
| 157 | - // Control group syntax |
|
| 158 | - } elseif (Form::hasInstanceOpened()) { |
|
| 159 | - $html = $this->group->wrapField($this); |
|
| 160 | - // Classic syntax |
|
| 161 | - } else { |
|
| 162 | - $html = $this->currentFramework()->createLabelOf($this); |
|
| 163 | - $html .= $this->render(); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return $html; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Prints out the field |
|
| 171 | - * |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - public function __toString() |
|
| 175 | - { |
|
| 176 | - return $this->wrapAndRender(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - //////////////////////////////////////////////////////////////////// |
|
| 180 | - ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 181 | - //////////////////////////////////////////////////////////////////// |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Whether the current field is required or not |
|
| 185 | - * |
|
| 186 | - * @return boolean |
|
| 187 | - */ |
|
| 188 | - public function isRequired() |
|
| 189 | - { |
|
| 190 | - return isset($this->attributes['required']); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Set required live validation attribute |
|
| 195 | - * |
|
| 196 | - * @param boolean $isRequired |
|
| 197 | - * @return $this |
|
| 198 | - */ |
|
| 199 | - public function required($isRequired=true) |
|
| 200 | - { |
|
| 201 | - if ($isRequired) { |
|
| 202 | - $this->attributes['required'] = true; |
|
| 203 | - } else { |
|
| 204 | - unset($this->attributes['required']); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $this; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Check if a field is unwrappable (no label) |
|
| 212 | - * |
|
| 213 | - * @return boolean |
|
| 214 | - */ |
|
| 215 | - public function isUnwrappable() |
|
| 216 | - { |
|
| 217 | - return |
|
| 218 | - ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 219 | - ($this->form and $this->isOfType('inline')) or |
|
| 220 | - $this->isButton() or |
|
| 221 | - $this->isOfType('hidden') or |
|
| 222 | - \Former\Form\Group::$opened or |
|
| 223 | - $this->group and $this->group->isRaw(); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Check if field is a checkbox or a radio |
|
| 228 | - * |
|
| 229 | - * @return boolean |
|
| 230 | - */ |
|
| 231 | - public function isCheckable() |
|
| 232 | - { |
|
| 233 | - return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios'); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Check if the field is a button |
|
| 238 | - * |
|
| 239 | - * @return boolean |
|
| 240 | - */ |
|
| 241 | - public function isButton() |
|
| 242 | - { |
|
| 243 | - return false; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the rules applied to the current field |
|
| 248 | - * |
|
| 249 | - * @return array An array of rules |
|
| 250 | - */ |
|
| 251 | - public function getRules() |
|
| 252 | - { |
|
| 253 | - return $this->app['former']->getRules($this->name); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - //////////////////////////////////////////////////////////////////// |
|
| 257 | - //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 258 | - //////////////////////////////////////////////////////////////////// |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Apply a Live Validation rule by chaining |
|
| 262 | - * |
|
| 263 | - * @param string $rule The rule |
|
| 264 | - */ |
|
| 265 | - public function rule($rule) |
|
| 266 | - { |
|
| 267 | - $parameters = func_get_args(); |
|
| 268 | - array_shift($parameters); |
|
| 269 | - |
|
| 270 | - $live = new LiveValidation($this); |
|
| 271 | - $live->apply(array( |
|
| 272 | - $rule => $parameters, |
|
| 273 | - )); |
|
| 274 | - |
|
| 275 | - return $this; |
|
| 276 | - } |
|
| 19 | + /** |
|
| 20 | + * The IoC Container |
|
| 21 | + * |
|
| 22 | + * @var Container |
|
| 23 | + */ |
|
| 24 | + protected $app; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The Form instance |
|
| 28 | + * |
|
| 29 | + * @var Former\Form |
|
| 30 | + */ |
|
| 31 | + protected $form; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A label for the field (if not using Bootstrap) |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $label; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The field's group |
|
| 42 | + * |
|
| 43 | + * @var Group |
|
| 44 | + */ |
|
| 45 | + protected $group; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The field's default element |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $element = 'input'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Whether the Field is self-closing or not |
|
| 56 | + * |
|
| 57 | + * @var boolean |
|
| 58 | + */ |
|
| 59 | + protected $isSelfClosing = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The field's bind destination |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $bind; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Get the current framework instance |
|
| 70 | + * |
|
| 71 | + * @return Framework |
|
| 72 | + */ |
|
| 73 | + protected function currentFramework() |
|
| 74 | + { |
|
| 75 | + if ($this->app->bound('former.form.framework')) { |
|
| 76 | + return $this->app['former.form.framework']; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $this->app['former.framework']; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + ///////////////////////////// INTERFACE //////////////////////////// |
|
| 84 | + //////////////////////////////////////////////////////////////////// |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Set up a Field instance |
|
| 88 | + * |
|
| 89 | + * @param string $type A field type |
|
| 90 | + */ |
|
| 91 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 92 | + { |
|
| 93 | + // Set base parameters |
|
| 94 | + $this->app = $app; |
|
| 95 | + $this->type = $type; |
|
| 96 | + $this->value = $value; |
|
| 97 | + $this->setAttributes($attributes); |
|
| 98 | + $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 99 | + |
|
| 100 | + // Compute and translate label |
|
| 101 | + $this->automaticLabels($name, $label); |
|
| 102 | + |
|
| 103 | + // Repopulate field |
|
| 104 | + if ($type != 'password' && $name !== '_token') { |
|
| 105 | + $this->value = $this->repopulate(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Apply Live validation rules |
|
| 109 | + if ($this->app['former']->getOption('live_validation')) { |
|
| 110 | + $rules = new LiveValidation($this); |
|
| 111 | + $rules->apply($this->getRules()); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // Bind the Group class |
|
| 115 | + $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 116 | + $groupClass = Former::FORMSPACE.$groupClass; |
|
| 117 | + |
|
| 118 | + $this->group = new $groupClass($this->app, $this->label); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Redirect calls to the group if necessary |
|
| 123 | + * |
|
| 124 | + * @param string $method |
|
| 125 | + */ |
|
| 126 | + public function __call($method, $parameters) |
|
| 127 | + { |
|
| 128 | + // Translate attributes |
|
| 129 | + $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 130 | + if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 131 | + $parameters[0] = Helpers::translate($parameters[0]); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Redirect calls to the Control Group |
|
| 135 | + if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) { |
|
| 136 | + $method = str_replace('onGroup', '', $method); |
|
| 137 | + $method = lcfirst($method); |
|
| 138 | + |
|
| 139 | + call_user_func_array(array($this->group, $method), $parameters); |
|
| 140 | + |
|
| 141 | + return $this; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return parent::__call($method, $parameters); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Prints out the field, wrapped in its group |
|
| 149 | + * |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function wrapAndRender() |
|
| 153 | + { |
|
| 154 | + // Dry syntax (hidden fields, plain fields) |
|
| 155 | + if ($this->isUnwrappable()) { |
|
| 156 | + $html = $this->render(); |
|
| 157 | + // Control group syntax |
|
| 158 | + } elseif (Form::hasInstanceOpened()) { |
|
| 159 | + $html = $this->group->wrapField($this); |
|
| 160 | + // Classic syntax |
|
| 161 | + } else { |
|
| 162 | + $html = $this->currentFramework()->createLabelOf($this); |
|
| 163 | + $html .= $this->render(); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return $html; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Prints out the field |
|
| 171 | + * |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + public function __toString() |
|
| 175 | + { |
|
| 176 | + return $this->wrapAndRender(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + //////////////////////////////////////////////////////////////////// |
|
| 180 | + ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 181 | + //////////////////////////////////////////////////////////////////// |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Whether the current field is required or not |
|
| 185 | + * |
|
| 186 | + * @return boolean |
|
| 187 | + */ |
|
| 188 | + public function isRequired() |
|
| 189 | + { |
|
| 190 | + return isset($this->attributes['required']); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Set required live validation attribute |
|
| 195 | + * |
|
| 196 | + * @param boolean $isRequired |
|
| 197 | + * @return $this |
|
| 198 | + */ |
|
| 199 | + public function required($isRequired=true) |
|
| 200 | + { |
|
| 201 | + if ($isRequired) { |
|
| 202 | + $this->attributes['required'] = true; |
|
| 203 | + } else { |
|
| 204 | + unset($this->attributes['required']); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $this; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Check if a field is unwrappable (no label) |
|
| 212 | + * |
|
| 213 | + * @return boolean |
|
| 214 | + */ |
|
| 215 | + public function isUnwrappable() |
|
| 216 | + { |
|
| 217 | + return |
|
| 218 | + ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 219 | + ($this->form and $this->isOfType('inline')) or |
|
| 220 | + $this->isButton() or |
|
| 221 | + $this->isOfType('hidden') or |
|
| 222 | + \Former\Form\Group::$opened or |
|
| 223 | + $this->group and $this->group->isRaw(); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Check if field is a checkbox or a radio |
|
| 228 | + * |
|
| 229 | + * @return boolean |
|
| 230 | + */ |
|
| 231 | + public function isCheckable() |
|
| 232 | + { |
|
| 233 | + return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios'); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Check if the field is a button |
|
| 238 | + * |
|
| 239 | + * @return boolean |
|
| 240 | + */ |
|
| 241 | + public function isButton() |
|
| 242 | + { |
|
| 243 | + return false; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the rules applied to the current field |
|
| 248 | + * |
|
| 249 | + * @return array An array of rules |
|
| 250 | + */ |
|
| 251 | + public function getRules() |
|
| 252 | + { |
|
| 253 | + return $this->app['former']->getRules($this->name); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + //////////////////////////////////////////////////////////////////// |
|
| 257 | + //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 258 | + //////////////////////////////////////////////////////////////////// |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Apply a Live Validation rule by chaining |
|
| 262 | + * |
|
| 263 | + * @param string $rule The rule |
|
| 264 | + */ |
|
| 265 | + public function rule($rule) |
|
| 266 | + { |
|
| 267 | + $parameters = func_get_args(); |
|
| 268 | + array_shift($parameters); |
|
| 269 | + |
|
| 270 | + $live = new LiveValidation($this); |
|
| 271 | + $live->apply(array( |
|
| 272 | + $rule => $parameters, |
|
| 273 | + )); |
|
| 274 | + |
|
| 275 | + return $this; |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | 278 | /** |
| 279 | 279 | * Apply multiple rules passed as a string. |
@@ -295,9 +295,9 @@ discard block |
||
| 295 | 295 | $rulename = substr($rule, 0, $colon) ; |
| 296 | 296 | |
| 297 | 297 | /** |
| 298 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 299 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 300 | - */ |
|
| 298 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 299 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 300 | + */ |
|
| 301 | 301 | if ($rulename !== 'regex') { |
| 302 | 302 | $parameters = str_getcsv(substr($rule, $colon + 1)); |
| 303 | 303 | } else { |
@@ -319,149 +319,149 @@ discard block |
||
| 319 | 319 | return $this; |
| 320 | 320 | } |
| 321 | 321 | |
| 322 | - /** |
|
| 323 | - * Adds a label to the group/field |
|
| 324 | - * |
|
| 325 | - * @param string $text A label |
|
| 326 | - * @param array $attributes The label's attributes |
|
| 327 | - * |
|
| 328 | - * @return Field A field |
|
| 329 | - */ |
|
| 330 | - public function label($text, $attributes = array()) |
|
| 331 | - { |
|
| 332 | - // Create the Label element |
|
| 333 | - $for = $this->id ?: $this->name; |
|
| 334 | - $label = $this->app['former']->label($text, $for, $attributes); |
|
| 335 | - |
|
| 336 | - // Set label |
|
| 337 | - $this->label = $label; |
|
| 338 | - if ($this->group) { |
|
| 339 | - $this->group->setLabel($label); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return $this; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Set the Field value no matter what |
|
| 347 | - * |
|
| 348 | - * @param string $value A new value |
|
| 349 | - */ |
|
| 350 | - public function forceValue($value) |
|
| 351 | - { |
|
| 352 | - $this->value = $value; |
|
| 353 | - |
|
| 354 | - return $this; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 359 | - * |
|
| 360 | - * @param string $value A new value |
|
| 361 | - */ |
|
| 362 | - public function value($value) |
|
| 363 | - { |
|
| 364 | - // Check if we already have a value stored for this field or in POST data |
|
| 365 | - $already = $this->repopulate(); |
|
| 366 | - |
|
| 367 | - if (!$already) { |
|
| 368 | - $this->value = $value; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Change the field's name |
|
| 376 | - * |
|
| 377 | - * @param string $name The new name |
|
| 378 | - */ |
|
| 379 | - public function name($name) |
|
| 380 | - { |
|
| 381 | - $this->name = $name; |
|
| 382 | - |
|
| 383 | - // Also relink the label to the new name |
|
| 384 | - $this->label($name); |
|
| 385 | - |
|
| 386 | - return $this; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Get the field's labels |
|
| 391 | - * |
|
| 392 | - * @return string |
|
| 393 | - */ |
|
| 394 | - public function getLabel() |
|
| 395 | - { |
|
| 396 | - return $this->label; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Change the field's bind destination |
|
| 401 | - * |
|
| 402 | - * @param $destination |
|
| 403 | - */ |
|
| 404 | - public function bind($destination) { |
|
| 405 | - $this->bind = $destination; |
|
| 406 | - if ($this->type != 'password') { |
|
| 407 | - $this->value = $this->repopulate(); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - return $this; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - //////////////////////////////////////////////////////////////////// |
|
| 414 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 415 | - //////////////////////////////////////////////////////////////////// |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Use values stored in Former to populate the current field |
|
| 419 | - */ |
|
| 420 | - private function repopulate($fallback = null) |
|
| 421 | - { |
|
| 422 | - // Get values from POST, populated, and manually set value |
|
| 423 | - $post = $this->app['former']->getPost($this->name); |
|
| 424 | - $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 425 | - $populate = $populator->get($this->bind ?: $this->name); |
|
| 426 | - |
|
| 427 | - // Assign a priority to each |
|
| 428 | - if (!is_null($post)) { |
|
| 429 | - return $post; |
|
| 430 | - } |
|
| 431 | - if (!is_null($populate)) { |
|
| 432 | - return $populate; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - return $fallback ?: $this->value; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Ponders a label and a field name, and tries to get the best out of it |
|
| 440 | - * |
|
| 441 | - * @param string $label A label |
|
| 442 | - * @param string $name A field name |
|
| 443 | - * |
|
| 444 | - * @return false|null A label and a field name |
|
| 445 | - */ |
|
| 446 | - private function automaticLabels($name, $label) |
|
| 447 | - { |
|
| 448 | - // Disabled automatic labels |
|
| 449 | - if (!$this->app['former']->getOption('automatic_label')) { |
|
| 450 | - $this->name = $name; |
|
| 451 | - $this->label($label); |
|
| 452 | - |
|
| 453 | - return false; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // Check for the two possibilities |
|
| 457 | - if ($label and is_null($name)) { |
|
| 458 | - $name = Str::slug($label); |
|
| 459 | - } elseif (is_null($label) and $name) { |
|
| 460 | - $label = preg_replace('/\[\]$/', '', $name); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - // Save values |
|
| 464 | - $this->name = $name; |
|
| 465 | - $this->label($label); |
|
| 466 | - } |
|
| 322 | + /** |
|
| 323 | + * Adds a label to the group/field |
|
| 324 | + * |
|
| 325 | + * @param string $text A label |
|
| 326 | + * @param array $attributes The label's attributes |
|
| 327 | + * |
|
| 328 | + * @return Field A field |
|
| 329 | + */ |
|
| 330 | + public function label($text, $attributes = array()) |
|
| 331 | + { |
|
| 332 | + // Create the Label element |
|
| 333 | + $for = $this->id ?: $this->name; |
|
| 334 | + $label = $this->app['former']->label($text, $for, $attributes); |
|
| 335 | + |
|
| 336 | + // Set label |
|
| 337 | + $this->label = $label; |
|
| 338 | + if ($this->group) { |
|
| 339 | + $this->group->setLabel($label); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return $this; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Set the Field value no matter what |
|
| 347 | + * |
|
| 348 | + * @param string $value A new value |
|
| 349 | + */ |
|
| 350 | + public function forceValue($value) |
|
| 351 | + { |
|
| 352 | + $this->value = $value; |
|
| 353 | + |
|
| 354 | + return $this; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 359 | + * |
|
| 360 | + * @param string $value A new value |
|
| 361 | + */ |
|
| 362 | + public function value($value) |
|
| 363 | + { |
|
| 364 | + // Check if we already have a value stored for this field or in POST data |
|
| 365 | + $already = $this->repopulate(); |
|
| 366 | + |
|
| 367 | + if (!$already) { |
|
| 368 | + $this->value = $value; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Change the field's name |
|
| 376 | + * |
|
| 377 | + * @param string $name The new name |
|
| 378 | + */ |
|
| 379 | + public function name($name) |
|
| 380 | + { |
|
| 381 | + $this->name = $name; |
|
| 382 | + |
|
| 383 | + // Also relink the label to the new name |
|
| 384 | + $this->label($name); |
|
| 385 | + |
|
| 386 | + return $this; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Get the field's labels |
|
| 391 | + * |
|
| 392 | + * @return string |
|
| 393 | + */ |
|
| 394 | + public function getLabel() |
|
| 395 | + { |
|
| 396 | + return $this->label; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Change the field's bind destination |
|
| 401 | + * |
|
| 402 | + * @param $destination |
|
| 403 | + */ |
|
| 404 | + public function bind($destination) { |
|
| 405 | + $this->bind = $destination; |
|
| 406 | + if ($this->type != 'password') { |
|
| 407 | + $this->value = $this->repopulate(); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + return $this; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + //////////////////////////////////////////////////////////////////// |
|
| 414 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 415 | + //////////////////////////////////////////////////////////////////// |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Use values stored in Former to populate the current field |
|
| 419 | + */ |
|
| 420 | + private function repopulate($fallback = null) |
|
| 421 | + { |
|
| 422 | + // Get values from POST, populated, and manually set value |
|
| 423 | + $post = $this->app['former']->getPost($this->name); |
|
| 424 | + $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 425 | + $populate = $populator->get($this->bind ?: $this->name); |
|
| 426 | + |
|
| 427 | + // Assign a priority to each |
|
| 428 | + if (!is_null($post)) { |
|
| 429 | + return $post; |
|
| 430 | + } |
|
| 431 | + if (!is_null($populate)) { |
|
| 432 | + return $populate; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + return $fallback ?: $this->value; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Ponders a label and a field name, and tries to get the best out of it |
|
| 440 | + * |
|
| 441 | + * @param string $label A label |
|
| 442 | + * @param string $name A field name |
|
| 443 | + * |
|
| 444 | + * @return false|null A label and a field name |
|
| 445 | + */ |
|
| 446 | + private function automaticLabels($name, $label) |
|
| 447 | + { |
|
| 448 | + // Disabled automatic labels |
|
| 449 | + if (!$this->app['former']->getOption('automatic_label')) { |
|
| 450 | + $this->name = $name; |
|
| 451 | + $this->label($label); |
|
| 452 | + |
|
| 453 | + return false; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // Check for the two possibilities |
|
| 457 | + if ($label and is_null($name)) { |
|
| 458 | + $name = Str::slug($label); |
|
| 459 | + } elseif (is_null($label) and $name) { |
|
| 460 | + $label = preg_replace('/\[\]$/', '', $name); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + // Save values |
|
| 464 | + $this->name = $name; |
|
| 465 | + $this->label($label); |
|
| 466 | + } |
|
| 467 | 467 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * @param boolean $isRequired |
| 197 | 197 | * @return $this |
| 198 | 198 | */ |
| 199 | - public function required($isRequired=true) |
|
| 199 | + public function required($isRequired = true) |
|
| 200 | 200 | { |
| 201 | 201 | if ($isRequired) { |
| 202 | 202 | $this->attributes['required'] = true; |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | |
| 293 | 293 | // If we have a rule with a value |
| 294 | 294 | if (($colon = strpos($rule, ':')) !== false) { |
| 295 | - $rulename = substr($rule, 0, $colon) ; |
|
| 295 | + $rulename = substr($rule, 0, $colon); |
|
| 296 | 296 | |
| 297 | 297 | /** |
| 298 | 298 | * Regular expressions may contain commas and should not be divided by str_getcsv. |
@@ -188,6 +188,9 @@ discard block |
||
| 188 | 188 | return $element; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | + /** |
|
| 192 | + * @param string $choiceType |
|
| 193 | + */ |
|
| 191 | 194 | public function getCheckables($choiceType) |
| 192 | 195 | { |
| 193 | 196 | if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
@@ -292,7 +295,7 @@ discard block |
||
| 292 | 295 | /** |
| 293 | 296 | * Sets the element's type based on options |
| 294 | 297 | * |
| 295 | - * @return this |
|
| 298 | + * @return Choice |
|
| 296 | 299 | */ |
| 297 | 300 | protected function setChoiceType() |
| 298 | 301 | { |
@@ -309,7 +312,6 @@ discard block |
||
| 309 | 312 | /** |
| 310 | 313 | * Select a value in the field's children |
| 311 | 314 | * |
| 312 | - * @param mixed $value |
|
| 313 | 315 | * |
| 314 | 316 | * @return bool |
| 315 | 317 | */ |
@@ -333,10 +335,6 @@ discard block |
||
| 333 | 335 | /** |
| 334 | 336 | * Set the choices |
| 335 | 337 | * |
| 336 | - * @param array $_choices The choices as an array |
|
| 337 | - * @param mixed $selected Facultative selected entry |
|
| 338 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | - * the option's values instead of the array's keys |
|
| 340 | 338 | */ |
| 341 | 339 | public function addChoice($value, $key = null) |
| 342 | 340 | { |
@@ -349,7 +347,6 @@ discard block |
||
| 349 | 347 | * Set the choices |
| 350 | 348 | * |
| 351 | 349 | * @param array $_choices The choices as an array |
| 352 | - * @param mixed $selected Facultative selected entry |
|
| 353 | 350 | * @param boolean $valuesAsKeys Whether the array's values should be used as |
| 354 | 351 | * the option's values instead of the array's keys |
| 355 | 352 | */ |
@@ -12,477 +12,477 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Choice extends Field |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Renders the checkables as inline |
|
| 17 | - * |
|
| 18 | - * @var boolean |
|
| 19 | - */ |
|
| 20 | - protected $inline = false; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The choice's placeholder |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - private $placeholder = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The choice's options |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - protected $options = [ |
|
| 15 | + /** |
|
| 16 | + * Renders the checkables as inline |
|
| 17 | + * |
|
| 18 | + * @var boolean |
|
| 19 | + */ |
|
| 20 | + protected $inline = false; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The choice's placeholder |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + private $placeholder = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The choice's options |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + protected $options = [ |
|
| 35 | 35 | 'isMultiple' => false, |
| 36 | 36 | 'isExpanded' => false, |
| 37 | 37 | ]; |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The choice's choices |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - protected $choices = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The choice's element |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - protected $element = 'choice'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The choice's self-closing state |
|
| 55 | - * |
|
| 56 | - * @var boolean |
|
| 57 | - */ |
|
| 58 | - protected $isSelfClosing = false; |
|
| 59 | - |
|
| 60 | - //////////////////////////////////////////////////////////////////// |
|
| 61 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 62 | - //////////////////////////////////////////////////////////////////// |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Easier arguments order for selects |
|
| 66 | - * |
|
| 67 | - * @param Container $app The Container instance |
|
| 68 | - * @param string $type select |
|
| 69 | - * @param string $name Field name |
|
| 70 | - * @param string $label Its label |
|
| 71 | - * @param array $choices The choice's choices |
|
| 72 | - * @param string $selected The selected choice(s) |
|
| 73 | - * @param array $attributes Attributes |
|
| 74 | - */ |
|
| 75 | - public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
|
| 76 | - { |
|
| 77 | - if ($selected) { |
|
| 78 | - $this->value = $selected; |
|
| 79 | - } |
|
| 80 | - if ($choices) { |
|
| 81 | - $this->choices($choices); |
|
| 82 | - } |
|
| 39 | + /** |
|
| 40 | + * The choice's choices |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + protected $choices = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The choice's element |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + protected $element = 'choice'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The choice's self-closing state |
|
| 55 | + * |
|
| 56 | + * @var boolean |
|
| 57 | + */ |
|
| 58 | + protected $isSelfClosing = false; |
|
| 59 | + |
|
| 60 | + //////////////////////////////////////////////////////////////////// |
|
| 61 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 62 | + //////////////////////////////////////////////////////////////////// |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Easier arguments order for selects |
|
| 66 | + * |
|
| 67 | + * @param Container $app The Container instance |
|
| 68 | + * @param string $type select |
|
| 69 | + * @param string $name Field name |
|
| 70 | + * @param string $label Its label |
|
| 71 | + * @param array $choices The choice's choices |
|
| 72 | + * @param string $selected The selected choice(s) |
|
| 73 | + * @param array $attributes Attributes |
|
| 74 | + */ |
|
| 75 | + public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
|
| 76 | + { |
|
| 77 | + if ($selected) { |
|
| 78 | + $this->value = $selected; |
|
| 79 | + } |
|
| 80 | + if ($choices) { |
|
| 81 | + $this->choices($choices); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 84 | + parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 85 | 85 | |
| 86 | - $this->setChoiceType(); |
|
| 87 | - |
|
| 88 | - // Nested models population |
|
| 89 | - if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 90 | - $this->fromQuery($this->value); |
|
| 91 | - $this->value = $selected ?: null; |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Renders the choice |
|
| 97 | - * |
|
| 98 | - * @return string A <select> tag |
|
| 99 | - */ |
|
| 100 | - public function render() |
|
| 101 | - { |
|
| 102 | - $choiceType = $this->getType(); |
|
| 103 | - $this->setFieldClasses(); |
|
| 104 | - |
|
| 105 | - if (!isset($this->attributes['id'])) { |
|
| 106 | - $this->setAttribute('id', $this->name); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - switch ($choiceType) { |
|
| 110 | - case 'select': |
|
| 111 | - $field = $this->getSelect(); |
|
| 112 | - break; |
|
| 113 | - case 'checkbox': |
|
| 114 | - case 'radio': |
|
| 115 | - $field = $this->getCheckables($choiceType); |
|
| 116 | - break; |
|
| 117 | - } |
|
| 118 | - $this->value = null; |
|
| 119 | - $content = $field->render(); |
|
| 120 | - return $content; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - public function getSelect() |
|
| 124 | - { |
|
| 125 | - $field = Element::create('select', null, $this->attributes); |
|
| 126 | - |
|
| 127 | - $name = $this->name; |
|
| 128 | - if ($this->options['isMultiple']) { |
|
| 129 | - $field->multiple(); |
|
| 130 | - $name .= '[]'; |
|
| 131 | - } |
|
| 86 | + $this->setChoiceType(); |
|
| 87 | + |
|
| 88 | + // Nested models population |
|
| 89 | + if (str_contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 90 | + $this->fromQuery($this->value); |
|
| 91 | + $this->value = $selected ?: null; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Renders the choice |
|
| 97 | + * |
|
| 98 | + * @return string A <select> tag |
|
| 99 | + */ |
|
| 100 | + public function render() |
|
| 101 | + { |
|
| 102 | + $choiceType = $this->getType(); |
|
| 103 | + $this->setFieldClasses(); |
|
| 104 | + |
|
| 105 | + if (!isset($this->attributes['id'])) { |
|
| 106 | + $this->setAttribute('id', $this->name); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + switch ($choiceType) { |
|
| 110 | + case 'select': |
|
| 111 | + $field = $this->getSelect(); |
|
| 112 | + break; |
|
| 113 | + case 'checkbox': |
|
| 114 | + case 'radio': |
|
| 115 | + $field = $this->getCheckables($choiceType); |
|
| 116 | + break; |
|
| 117 | + } |
|
| 118 | + $this->value = null; |
|
| 119 | + $content = $field->render(); |
|
| 120 | + return $content; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + public function getSelect() |
|
| 124 | + { |
|
| 125 | + $field = Element::create('select', null, $this->attributes); |
|
| 126 | + |
|
| 127 | + $name = $this->name; |
|
| 128 | + if ($this->options['isMultiple']) { |
|
| 129 | + $field->multiple(); |
|
| 130 | + $name .= '[]'; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - $field->setAttribute('name', $name); |
|
| 133 | + $field->setAttribute('name', $name); |
|
| 134 | 134 | |
| 135 | - $field->nest($this->getOptions()); |
|
| 135 | + $field->nest($this->getOptions()); |
|
| 136 | 136 | |
| 137 | - return $field; |
|
| 138 | - } |
|
| 137 | + return $field; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - public function getOptions() |
|
| 141 | - { |
|
| 142 | - $children = []; |
|
| 140 | + public function getOptions() |
|
| 141 | + { |
|
| 142 | + $children = []; |
|
| 143 | 143 | |
| 144 | - // Add placeholder text if any |
|
| 145 | - if ($placeholder = $this->getPlaceholder()) { |
|
| 146 | - $children[] = $placeholder; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - foreach ($this->choices as $key => $value) { |
|
| 150 | - if (is_array($value) and !isset($value['value'])) { |
|
| 151 | - $children[] = $this->getOptGroup($key, $value); |
|
| 152 | - } else { |
|
| 153 | - $children[] = $this->getOption($key, $value); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - return $children; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function getOptGroup($label, $options) |
|
| 160 | - { |
|
| 161 | - $element = Element::create('optgroup')->label($label); |
|
| 162 | - $children = []; |
|
| 163 | - foreach ($options as $key => $value) { |
|
| 164 | - $option = $this->getOption($key, $value); |
|
| 165 | - $children[] = $option; |
|
| 166 | - } |
|
| 167 | - $element->nest($children); |
|
| 168 | - |
|
| 169 | - return $element; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - public function getOption($key, $value) |
|
| 173 | - { |
|
| 174 | - if (is_array($value) and isset($value['value'])) { |
|
| 175 | - $attributes = $value; |
|
| 176 | - $text = $key; |
|
| 177 | - $key = null; |
|
| 178 | - } else { |
|
| 179 | - $attributes = array('value' => $key); |
|
| 180 | - $text = $value; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $element = Element::create('option', $text, $attributes); |
|
| 184 | - if ($this->hasValue($attributes['value'])) { |
|
| 185 | - $element->selected('selected'); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $element; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function getCheckables($choiceType) |
|
| 192 | - { |
|
| 193 | - if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
|
| 194 | - $this->value = explode(',', $this->value); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $disabled = isset($this->attributes['disabled']); |
|
| 198 | - unset($this->attributes['disabled']); |
|
| 199 | - |
|
| 200 | - $field = Element::create('div', null, $this->attributes); |
|
| 201 | - |
|
| 202 | - $children = []; |
|
| 203 | - foreach ($this->choices as $key => $value) { |
|
| 204 | - $attributes = []; |
|
| 205 | - |
|
| 206 | - if (is_array($value) and isset($value['value'])) { |
|
| 207 | - $attributes = $value; |
|
| 208 | - $label = $key; |
|
| 209 | - $inputValue = $value['value']; |
|
| 210 | - } else { |
|
| 211 | - $attributes = []; |
|
| 212 | - $label = $value; |
|
| 213 | - $inputValue = $key; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if ($disabled) { |
|
| 217 | - $attributes['disabled'] = true; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - if(isset($attributes['name'])) { |
|
| 221 | - $name = $attributes['name']; |
|
| 222 | - unset($attributes['name']); |
|
| 223 | - } else { |
|
| 224 | - $name = $this->name; |
|
| 225 | - } |
|
| 226 | - if ($this->options['isMultiple']) { |
|
| 227 | - $name .= '[]'; |
|
| 228 | - } |
|
| 144 | + // Add placeholder text if any |
|
| 145 | + if ($placeholder = $this->getPlaceholder()) { |
|
| 146 | + $children[] = $placeholder; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + foreach ($this->choices as $key => $value) { |
|
| 150 | + if (is_array($value) and !isset($value['value'])) { |
|
| 151 | + $children[] = $this->getOptGroup($key, $value); |
|
| 152 | + } else { |
|
| 153 | + $children[] = $this->getOption($key, $value); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + return $children; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function getOptGroup($label, $options) |
|
| 160 | + { |
|
| 161 | + $element = Element::create('optgroup')->label($label); |
|
| 162 | + $children = []; |
|
| 163 | + foreach ($options as $key => $value) { |
|
| 164 | + $option = $this->getOption($key, $value); |
|
| 165 | + $children[] = $option; |
|
| 166 | + } |
|
| 167 | + $element->nest($children); |
|
| 168 | + |
|
| 169 | + return $element; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + public function getOption($key, $value) |
|
| 173 | + { |
|
| 174 | + if (is_array($value) and isset($value['value'])) { |
|
| 175 | + $attributes = $value; |
|
| 176 | + $text = $key; |
|
| 177 | + $key = null; |
|
| 178 | + } else { |
|
| 179 | + $attributes = array('value' => $key); |
|
| 180 | + $text = $value; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $element = Element::create('option', $text, $attributes); |
|
| 184 | + if ($this->hasValue($attributes['value'])) { |
|
| 185 | + $element->selected('selected'); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $element; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function getCheckables($choiceType) |
|
| 192 | + { |
|
| 193 | + if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
|
| 194 | + $this->value = explode(',', $this->value); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $disabled = isset($this->attributes['disabled']); |
|
| 198 | + unset($this->attributes['disabled']); |
|
| 199 | + |
|
| 200 | + $field = Element::create('div', null, $this->attributes); |
|
| 201 | + |
|
| 202 | + $children = []; |
|
| 203 | + foreach ($this->choices as $key => $value) { |
|
| 204 | + $attributes = []; |
|
| 205 | + |
|
| 206 | + if (is_array($value) and isset($value['value'])) { |
|
| 207 | + $attributes = $value; |
|
| 208 | + $label = $key; |
|
| 209 | + $inputValue = $value['value']; |
|
| 210 | + } else { |
|
| 211 | + $attributes = []; |
|
| 212 | + $label = $value; |
|
| 213 | + $inputValue = $key; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if ($disabled) { |
|
| 217 | + $attributes['disabled'] = true; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + if(isset($attributes['name'])) { |
|
| 221 | + $name = $attributes['name']; |
|
| 222 | + unset($attributes['name']); |
|
| 223 | + } else { |
|
| 224 | + $name = $this->name; |
|
| 225 | + } |
|
| 226 | + if ($this->options['isMultiple']) { |
|
| 227 | + $name .= '[]'; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - if(!isset($attributes['id'])) { |
|
| 231 | - $attributes['id'] = $this->id.'_'.count($children); |
|
| 232 | - } |
|
| 230 | + if(!isset($attributes['id'])) { |
|
| 231 | + $attributes['id'] = $this->id.'_'.count($children); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - // If inline items, add class |
|
| 235 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 234 | + // If inline items, add class |
|
| 235 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 236 | 236 | |
| 237 | - // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 238 | - // rendering inline. |
|
| 239 | - $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $choiceType.$isInline; |
|
| 237 | + // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 238 | + // rendering inline. |
|
| 239 | + $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $choiceType.$isInline; |
|
| 240 | 240 | |
| 241 | - $element = Input::create($choiceType, $name, $inputValue, $attributes); |
|
| 241 | + $element = Input::create($choiceType, $name, $inputValue, $attributes); |
|
| 242 | 242 | |
| 243 | - // $element->setAttribute('name', $name); |
|
| 244 | - |
|
| 245 | - if ($this->hasValue($inputValue)) { |
|
| 246 | - $element->checked('checked'); |
|
| 247 | - } |
|
| 248 | - // $attributes['value'] = $key; |
|
| 249 | - if (!$label) { |
|
| 250 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 251 | - } else { |
|
| 252 | - $rendered = $element->render(); |
|
| 253 | - $labelElement = Element::create('label', $rendered.$label); |
|
| 254 | - $element = $labelElement->for($attributes['id'])->class($class); |
|
| 255 | - } |
|
| 243 | + // $element->setAttribute('name', $name); |
|
| 244 | + |
|
| 245 | + if ($this->hasValue($inputValue)) { |
|
| 246 | + $element->checked('checked'); |
|
| 247 | + } |
|
| 248 | + // $attributes['value'] = $key; |
|
| 249 | + if (!$label) { |
|
| 250 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 251 | + } else { |
|
| 252 | + $rendered = $element->render(); |
|
| 253 | + $labelElement = Element::create('label', $rendered.$label); |
|
| 254 | + $element = $labelElement->for($attributes['id'])->class($class); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 258 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 259 | - $wrapper = Element::create('div', $element->render())->class($choiceType); |
|
| 260 | - if ($disabled) { |
|
| 261 | - $wrapper->addClass('disabled'); |
|
| 262 | - } |
|
| 263 | - $element = $wrapper; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - $children[] = $element; |
|
| 267 | - } |
|
| 268 | - $field->nest($children); |
|
| 257 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 258 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 259 | + $wrapper = Element::create('div', $element->render())->class($choiceType); |
|
| 260 | + if ($disabled) { |
|
| 261 | + $wrapper->addClass('disabled'); |
|
| 262 | + } |
|
| 263 | + $element = $wrapper; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + $children[] = $element; |
|
| 267 | + } |
|
| 268 | + $field->nest($children); |
|
| 269 | 269 | |
| 270 | - return $field; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the choice's placeholder |
|
| 275 | - * |
|
| 276 | - * @return Element |
|
| 277 | - */ |
|
| 278 | - protected function getPlaceholder() |
|
| 279 | - { |
|
| 280 | - if (!$this->placeholder) { |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 285 | - if (!(array)$this->value) { |
|
| 286 | - $attributes['selected'] = 'selected'; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - return Element::create('option', $this->placeholder, $attributes); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Sets the element's type based on options |
|
| 294 | - * |
|
| 295 | - * @return this |
|
| 296 | - */ |
|
| 297 | - protected function setChoiceType() |
|
| 298 | - { |
|
| 299 | - if ($this->options['isExpanded'] && !$this->options['isMultiple']) { |
|
| 300 | - $this->setType('radio'); |
|
| 301 | - } elseif ($this->options['isExpanded'] && $this->options['isMultiple']) { |
|
| 302 | - $this->setType('checkbox'); |
|
| 303 | - } else { |
|
| 304 | - $this->setType('select'); |
|
| 305 | - } |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Select a value in the field's children |
|
| 311 | - * |
|
| 312 | - * @param mixed $value |
|
| 313 | - * |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - protected function hasValue($choiceValue) |
|
| 317 | - { |
|
| 318 | - foreach ((array)$this->value as $key => $value) { |
|
| 319 | - if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 320 | - $value = $value->getKey(); |
|
| 321 | - } |
|
| 322 | - if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 323 | - return true; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - return false; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - //////////////////////////////////////////////////////////////////// |
|
| 330 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 331 | - //////////////////////////////////////////////////////////////////// |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Set the choices |
|
| 335 | - * |
|
| 336 | - * @param array $_choices The choices as an array |
|
| 337 | - * @param mixed $selected Facultative selected entry |
|
| 338 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | - * the option's values instead of the array's keys |
|
| 340 | - */ |
|
| 341 | - public function addChoice($value, $key = null) |
|
| 342 | - { |
|
| 343 | - $this->choices[$key ?? $value] = $value; |
|
| 344 | - |
|
| 345 | - return $this; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Set the choices |
|
| 350 | - * |
|
| 351 | - * @param array $_choices The choices as an array |
|
| 352 | - * @param mixed $selected Facultative selected entry |
|
| 353 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 354 | - * the option's values instead of the array's keys |
|
| 355 | - */ |
|
| 356 | - public function choices($_choices, $valuesAsKeys = false) |
|
| 357 | - { |
|
| 358 | - $choices = (array) $_choices; |
|
| 359 | - |
|
| 360 | - // If valuesAsKeys is true, use the values as keys |
|
| 361 | - if ($valuesAsKeys) { |
|
| 362 | - foreach ($choices as $value) { |
|
| 363 | - $this->addChoice($value, $value); |
|
| 364 | - } |
|
| 365 | - } else { |
|
| 366 | - foreach ($choices as $key => $value) { |
|
| 367 | - $this->addChoice($value, $key); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Creates a list of choices from a range |
|
| 376 | - * |
|
| 377 | - * @param integer $from |
|
| 378 | - * @param integer $to |
|
| 379 | - * @param integer $step |
|
| 380 | - */ |
|
| 381 | - public function range($from, $to, $step = 1) |
|
| 382 | - { |
|
| 383 | - $range = range($from, $to, $step); |
|
| 384 | - $this->choices($range, true); |
|
| 385 | - |
|
| 386 | - return $this; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Use the results from a Fluent/Eloquent query as choices |
|
| 391 | - * |
|
| 392 | - * @param array $results An array of Eloquent models |
|
| 393 | - * @param string|function $text The value to use as text |
|
| 394 | - * @param string|array $attributes The data to use as attributes |
|
| 395 | - * @param string $selected The default selected item |
|
| 396 | - */ |
|
| 397 | - public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 398 | - { |
|
| 399 | - $this->choices(Helpers::queryToArray($results, $text, $attributes))->value($selected); |
|
| 400 | - |
|
| 401 | - return $this; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Add a placeholder to the current select |
|
| 406 | - * |
|
| 407 | - * @param string $placeholder The placeholder text |
|
| 408 | - */ |
|
| 409 | - public function placeholder($placeholder) |
|
| 410 | - { |
|
| 411 | - $this->placeholder = Helpers::translate($placeholder); |
|
| 412 | - |
|
| 413 | - return $this; |
|
| 414 | - } |
|
| 270 | + return $field; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the choice's placeholder |
|
| 275 | + * |
|
| 276 | + * @return Element |
|
| 277 | + */ |
|
| 278 | + protected function getPlaceholder() |
|
| 279 | + { |
|
| 280 | + if (!$this->placeholder) { |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 285 | + if (!(array)$this->value) { |
|
| 286 | + $attributes['selected'] = 'selected'; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + return Element::create('option', $this->placeholder, $attributes); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Sets the element's type based on options |
|
| 294 | + * |
|
| 295 | + * @return this |
|
| 296 | + */ |
|
| 297 | + protected function setChoiceType() |
|
| 298 | + { |
|
| 299 | + if ($this->options['isExpanded'] && !$this->options['isMultiple']) { |
|
| 300 | + $this->setType('radio'); |
|
| 301 | + } elseif ($this->options['isExpanded'] && $this->options['isMultiple']) { |
|
| 302 | + $this->setType('checkbox'); |
|
| 303 | + } else { |
|
| 304 | + $this->setType('select'); |
|
| 305 | + } |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Select a value in the field's children |
|
| 311 | + * |
|
| 312 | + * @param mixed $value |
|
| 313 | + * |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + protected function hasValue($choiceValue) |
|
| 317 | + { |
|
| 318 | + foreach ((array)$this->value as $key => $value) { |
|
| 319 | + if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 320 | + $value = $value->getKey(); |
|
| 321 | + } |
|
| 322 | + if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 323 | + return true; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + return false; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + //////////////////////////////////////////////////////////////////// |
|
| 330 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 331 | + //////////////////////////////////////////////////////////////////// |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Set the choices |
|
| 335 | + * |
|
| 336 | + * @param array $_choices The choices as an array |
|
| 337 | + * @param mixed $selected Facultative selected entry |
|
| 338 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | + * the option's values instead of the array's keys |
|
| 340 | + */ |
|
| 341 | + public function addChoice($value, $key = null) |
|
| 342 | + { |
|
| 343 | + $this->choices[$key ?? $value] = $value; |
|
| 344 | + |
|
| 345 | + return $this; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Set the choices |
|
| 350 | + * |
|
| 351 | + * @param array $_choices The choices as an array |
|
| 352 | + * @param mixed $selected Facultative selected entry |
|
| 353 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 354 | + * the option's values instead of the array's keys |
|
| 355 | + */ |
|
| 356 | + public function choices($_choices, $valuesAsKeys = false) |
|
| 357 | + { |
|
| 358 | + $choices = (array) $_choices; |
|
| 359 | + |
|
| 360 | + // If valuesAsKeys is true, use the values as keys |
|
| 361 | + if ($valuesAsKeys) { |
|
| 362 | + foreach ($choices as $value) { |
|
| 363 | + $this->addChoice($value, $value); |
|
| 364 | + } |
|
| 365 | + } else { |
|
| 366 | + foreach ($choices as $key => $value) { |
|
| 367 | + $this->addChoice($value, $key); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Creates a list of choices from a range |
|
| 376 | + * |
|
| 377 | + * @param integer $from |
|
| 378 | + * @param integer $to |
|
| 379 | + * @param integer $step |
|
| 380 | + */ |
|
| 381 | + public function range($from, $to, $step = 1) |
|
| 382 | + { |
|
| 383 | + $range = range($from, $to, $step); |
|
| 384 | + $this->choices($range, true); |
|
| 385 | + |
|
| 386 | + return $this; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Use the results from a Fluent/Eloquent query as choices |
|
| 391 | + * |
|
| 392 | + * @param array $results An array of Eloquent models |
|
| 393 | + * @param string|function $text The value to use as text |
|
| 394 | + * @param string|array $attributes The data to use as attributes |
|
| 395 | + * @param string $selected The default selected item |
|
| 396 | + */ |
|
| 397 | + public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 398 | + { |
|
| 399 | + $this->choices(Helpers::queryToArray($results, $text, $attributes))->value($selected); |
|
| 400 | + |
|
| 401 | + return $this; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Add a placeholder to the current select |
|
| 406 | + * |
|
| 407 | + * @param string $placeholder The placeholder text |
|
| 408 | + */ |
|
| 409 | + public function placeholder($placeholder) |
|
| 410 | + { |
|
| 411 | + $this->placeholder = Helpers::translate($placeholder); |
|
| 412 | + |
|
| 413 | + return $this; |
|
| 414 | + } |
|
| 415 | 415 | |
| 416 | - /** |
|
| 417 | - * Set isMultiple |
|
| 418 | - * |
|
| 419 | - * @param boolean $isMultiple |
|
| 420 | - * @return $this |
|
| 421 | - */ |
|
| 422 | - public function multiple($isMultiple = true) |
|
| 423 | - { |
|
| 424 | - $this->options['isMultiple'] = $isMultiple; |
|
| 425 | - $this->setChoiceType(); |
|
| 426 | - |
|
| 427 | - return $this; |
|
| 428 | - } |
|
| 416 | + /** |
|
| 417 | + * Set isMultiple |
|
| 418 | + * |
|
| 419 | + * @param boolean $isMultiple |
|
| 420 | + * @return $this |
|
| 421 | + */ |
|
| 422 | + public function multiple($isMultiple = true) |
|
| 423 | + { |
|
| 424 | + $this->options['isMultiple'] = $isMultiple; |
|
| 425 | + $this->setChoiceType(); |
|
| 426 | + |
|
| 427 | + return $this; |
|
| 428 | + } |
|
| 429 | 429 | |
| 430 | - /** |
|
| 431 | - * Set isExpanded |
|
| 432 | - * |
|
| 433 | - * @param boolean $isExpanded |
|
| 434 | - * @return $this |
|
| 435 | - */ |
|
| 436 | - public function expanded($isExpanded = true) |
|
| 437 | - { |
|
| 438 | - $this->options['isExpanded'] = $isExpanded; |
|
| 439 | - $this->setChoiceType(); |
|
| 440 | - |
|
| 441 | - return $this; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Set the choices as inline (for expanded items) |
|
| 446 | - */ |
|
| 447 | - public function inline($isInline = true) |
|
| 448 | - { |
|
| 449 | - $this->inline = $isInline; |
|
| 450 | - |
|
| 451 | - return $this; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Set the choices as stacked (for expanded items) |
|
| 456 | - */ |
|
| 457 | - public function stacked($isStacked = true) |
|
| 458 | - { |
|
| 459 | - $this->inline = !$isStacked; |
|
| 460 | - |
|
| 461 | - return $this; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Check if field is a checkbox or a radio |
|
| 466 | - * |
|
| 467 | - * @return boolean |
|
| 468 | - */ |
|
| 469 | - public function isCheckable() |
|
| 470 | - { |
|
| 471 | - return $this->options['isExpanded']; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - //////////////////////////////////////////////////////////////////// |
|
| 475 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 476 | - //////////////////////////////////////////////////////////////////// |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Returns the current choices in memory for manipulations |
|
| 480 | - * |
|
| 481 | - * @return array The current choices array |
|
| 482 | - */ |
|
| 483 | - public function getChoices() |
|
| 484 | - { |
|
| 485 | - return $this->choices; |
|
| 486 | - } |
|
| 430 | + /** |
|
| 431 | + * Set isExpanded |
|
| 432 | + * |
|
| 433 | + * @param boolean $isExpanded |
|
| 434 | + * @return $this |
|
| 435 | + */ |
|
| 436 | + public function expanded($isExpanded = true) |
|
| 437 | + { |
|
| 438 | + $this->options['isExpanded'] = $isExpanded; |
|
| 439 | + $this->setChoiceType(); |
|
| 440 | + |
|
| 441 | + return $this; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Set the choices as inline (for expanded items) |
|
| 446 | + */ |
|
| 447 | + public function inline($isInline = true) |
|
| 448 | + { |
|
| 449 | + $this->inline = $isInline; |
|
| 450 | + |
|
| 451 | + return $this; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Set the choices as stacked (for expanded items) |
|
| 456 | + */ |
|
| 457 | + public function stacked($isStacked = true) |
|
| 458 | + { |
|
| 459 | + $this->inline = !$isStacked; |
|
| 460 | + |
|
| 461 | + return $this; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Check if field is a checkbox or a radio |
|
| 466 | + * |
|
| 467 | + * @return boolean |
|
| 468 | + */ |
|
| 469 | + public function isCheckable() |
|
| 470 | + { |
|
| 471 | + return $this->options['isExpanded']; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + //////////////////////////////////////////////////////////////////// |
|
| 475 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 476 | + //////////////////////////////////////////////////////////////////// |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Returns the current choices in memory for manipulations |
|
| 480 | + * |
|
| 481 | + * @return array The current choices array |
|
| 482 | + */ |
|
| 483 | + public function getChoices() |
|
| 484 | + { |
|
| 485 | + return $this->choices; |
|
| 486 | + } |
|
| 487 | 487 | |
| 488 | 488 | } |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | $attributes['disabled'] = true; |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - if(isset($attributes['name'])) { |
|
| 220 | + if (isset($attributes['name'])) { |
|
| 221 | 221 | $name = $attributes['name']; |
| 222 | 222 | unset($attributes['name']); |
| 223 | 223 | } else { |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | $name .= '[]'; |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | - if(!isset($attributes['id'])) { |
|
| 230 | + if (!isset($attributes['id'])) { |
|
| 231 | 231 | $attributes['id'] = $this->id.'_'.count($children); |
| 232 | 232 | } |
| 233 | 233 | |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $attributes = array('value' => '', 'disabled' => 'disabled'); |
| 285 | - if (!(array)$this->value) { |
|
| 285 | + if (!(array) $this->value) { |
|
| 286 | 286 | $attributes['selected'] = 'selected'; |
| 287 | 287 | } |
| 288 | 288 | |
@@ -315,11 +315,11 @@ discard block |
||
| 315 | 315 | */ |
| 316 | 316 | protected function hasValue($choiceValue) |
| 317 | 317 | { |
| 318 | - foreach ((array)$this->value as $key => $value) { |
|
| 318 | + foreach ((array) $this->value as $key => $value) { |
|
| 319 | 319 | if (is_object($value) && method_exists($value, 'getKey')) { |
| 320 | 320 | $value = $value->getKey(); |
| 321 | 321 | } |
| 322 | - if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 322 | + if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int) $value) { |
|
| 323 | 323 | return true; |
| 324 | 324 | } |
| 325 | 325 | } |
@@ -181,7 +181,7 @@ |
||
| 181 | 181 | /** |
| 182 | 182 | * Set the modifiers from initial method call |
| 183 | 183 | * |
| 184 | - * @return $this |
|
| 184 | + * @return string |
|
| 185 | 185 | */ |
| 186 | 186 | public function getModifiers() |
| 187 | 187 | { |
@@ -10,192 +10,192 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class FormerObject extends Element |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The field's name |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $name; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The field type |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected $type; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A list of class properties to be added to attributes |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - protected $injectedProperties = array('name'); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The field's modifiers from method call |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $modifiers; |
|
| 40 | - |
|
| 41 | - //////////////////////////////////////////////////////////////////// |
|
| 42 | - /////////////////////////// ID AND LABELS ////////////////////////// |
|
| 43 | - //////////////////////////////////////////////////////////////////// |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Create an unique ID and return it |
|
| 47 | - * |
|
| 48 | - * @return string |
|
| 49 | - */ |
|
| 50 | - public function getCreatedId() |
|
| 51 | - { |
|
| 52 | - $this->setId(); |
|
| 53 | - |
|
| 54 | - return $this->attributes['id']; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Set the matching ID on a field if possible |
|
| 59 | - */ |
|
| 60 | - protected function setId() |
|
| 61 | - { |
|
| 62 | - if (!array_key_exists('id', $this->attributes) and |
|
| 63 | - in_array($this->name, $this->app['former']->labels) |
|
| 64 | - ) { |
|
| 65 | - // Set and save the field's ID |
|
| 66 | - $id = $this->getUniqueId($this->name); |
|
| 67 | - $this->attributes['id'] = $id; |
|
| 68 | - $this->app['former']->ids[] = $id; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Get an unique ID for a field from its name |
|
| 74 | - * |
|
| 75 | - * @param string $name |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - protected function getUniqueId($name) |
|
| 80 | - { |
|
| 81 | - $names = &$this->app['former']->names; |
|
| 82 | - |
|
| 83 | - if (array_key_exists($name, $names)) { |
|
| 84 | - $count = $names[$name] + 1; |
|
| 85 | - $names[$name] = $count; |
|
| 86 | - return $name . '-' . $count; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $names[$name] = 1; |
|
| 90 | - return $name; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Runs the frameworks getFieldClasses method on this |
|
| 95 | - * |
|
| 96 | - * @return $this |
|
| 97 | - */ |
|
| 98 | - protected function setFieldClasses() |
|
| 99 | - { |
|
| 100 | - $framework = isset($this->app['former.form.framework']) ? $this->app['former.form.framework'] : $this->app['former.framework']; |
|
| 101 | - $framework->getFieldClasses($this, $this->modifiers); |
|
| 102 | - |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Render the FormerObject and set its id |
|
| 108 | - * |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - public function render() |
|
| 112 | - { |
|
| 113 | - // Set the proper ID according to the label |
|
| 114 | - $this->setId(); |
|
| 115 | - |
|
| 116 | - if($this instanceof Field) { |
|
| 117 | - $this->setFieldClasses(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // Encode HTML value |
|
| 121 | - $isButton = ($this instanceof Field) ? $this->isButton() : false; |
|
| 122 | - if (!$isButton and is_string($this->value)) { |
|
| 123 | - $this->value = Helpers::encode($this->value); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return parent::render(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - //////////////////////////////////////////////////////////////////// |
|
| 130 | - ////////////////////////////// GETTERS ///////////////////////////// |
|
| 131 | - //////////////////////////////////////////////////////////////////// |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Get the object's name |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getName() |
|
| 139 | - { |
|
| 140 | - return $this->name; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - //////////////////////////////////////////////////////////////////// |
|
| 144 | - //////////////////////////// OBJECT TYPE /////////////////////////// |
|
| 145 | - //////////////////////////////////////////////////////////////////// |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get the object's type |
|
| 149 | - * |
|
| 150 | - * @return string |
|
| 151 | - */ |
|
| 152 | - public function getType() |
|
| 153 | - { |
|
| 154 | - return $this->type; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Change a object's type |
|
| 159 | - * |
|
| 160 | - * @param string $type |
|
| 161 | - */ |
|
| 162 | - public function setType($type) |
|
| 163 | - { |
|
| 164 | - $this->type = $type; |
|
| 165 | - |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Check if an object is of a certain type |
|
| 171 | - * |
|
| 172 | - * @return boolean |
|
| 173 | - */ |
|
| 174 | - public function isOfType() |
|
| 175 | - { |
|
| 176 | - $types = func_get_args(); |
|
| 177 | - |
|
| 178 | - return in_array($this->type, $types); |
|
| 179 | - } |
|
| 13 | + /** |
|
| 14 | + * The field's name |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $name; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The field type |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected $type; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A list of class properties to be added to attributes |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + protected $injectedProperties = array('name'); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The field's modifiers from method call |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $modifiers; |
|
| 40 | + |
|
| 41 | + //////////////////////////////////////////////////////////////////// |
|
| 42 | + /////////////////////////// ID AND LABELS ////////////////////////// |
|
| 43 | + //////////////////////////////////////////////////////////////////// |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Create an unique ID and return it |
|
| 47 | + * |
|
| 48 | + * @return string |
|
| 49 | + */ |
|
| 50 | + public function getCreatedId() |
|
| 51 | + { |
|
| 52 | + $this->setId(); |
|
| 53 | + |
|
| 54 | + return $this->attributes['id']; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Set the matching ID on a field if possible |
|
| 59 | + */ |
|
| 60 | + protected function setId() |
|
| 61 | + { |
|
| 62 | + if (!array_key_exists('id', $this->attributes) and |
|
| 63 | + in_array($this->name, $this->app['former']->labels) |
|
| 64 | + ) { |
|
| 65 | + // Set and save the field's ID |
|
| 66 | + $id = $this->getUniqueId($this->name); |
|
| 67 | + $this->attributes['id'] = $id; |
|
| 68 | + $this->app['former']->ids[] = $id; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Get an unique ID for a field from its name |
|
| 74 | + * |
|
| 75 | + * @param string $name |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + protected function getUniqueId($name) |
|
| 80 | + { |
|
| 81 | + $names = &$this->app['former']->names; |
|
| 82 | + |
|
| 83 | + if (array_key_exists($name, $names)) { |
|
| 84 | + $count = $names[$name] + 1; |
|
| 85 | + $names[$name] = $count; |
|
| 86 | + return $name . '-' . $count; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $names[$name] = 1; |
|
| 90 | + return $name; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Runs the frameworks getFieldClasses method on this |
|
| 95 | + * |
|
| 96 | + * @return $this |
|
| 97 | + */ |
|
| 98 | + protected function setFieldClasses() |
|
| 99 | + { |
|
| 100 | + $framework = isset($this->app['former.form.framework']) ? $this->app['former.form.framework'] : $this->app['former.framework']; |
|
| 101 | + $framework->getFieldClasses($this, $this->modifiers); |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Render the FormerObject and set its id |
|
| 108 | + * |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + public function render() |
|
| 112 | + { |
|
| 113 | + // Set the proper ID according to the label |
|
| 114 | + $this->setId(); |
|
| 115 | + |
|
| 116 | + if($this instanceof Field) { |
|
| 117 | + $this->setFieldClasses(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // Encode HTML value |
|
| 121 | + $isButton = ($this instanceof Field) ? $this->isButton() : false; |
|
| 122 | + if (!$isButton and is_string($this->value)) { |
|
| 123 | + $this->value = Helpers::encode($this->value); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return parent::render(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + //////////////////////////////////////////////////////////////////// |
|
| 130 | + ////////////////////////////// GETTERS ///////////////////////////// |
|
| 131 | + //////////////////////////////////////////////////////////////////// |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Get the object's name |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getName() |
|
| 139 | + { |
|
| 140 | + return $this->name; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + //////////////////////////////////////////////////////////////////// |
|
| 144 | + //////////////////////////// OBJECT TYPE /////////////////////////// |
|
| 145 | + //////////////////////////////////////////////////////////////////// |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get the object's type |
|
| 149 | + * |
|
| 150 | + * @return string |
|
| 151 | + */ |
|
| 152 | + public function getType() |
|
| 153 | + { |
|
| 154 | + return $this->type; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Change a object's type |
|
| 159 | + * |
|
| 160 | + * @param string $type |
|
| 161 | + */ |
|
| 162 | + public function setType($type) |
|
| 163 | + { |
|
| 164 | + $this->type = $type; |
|
| 165 | + |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Check if an object is of a certain type |
|
| 171 | + * |
|
| 172 | + * @return boolean |
|
| 173 | + */ |
|
| 174 | + public function isOfType() |
|
| 175 | + { |
|
| 176 | + $types = func_get_args(); |
|
| 177 | + |
|
| 178 | + return in_array($this->type, $types); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Set the modifiers from initial method call |
|
| 183 | - * |
|
| 184 | - * @return $this |
|
| 185 | - */ |
|
| 186 | - public function getModifiers() |
|
| 187 | - { |
|
| 188 | - return $this->modifiers; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Set the modifiers from initial method call |
|
| 193 | - * |
|
| 194 | - * @return $this |
|
| 195 | - */ |
|
| 196 | - public function setModifiers($modifiers) |
|
| 197 | - { |
|
| 198 | - $this->modifiers = $modifiers; |
|
| 199 | - return $this; |
|
| 200 | - } |
|
| 181 | + /** |
|
| 182 | + * Set the modifiers from initial method call |
|
| 183 | + * |
|
| 184 | + * @return $this |
|
| 185 | + */ |
|
| 186 | + public function getModifiers() |
|
| 187 | + { |
|
| 188 | + return $this->modifiers; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Set the modifiers from initial method call |
|
| 193 | + * |
|
| 194 | + * @return $this |
|
| 195 | + */ |
|
| 196 | + public function setModifiers($modifiers) |
|
| 197 | + { |
|
| 198 | + $this->modifiers = $modifiers; |
|
| 199 | + return $this; |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | if (array_key_exists($name, $names)) { |
| 84 | 84 | $count = $names[$name] + 1; |
| 85 | 85 | $names[$name] = $count; |
| 86 | - return $name . '-' . $count; |
|
| 86 | + return $name.'-'.$count; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | $names[$name] = 1; |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | // Set the proper ID according to the label |
| 114 | 114 | $this->setId(); |
| 115 | 115 | |
| 116 | - if($this instanceof Field) { |
|
| 116 | + if ($this instanceof Field) { |
|
| 117 | 117 | $this->setFieldClasses(); |
| 118 | 118 | } |
| 119 | 119 | |
@@ -13,483 +13,483 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Former |
| 15 | 15 | { |
| 16 | - // Instances |
|
| 17 | - //////////////////////////////////////////////////////////////////// |
|
| 18 | - |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The current environment |
|
| 22 | - * |
|
| 23 | - * @var \Illuminate\Container\Container |
|
| 24 | - */ |
|
| 25 | - protected $app; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Method Dispatcher |
|
| 29 | - * |
|
| 30 | - * @var MethodDispatcher |
|
| 31 | - */ |
|
| 32 | - protected $dispatch; |
|
| 33 | - |
|
| 34 | - // Informations |
|
| 35 | - //////////////////////////////////////////////////////////////////// |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The form's errors |
|
| 39 | - * |
|
| 40 | - * @var Message |
|
| 41 | - */ |
|
| 42 | - protected $errors; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * An array of rules to use |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - protected $rules = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * An array of field macros |
|
| 53 | - * |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $macros = array(); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The labels created so far |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - public $labels = array(); |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The IDs created so far |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - public $ids = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * A lookup table where the key is the input name, |
|
| 74 | - * and the value is number of times seen. This is |
|
| 75 | - * used to calculate unique ids. |
|
| 76 | - * |
|
| 77 | - * @var array |
|
| 78 | - */ |
|
| 79 | - public $names = array(); |
|
| 80 | - |
|
| 81 | - // Namespaces |
|
| 82 | - //////////////////////////////////////////////////////////////////// |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The namespace of Form elements |
|
| 86 | - */ |
|
| 87 | - const FORMSPACE = 'Former\Form\\'; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * The namespace of fields |
|
| 91 | - */ |
|
| 92 | - const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Build a new Former instance |
|
| 96 | - * |
|
| 97 | - * @param Container $app |
|
| 98 | - * @param MethodDispatcher $dispatcher |
|
| 99 | - */ |
|
| 100 | - public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 101 | - { |
|
| 102 | - $this->app = $app; |
|
| 103 | - $this->dispatch = $dispatcher; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - //////////////////////////////////////////////////////////////////// |
|
| 107 | - //////////////////////////// INTERFACE ///////////////////////////// |
|
| 108 | - //////////////////////////////////////////////////////////////////// |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Acts as a router that redirects methods to all of Former classes |
|
| 112 | - * |
|
| 113 | - * @param string $method The method called |
|
| 114 | - * @param array $parameters An array of parameters |
|
| 115 | - * |
|
| 116 | - * @return mixed |
|
| 117 | - */ |
|
| 118 | - public function __call($method, $parameters) |
|
| 119 | - { |
|
| 120 | - // Dispatch to Form\Elements |
|
| 121 | - // Explicitly check false since closeGroup() may return an empty string |
|
| 122 | - if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 123 | - return $element; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - // Dispatch to Form\Form |
|
| 127 | - if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 128 | - $this->app->instance('former.form', $form); |
|
| 129 | - |
|
| 130 | - return $this->app['former.form']; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // Dispatch to Form\Group |
|
| 134 | - if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 135 | - return $group; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // Dispatch to Form\Actions |
|
| 139 | - if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 140 | - return $actions; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Dispatch to macros |
|
| 144 | - if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 145 | - return $macro; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Checking for any supplementary classes |
|
| 149 | - $modifiers = explode('_', $method); |
|
| 150 | - $method = array_pop($modifiers); |
|
| 16 | + // Instances |
|
| 17 | + //////////////////////////////////////////////////////////////////// |
|
| 18 | + |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The current environment |
|
| 22 | + * |
|
| 23 | + * @var \Illuminate\Container\Container |
|
| 24 | + */ |
|
| 25 | + protected $app; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The Method Dispatcher |
|
| 29 | + * |
|
| 30 | + * @var MethodDispatcher |
|
| 31 | + */ |
|
| 32 | + protected $dispatch; |
|
| 33 | + |
|
| 34 | + // Informations |
|
| 35 | + //////////////////////////////////////////////////////////////////// |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The form's errors |
|
| 39 | + * |
|
| 40 | + * @var Message |
|
| 41 | + */ |
|
| 42 | + protected $errors; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * An array of rules to use |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + protected $rules = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * An array of field macros |
|
| 53 | + * |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $macros = array(); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The labels created so far |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + public $labels = array(); |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The IDs created so far |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + public $ids = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * A lookup table where the key is the input name, |
|
| 74 | + * and the value is number of times seen. This is |
|
| 75 | + * used to calculate unique ids. |
|
| 76 | + * |
|
| 77 | + * @var array |
|
| 78 | + */ |
|
| 79 | + public $names = array(); |
|
| 80 | + |
|
| 81 | + // Namespaces |
|
| 82 | + //////////////////////////////////////////////////////////////////// |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The namespace of Form elements |
|
| 86 | + */ |
|
| 87 | + const FORMSPACE = 'Former\Form\\'; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * The namespace of fields |
|
| 91 | + */ |
|
| 92 | + const FIELDSPACE = 'Former\Form\Fields\\'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Build a new Former instance |
|
| 96 | + * |
|
| 97 | + * @param Container $app |
|
| 98 | + * @param MethodDispatcher $dispatcher |
|
| 99 | + */ |
|
| 100 | + public function __construct(Container $app, MethodDispatcher $dispatcher) |
|
| 101 | + { |
|
| 102 | + $this->app = $app; |
|
| 103 | + $this->dispatch = $dispatcher; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + //////////////////////////////////////////////////////////////////// |
|
| 107 | + //////////////////////////// INTERFACE ///////////////////////////// |
|
| 108 | + //////////////////////////////////////////////////////////////////// |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Acts as a router that redirects methods to all of Former classes |
|
| 112 | + * |
|
| 113 | + * @param string $method The method called |
|
| 114 | + * @param array $parameters An array of parameters |
|
| 115 | + * |
|
| 116 | + * @return mixed |
|
| 117 | + */ |
|
| 118 | + public function __call($method, $parameters) |
|
| 119 | + { |
|
| 120 | + // Dispatch to Form\Elements |
|
| 121 | + // Explicitly check false since closeGroup() may return an empty string |
|
| 122 | + if (($element = $this->dispatch->toElements($method, $parameters)) !== false) { |
|
| 123 | + return $element; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + // Dispatch to Form\Form |
|
| 127 | + if ($form = $this->dispatch->toForm($method, $parameters)) { |
|
| 128 | + $this->app->instance('former.form', $form); |
|
| 129 | + |
|
| 130 | + return $this->app['former.form']; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // Dispatch to Form\Group |
|
| 134 | + if ($group = $this->dispatch->toGroup($method, $parameters)) { |
|
| 135 | + return $group; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // Dispatch to Form\Actions |
|
| 139 | + if ($actions = $this->dispatch->toActions($method, $parameters)) { |
|
| 140 | + return $actions; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Dispatch to macros |
|
| 144 | + if ($macro = $this->dispatch->toMacros($method, $parameters)) { |
|
| 145 | + return $macro; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // Checking for any supplementary classes |
|
| 149 | + $modifiers = explode('_', $method); |
|
| 150 | + $method = array_pop($modifiers); |
|
| 151 | 151 | |
| 152 | - // Dispatch to the different Form\Fields |
|
| 153 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | - $field->setModifiers($modifiers); |
|
| 155 | - $field->addClass(''); |
|
| 152 | + // Dispatch to the different Form\Fields |
|
| 153 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | + $field->setModifiers($modifiers); |
|
| 155 | + $field->addClass(''); |
|
| 156 | 156 | |
| 157 | - // Else bind field |
|
| 158 | - $this->app->instance('former.field', $field); |
|
| 159 | - |
|
| 160 | - return $this->app['former.field']; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - //////////////////////////////////////////////////////////////////// |
|
| 164 | - //////////////////////////////// MACROS //////////////////////////// |
|
| 165 | - //////////////////////////////////////////////////////////////////// |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Register a macro with Former |
|
| 169 | - * |
|
| 170 | - * @param string $name The name of the macro |
|
| 171 | - * @param Callable $macro The macro itself |
|
| 172 | - * |
|
| 173 | - * @return mixed |
|
| 174 | - */ |
|
| 175 | - public function macro($name, $macro) |
|
| 176 | - { |
|
| 177 | - $this->macros[$name] = $macro; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Check if a macro exists |
|
| 182 | - * |
|
| 183 | - * @param string $name |
|
| 184 | - * |
|
| 185 | - * @return boolean |
|
| 186 | - */ |
|
| 187 | - public function hasMacro($name) |
|
| 188 | - { |
|
| 189 | - return isset($this->macros[$name]); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get a registered macro |
|
| 194 | - * |
|
| 195 | - * @param string $name |
|
| 196 | - * |
|
| 197 | - * @return Closure |
|
| 198 | - */ |
|
| 199 | - public function getMacro($name) |
|
| 200 | - { |
|
| 201 | - return $this->macros[$name]; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - ///////////////////////////// POPULATOR //////////////////////////// |
|
| 206 | - //////////////////////////////////////////////////////////////////// |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Add values to populate the array |
|
| 210 | - * |
|
| 211 | - * @param mixed $values Can be an Eloquent object or an array |
|
| 212 | - */ |
|
| 213 | - public function populate($values) |
|
| 214 | - { |
|
| 215 | - $this->app['former.populator']->replace($values); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Set the value of a particular field |
|
| 220 | - * |
|
| 221 | - * @param string $field The field's name |
|
| 222 | - * @param mixed $value Its new value |
|
| 223 | - */ |
|
| 224 | - public function populateField($field, $value) |
|
| 225 | - { |
|
| 226 | - $this->app['former.populator']->put($field, $value); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * Get the value of a field |
|
| 231 | - * |
|
| 232 | - * @param string $field The field's name |
|
| 233 | - * @param null $fallback |
|
| 234 | - * |
|
| 235 | - * @return mixed |
|
| 236 | - */ |
|
| 237 | - public function getValue($field, $fallback = null) |
|
| 238 | - { |
|
| 239 | - return $this->app['former.populator']->get($field, $fallback); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Fetch a field value from both the new and old POST array |
|
| 244 | - * |
|
| 245 | - * @param string $name A field name |
|
| 246 | - * @param string $fallback A fallback if nothing was found |
|
| 247 | - * |
|
| 248 | - * @return string The results |
|
| 249 | - */ |
|
| 250 | - public function getPost($name, $fallback = null) |
|
| 251 | - { |
|
| 252 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 253 | - $name = trim($name, '.'); |
|
| 254 | - $oldValue = $this->app['request']->old($name, $fallback); |
|
| 255 | - |
|
| 256 | - return $this->app['request']->input($name, $oldValue, true); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - //////////////////////////////////////////////////////////////////// |
|
| 260 | - ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 261 | - //////////////////////////////////////////////////////////////////// |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Set the errors to use for validations |
|
| 265 | - * |
|
| 266 | - * @param Message $validator The result from a validation |
|
| 267 | - * |
|
| 268 | - * @return void |
|
| 269 | - */ |
|
| 270 | - public function withErrors($validator = null) |
|
| 271 | - { |
|
| 272 | - // Try to get the errors form the session |
|
| 273 | - if ($this->app['session']->has('errors')) { |
|
| 274 | - $this->errors = $this->app['session']->get('errors'); |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // If we're given a raw Validator, go fetch the errors in it |
|
| 278 | - if ($validator instanceof Validator) { |
|
| 279 | - $this->errors = $validator->getMessageBag(); |
|
| 280 | - } else { |
|
| 281 | - if ($validator instanceof MessageBag) { |
|
| 282 | - $this->errors = $validator; |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - return $this->errors; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Add live validation rules |
|
| 291 | - * |
|
| 292 | - * @param array *$rules An array of Laravel rules |
|
| 293 | - * |
|
| 294 | - * @return void |
|
| 295 | - */ |
|
| 296 | - public function withRules() |
|
| 297 | - { |
|
| 298 | - $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 299 | - |
|
| 300 | - // Parse the rules according to Laravel conventions |
|
| 301 | - foreach ($rules as $name => $fieldRules) { |
|
| 302 | - $expFieldRules = $fieldRules; |
|
| 303 | - if (!is_array($expFieldRules)) { |
|
| 304 | - $expFieldRules = explode('|', $expFieldRules); |
|
| 305 | - $expFieldRules = array_map('trim', $expFieldRules); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - foreach ($expFieldRules as $rule) { |
|
| 309 | - |
|
| 310 | - $parameters = null; |
|
| 311 | - |
|
| 312 | - if (($colon = strpos($rule, ':')) !== false) { |
|
| 313 | - $rulename = substr($rule, 0, $colon); |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 317 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 318 | - */ |
|
| 319 | - if ($rulename !== 'regex') { |
|
| 320 | - $parameters = str_getcsv(substr($rule, $colon + 1)); |
|
| 321 | - } else { |
|
| 322 | - $parameters = [substr($rule, $colon + 1)]; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - // Exclude unsupported rules |
|
| 327 | - $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 328 | - |
|
| 329 | - // Store processed rule in Former's array |
|
| 330 | - if (!isset($parameters)) { |
|
| 331 | - $parameters = array(); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $this->rules[$name][$rule] = $parameters; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Switch the framework used by Former |
|
| 341 | - * |
|
| 342 | - * @param string $framework The name of the framework to use |
|
| 343 | - */ |
|
| 344 | - public function framework($framework = null) |
|
| 345 | - { |
|
| 346 | - if (!$framework) { |
|
| 347 | - return $this->app['former.framework']->current(); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - $this->setOption('framework', $framework); |
|
| 351 | - |
|
| 352 | - $framework = $this->getFrameworkInstance($framework); |
|
| 353 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 354 | - return $framework; |
|
| 355 | - }); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Get a new framework instance |
|
| 360 | - * |
|
| 361 | - * @param string $framework |
|
| 362 | - * |
|
| 363 | - * @throws Exceptions\InvalidFrameworkException |
|
| 364 | - * @return \Former\Interfaces\FrameworkInterface |
|
| 365 | - */ |
|
| 366 | - public function getFrameworkInstance($framework) |
|
| 367 | - { |
|
| 368 | - $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 369 | - |
|
| 370 | - //get interfaces of the given framework |
|
| 371 | - $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 372 | - |
|
| 373 | - if(class_exists($formerClass)) { |
|
| 374 | - $returnClass = $formerClass; |
|
| 375 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | - // We have some outside class, lets return it. |
|
| 377 | - $returnClass = $framework; |
|
| 378 | - } else { |
|
| 379 | - throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - return new $returnClass($this->app); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Get an option from the config |
|
| 387 | - * |
|
| 388 | - * @param string $option The option |
|
| 389 | - * @param mixed $default Optional fallback |
|
| 390 | - * |
|
| 391 | - * @return mixed |
|
| 392 | - */ |
|
| 393 | - public function getOption($option, $default = null) |
|
| 394 | - { |
|
| 395 | - return $this->app['config']->get('former.'.$option, $default); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Set an option on the config |
|
| 400 | - * |
|
| 401 | - * @param string $option |
|
| 402 | - * @param string $value |
|
| 403 | - */ |
|
| 404 | - public function setOption($option, $value) |
|
| 405 | - { |
|
| 406 | - return $this->app['config']->set('former.'.$option, $value); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - //////////////////////////////////////////////////////////////////// |
|
| 410 | - ////////////////////////////// BUILDERS //////////////////////////// |
|
| 411 | - //////////////////////////////////////////////////////////////////// |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Closes a form |
|
| 415 | - * |
|
| 416 | - * @return string A form closing tag |
|
| 417 | - */ |
|
| 418 | - public function close() |
|
| 419 | - { |
|
| 420 | - if ($this->app->bound('former.form')) { |
|
| 421 | - $closing = $this->app['former.form']->close(); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // Destroy instances |
|
| 425 | - $instances = array('former.form', 'former.form.framework'); |
|
| 426 | - foreach ($instances as $instance) { |
|
| 427 | - $this->app[$instance] = null; |
|
| 428 | - unset($this->app[$instance]); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - // Reset populator |
|
| 432 | - $this->app['former.populator']->reset(); |
|
| 433 | - |
|
| 434 | - // Reset all values |
|
| 435 | - $this->errors = null; |
|
| 436 | - $this->rules = array(); |
|
| 437 | - |
|
| 438 | - return isset($closing) ? $closing : null; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - //////////////////////////////////////////////////////////////////// |
|
| 442 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 443 | - //////////////////////////////////////////////////////////////////// |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Get the errors for the current field |
|
| 447 | - * |
|
| 448 | - * @param string $name A field name |
|
| 449 | - * |
|
| 450 | - * @return string An error message |
|
| 451 | - */ |
|
| 452 | - public function getErrors($name = null) |
|
| 453 | - { |
|
| 454 | - // Get name and translate array notation |
|
| 455 | - if (!$name and $this->app['former.field']) { |
|
| 456 | - $name = $this->app['former.field']->getName(); |
|
| 457 | - |
|
| 458 | - // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 459 | - if (!$name) { |
|
| 460 | - return ''; |
|
| 461 | - } |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - if ($this->errors and $name) { |
|
| 465 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 466 | - $name = trim($name, '.'); |
|
| 467 | - |
|
| 468 | - return $this->errors->first($name); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - return $this->errors; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Get a rule from the Rules array |
|
| 476 | - * |
|
| 477 | - * @param string $name The field to fetch |
|
| 478 | - * |
|
| 479 | - * @return array An array of rules |
|
| 480 | - */ |
|
| 481 | - public function getRules($name) |
|
| 482 | - { |
|
| 483 | - // Check the rules for the name as given |
|
| 484 | - $ruleset = array_get($this->rules, $name); |
|
| 485 | - |
|
| 486 | - // If no rules found, convert to dot notation and try again |
|
| 487 | - if (is_null($ruleset)) { |
|
| 488 | - $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 489 | - $name = trim($name, '.'); |
|
| 490 | - $ruleset = array_get($this->rules, $name); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - return $ruleset; |
|
| 494 | - } |
|
| 157 | + // Else bind field |
|
| 158 | + $this->app->instance('former.field', $field); |
|
| 159 | + |
|
| 160 | + return $this->app['former.field']; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + //////////////////////////////////////////////////////////////////// |
|
| 164 | + //////////////////////////////// MACROS //////////////////////////// |
|
| 165 | + //////////////////////////////////////////////////////////////////// |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Register a macro with Former |
|
| 169 | + * |
|
| 170 | + * @param string $name The name of the macro |
|
| 171 | + * @param Callable $macro The macro itself |
|
| 172 | + * |
|
| 173 | + * @return mixed |
|
| 174 | + */ |
|
| 175 | + public function macro($name, $macro) |
|
| 176 | + { |
|
| 177 | + $this->macros[$name] = $macro; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Check if a macro exists |
|
| 182 | + * |
|
| 183 | + * @param string $name |
|
| 184 | + * |
|
| 185 | + * @return boolean |
|
| 186 | + */ |
|
| 187 | + public function hasMacro($name) |
|
| 188 | + { |
|
| 189 | + return isset($this->macros[$name]); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get a registered macro |
|
| 194 | + * |
|
| 195 | + * @param string $name |
|
| 196 | + * |
|
| 197 | + * @return Closure |
|
| 198 | + */ |
|
| 199 | + public function getMacro($name) |
|
| 200 | + { |
|
| 201 | + return $this->macros[$name]; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + ///////////////////////////// POPULATOR //////////////////////////// |
|
| 206 | + //////////////////////////////////////////////////////////////////// |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Add values to populate the array |
|
| 210 | + * |
|
| 211 | + * @param mixed $values Can be an Eloquent object or an array |
|
| 212 | + */ |
|
| 213 | + public function populate($values) |
|
| 214 | + { |
|
| 215 | + $this->app['former.populator']->replace($values); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Set the value of a particular field |
|
| 220 | + * |
|
| 221 | + * @param string $field The field's name |
|
| 222 | + * @param mixed $value Its new value |
|
| 223 | + */ |
|
| 224 | + public function populateField($field, $value) |
|
| 225 | + { |
|
| 226 | + $this->app['former.populator']->put($field, $value); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * Get the value of a field |
|
| 231 | + * |
|
| 232 | + * @param string $field The field's name |
|
| 233 | + * @param null $fallback |
|
| 234 | + * |
|
| 235 | + * @return mixed |
|
| 236 | + */ |
|
| 237 | + public function getValue($field, $fallback = null) |
|
| 238 | + { |
|
| 239 | + return $this->app['former.populator']->get($field, $fallback); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Fetch a field value from both the new and old POST array |
|
| 244 | + * |
|
| 245 | + * @param string $name A field name |
|
| 246 | + * @param string $fallback A fallback if nothing was found |
|
| 247 | + * |
|
| 248 | + * @return string The results |
|
| 249 | + */ |
|
| 250 | + public function getPost($name, $fallback = null) |
|
| 251 | + { |
|
| 252 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 253 | + $name = trim($name, '.'); |
|
| 254 | + $oldValue = $this->app['request']->old($name, $fallback); |
|
| 255 | + |
|
| 256 | + return $this->app['request']->input($name, $oldValue, true); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + //////////////////////////////////////////////////////////////////// |
|
| 260 | + ////////////////////////////// TOOLKIT ///////////////////////////// |
|
| 261 | + //////////////////////////////////////////////////////////////////// |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Set the errors to use for validations |
|
| 265 | + * |
|
| 266 | + * @param Message $validator The result from a validation |
|
| 267 | + * |
|
| 268 | + * @return void |
|
| 269 | + */ |
|
| 270 | + public function withErrors($validator = null) |
|
| 271 | + { |
|
| 272 | + // Try to get the errors form the session |
|
| 273 | + if ($this->app['session']->has('errors')) { |
|
| 274 | + $this->errors = $this->app['session']->get('errors'); |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // If we're given a raw Validator, go fetch the errors in it |
|
| 278 | + if ($validator instanceof Validator) { |
|
| 279 | + $this->errors = $validator->getMessageBag(); |
|
| 280 | + } else { |
|
| 281 | + if ($validator instanceof MessageBag) { |
|
| 282 | + $this->errors = $validator; |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return $this->errors; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Add live validation rules |
|
| 291 | + * |
|
| 292 | + * @param array *$rules An array of Laravel rules |
|
| 293 | + * |
|
| 294 | + * @return void |
|
| 295 | + */ |
|
| 296 | + public function withRules() |
|
| 297 | + { |
|
| 298 | + $rules = call_user_func_array('array_merge', func_get_args()); |
|
| 299 | + |
|
| 300 | + // Parse the rules according to Laravel conventions |
|
| 301 | + foreach ($rules as $name => $fieldRules) { |
|
| 302 | + $expFieldRules = $fieldRules; |
|
| 303 | + if (!is_array($expFieldRules)) { |
|
| 304 | + $expFieldRules = explode('|', $expFieldRules); |
|
| 305 | + $expFieldRules = array_map('trim', $expFieldRules); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + foreach ($expFieldRules as $rule) { |
|
| 309 | + |
|
| 310 | + $parameters = null; |
|
| 311 | + |
|
| 312 | + if (($colon = strpos($rule, ':')) !== false) { |
|
| 313 | + $rulename = substr($rule, 0, $colon); |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 317 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 318 | + */ |
|
| 319 | + if ($rulename !== 'regex') { |
|
| 320 | + $parameters = str_getcsv(substr($rule, $colon + 1)); |
|
| 321 | + } else { |
|
| 322 | + $parameters = [substr($rule, $colon + 1)]; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + // Exclude unsupported rules |
|
| 327 | + $rule = is_numeric($colon) ? substr($rule, 0, $colon) : $rule; |
|
| 328 | + |
|
| 329 | + // Store processed rule in Former's array |
|
| 330 | + if (!isset($parameters)) { |
|
| 331 | + $parameters = array(); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $this->rules[$name][$rule] = $parameters; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Switch the framework used by Former |
|
| 341 | + * |
|
| 342 | + * @param string $framework The name of the framework to use |
|
| 343 | + */ |
|
| 344 | + public function framework($framework = null) |
|
| 345 | + { |
|
| 346 | + if (!$framework) { |
|
| 347 | + return $this->app['former.framework']->current(); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + $this->setOption('framework', $framework); |
|
| 351 | + |
|
| 352 | + $framework = $this->getFrameworkInstance($framework); |
|
| 353 | + $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 354 | + return $framework; |
|
| 355 | + }); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Get a new framework instance |
|
| 360 | + * |
|
| 361 | + * @param string $framework |
|
| 362 | + * |
|
| 363 | + * @throws Exceptions\InvalidFrameworkException |
|
| 364 | + * @return \Former\Interfaces\FrameworkInterface |
|
| 365 | + */ |
|
| 366 | + public function getFrameworkInstance($framework) |
|
| 367 | + { |
|
| 368 | + $formerClass = __NAMESPACE__.'\Framework\\'.$framework; |
|
| 369 | + |
|
| 370 | + //get interfaces of the given framework |
|
| 371 | + $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
|
| 372 | + |
|
| 373 | + if(class_exists($formerClass)) { |
|
| 374 | + $returnClass = $formerClass; |
|
| 375 | + } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | + // We have some outside class, lets return it. |
|
| 377 | + $returnClass = $framework; |
|
| 378 | + } else { |
|
| 379 | + throw (new InvalidFrameworkException())->setFramework($framework); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + return new $returnClass($this->app); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Get an option from the config |
|
| 387 | + * |
|
| 388 | + * @param string $option The option |
|
| 389 | + * @param mixed $default Optional fallback |
|
| 390 | + * |
|
| 391 | + * @return mixed |
|
| 392 | + */ |
|
| 393 | + public function getOption($option, $default = null) |
|
| 394 | + { |
|
| 395 | + return $this->app['config']->get('former.'.$option, $default); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Set an option on the config |
|
| 400 | + * |
|
| 401 | + * @param string $option |
|
| 402 | + * @param string $value |
|
| 403 | + */ |
|
| 404 | + public function setOption($option, $value) |
|
| 405 | + { |
|
| 406 | + return $this->app['config']->set('former.'.$option, $value); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + //////////////////////////////////////////////////////////////////// |
|
| 410 | + ////////////////////////////// BUILDERS //////////////////////////// |
|
| 411 | + //////////////////////////////////////////////////////////////////// |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Closes a form |
|
| 415 | + * |
|
| 416 | + * @return string A form closing tag |
|
| 417 | + */ |
|
| 418 | + public function close() |
|
| 419 | + { |
|
| 420 | + if ($this->app->bound('former.form')) { |
|
| 421 | + $closing = $this->app['former.form']->close(); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // Destroy instances |
|
| 425 | + $instances = array('former.form', 'former.form.framework'); |
|
| 426 | + foreach ($instances as $instance) { |
|
| 427 | + $this->app[$instance] = null; |
|
| 428 | + unset($this->app[$instance]); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + // Reset populator |
|
| 432 | + $this->app['former.populator']->reset(); |
|
| 433 | + |
|
| 434 | + // Reset all values |
|
| 435 | + $this->errors = null; |
|
| 436 | + $this->rules = array(); |
|
| 437 | + |
|
| 438 | + return isset($closing) ? $closing : null; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + //////////////////////////////////////////////////////////////////// |
|
| 442 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 443 | + //////////////////////////////////////////////////////////////////// |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Get the errors for the current field |
|
| 447 | + * |
|
| 448 | + * @param string $name A field name |
|
| 449 | + * |
|
| 450 | + * @return string An error message |
|
| 451 | + */ |
|
| 452 | + public function getErrors($name = null) |
|
| 453 | + { |
|
| 454 | + // Get name and translate array notation |
|
| 455 | + if (!$name and $this->app['former.field']) { |
|
| 456 | + $name = $this->app['former.field']->getName(); |
|
| 457 | + |
|
| 458 | + // Always return empty string for anonymous fields (i.e. fields with no name/id) |
|
| 459 | + if (!$name) { |
|
| 460 | + return ''; |
|
| 461 | + } |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + if ($this->errors and $name) { |
|
| 465 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 466 | + $name = trim($name, '.'); |
|
| 467 | + |
|
| 468 | + return $this->errors->first($name); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + return $this->errors; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Get a rule from the Rules array |
|
| 476 | + * |
|
| 477 | + * @param string $name The field to fetch |
|
| 478 | + * |
|
| 479 | + * @return array An array of rules |
|
| 480 | + */ |
|
| 481 | + public function getRules($name) |
|
| 482 | + { |
|
| 483 | + // Check the rules for the name as given |
|
| 484 | + $ruleset = array_get($this->rules, $name); |
|
| 485 | + |
|
| 486 | + // If no rules found, convert to dot notation and try again |
|
| 487 | + if (is_null($ruleset)) { |
|
| 488 | + $name = str_replace(array('[', ']'), array('.', ''), $name); |
|
| 489 | + $name = trim($name, '.'); |
|
| 490 | + $ruleset = array_get($this->rules, $name); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + return $ruleset; |
|
| 494 | + } |
|
| 495 | 495 | } |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | // Checking for any supplementary classes |
| 149 | 149 | $modifiers = explode('_', $method); |
| 150 | - $method = array_pop($modifiers); |
|
| 150 | + $method = array_pop($modifiers); |
|
| 151 | 151 | |
| 152 | 152 | // Dispatch to the different Form\Fields |
| 153 | - $field = $this->dispatch->toFields($method, $parameters); |
|
| 153 | + $field = $this->dispatch->toFields($method, $parameters); |
|
| 154 | 154 | $field->setModifiers($modifiers); |
| 155 | 155 | $field->addClass(''); |
| 156 | 156 | |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | $this->setOption('framework', $framework); |
| 351 | 351 | |
| 352 | 352 | $framework = $this->getFrameworkInstance($framework); |
| 353 | - $this->app->bind('former.framework', function ($app) use ($framework) { |
|
| 353 | + $this->app->bind('former.framework', function($app) use ($framework) { |
|
| 354 | 354 | return $framework; |
| 355 | 355 | }); |
| 356 | 356 | } |
@@ -370,9 +370,9 @@ discard block |
||
| 370 | 370 | //get interfaces of the given framework |
| 371 | 371 | $interfaces = class_exists($framework) ? class_implements($framework) : array(); |
| 372 | 372 | |
| 373 | - if(class_exists($formerClass)) { |
|
| 373 | + if (class_exists($formerClass)) { |
|
| 374 | 374 | $returnClass = $formerClass; |
| 375 | - } elseif(class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 375 | + } elseif (class_exists($framework) && isset($interfaces['Former\Interfaces\FrameworkInterface'])) { |
|
| 376 | 376 | // We have some outside class, lets return it. |
| 377 | 377 | $returnClass = $framework; |
| 378 | 378 | } else { |
@@ -139,7 +139,7 @@ |
||
| 139 | 139 | $classes = array_intersect($classes, $this->fields); |
| 140 | 140 | |
| 141 | 141 | // Prepend field type |
| 142 | - $classes = array_map(function ($class) { |
|
| 142 | + $classes = array_map(function($class) { |
|
| 143 | 143 | return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
| 144 | 144 | }, $classes); |
| 145 | 145 | |
@@ -345,7 +345,7 @@ |
||
| 345 | 345 | * @param string $text |
| 346 | 346 | * @param array $attributes |
| 347 | 347 | * |
| 348 | - * @return string |
|
| 348 | + * @return Element |
|
| 349 | 349 | */ |
| 350 | 350 | public function createValidationError($text, $attributes = array()) |
| 351 | 351 | { |
@@ -13,459 +13,459 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap4 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 .= " col-$viewports[$viewport]-offset-$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 | - $classes[] = 'form-check'; |
|
| 219 | - |
|
| 220 | - if (in_array('inline', $classes)) { |
|
| 221 | - $field->inline(); |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // Filter classes according to field type |
|
| 226 | - if ($field->isButton()) { |
|
| 227 | - $classes = $this->filterButtonClasses($classes); |
|
| 228 | - } else { |
|
| 229 | - $classes = $this->filterFieldClasses($classes); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - // Add form-control class for text-type, textarea and select fields |
|
| 233 | - // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 234 | - if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array( |
|
| 235 | - 'file', |
|
| 236 | - 'plaintext', |
|
| 237 | - )) and !in_array('form-control', $classes) |
|
| 238 | - ) { |
|
| 239 | - $classes[] = 'form-control'; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - if ($this->app['former']->getErrors($field->getName())) { |
|
| 243 | - $classes[] = $this->errorState(); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return $this->addClassesToField($field, $classes); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Add group classes |
|
| 251 | - * |
|
| 252 | - * @return string A list of group classes |
|
| 253 | - */ |
|
| 254 | - public function getGroupClasses() |
|
| 255 | - { |
|
| 256 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 257 | - return 'form-group row'; |
|
| 258 | - } else { |
|
| 259 | - return 'form-group'; |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Add label classes |
|
| 265 | - * |
|
| 266 | - * @return string[] An array of attributes with the label class |
|
| 267 | - */ |
|
| 268 | - public function getLabelClasses() |
|
| 269 | - { |
|
| 270 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 271 | - return array('control-label', $this->labelWidth); |
|
| 272 | - } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 273 | - return array('sr-only'); |
|
| 274 | - } else { |
|
| 275 | - return array('control-label'); |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Add uneditable field classes |
|
| 281 | - * |
|
| 282 | - * @return string An array of attributes with the uneditable class |
|
| 283 | - */ |
|
| 284 | - public function getUneditableClasses() |
|
| 285 | - { |
|
| 286 | - return ''; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Add plain text field classes |
|
| 291 | - * |
|
| 292 | - * @return string An array of attributes with the plain text class |
|
| 293 | - */ |
|
| 294 | - public function getPlainTextClasses() |
|
| 295 | - { |
|
| 296 | - return 'form-control-static'; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Add form class |
|
| 301 | - * |
|
| 302 | - * @param string $type The type of form to add |
|
| 303 | - * |
|
| 304 | - * @return string|null |
|
| 305 | - */ |
|
| 306 | - public function getFormClasses($type) |
|
| 307 | - { |
|
| 308 | - return $type ? 'form-'.$type : null; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Add actions block class |
|
| 313 | - * |
|
| 314 | - * @return string|null |
|
| 315 | - */ |
|
| 316 | - public function getActionClasses() |
|
| 317 | - { |
|
| 318 | - if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 319 | - return 'form-group row'; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - return null; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - //////////////////////////////////////////////////////////////////// |
|
| 326 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 327 | - //////////////////////////////////////////////////////////////////// |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Render an help text |
|
| 331 | - * |
|
| 332 | - * @param string $text |
|
| 333 | - * @param array $attributes |
|
| 334 | - * |
|
| 335 | - * @return Element |
|
| 336 | - */ |
|
| 337 | - public function createHelp($text, $attributes = array()) |
|
| 338 | - { |
|
| 339 | - return Element::create('small', $text, $attributes)->addClass('text-muted'); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Render an validation error text |
|
| 344 | - * |
|
| 345 | - * @param string $text |
|
| 346 | - * @param array $attributes |
|
| 347 | - * |
|
| 348 | - * @return string |
|
| 349 | - */ |
|
| 350 | - public function createValidationError($text, $attributes = array()) |
|
| 351 | - { |
|
| 352 | - return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Render an help text |
|
| 357 | - * |
|
| 358 | - * @param string $text |
|
| 359 | - * @param array $attributes |
|
| 360 | - * |
|
| 361 | - * @return Element |
|
| 362 | - */ |
|
| 363 | - public function createBlockHelp($text, $attributes = array()) |
|
| 364 | - { |
|
| 365 | - return Element::create('small', $text, $attributes)->addClass('form-text text-muted'); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Render a disabled field |
|
| 370 | - * |
|
| 371 | - * @param Field $field |
|
| 372 | - * |
|
| 373 | - * @return Element |
|
| 374 | - */ |
|
| 375 | - public function createDisabledField(Field $field) |
|
| 376 | - { |
|
| 377 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Render a plain text field |
|
| 382 | - * |
|
| 383 | - * @param Field $field |
|
| 384 | - * |
|
| 385 | - * @return Element |
|
| 386 | - */ |
|
| 387 | - public function createPlainTextField(Field $field) |
|
| 388 | - { |
|
| 389 | - $label = $field->getLabel(); |
|
| 390 | - if ($label) { |
|
| 391 | - $label->for(''); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - //////////////////////////////////////////////////////////////////// |
|
| 398 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 399 | - //////////////////////////////////////////////////////////////////// |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Wrap an item to be prepended or appended to the current field |
|
| 403 | - * |
|
| 404 | - * @return Element A wrapped item |
|
| 405 | - */ |
|
| 406 | - public function placeAround($item) |
|
| 407 | - { |
|
| 408 | - // Render object |
|
| 409 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 410 | - $item = $item->__toString(); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - // Get class to use |
|
| 414 | - $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon'; |
|
| 415 | - |
|
| 416 | - return Element::create('span', $item)->addClass('input-group-'.$class); |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Wrap a field with prepended and appended items |
|
| 421 | - * |
|
| 422 | - * @param Field $field |
|
| 423 | - * @param array $prepend |
|
| 424 | - * @param array $append |
|
| 425 | - * |
|
| 426 | - * @return string A field concatented with prepended and/or appended items |
|
| 427 | - */ |
|
| 428 | - public function prependAppend($field, $prepend, $append) |
|
| 429 | - { |
|
| 430 | - $return = '<div class="input-group">'; |
|
| 431 | - $return .= join(null, $prepend); |
|
| 432 | - $return .= $field->render(); |
|
| 433 | - $return .= join(null, $append); |
|
| 434 | - $return .= '</div>'; |
|
| 435 | - |
|
| 436 | - return $return; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Wrap a field with potential additional tags |
|
| 441 | - * |
|
| 442 | - * @param Field $field |
|
| 443 | - * |
|
| 444 | - * @return Element A wrapped field |
|
| 445 | - */ |
|
| 446 | - public function wrapField($field) |
|
| 447 | - { |
|
| 448 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 449 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - return $field; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Wrap actions block with potential additional tags |
|
| 457 | - * |
|
| 458 | - * @param Actions $actions |
|
| 459 | - * |
|
| 460 | - * @return string A wrapped actions block |
|
| 461 | - */ |
|
| 462 | - public function wrapActions($actions) |
|
| 463 | - { |
|
| 464 | - // For horizontal forms, we wrap the actions in a div |
|
| 465 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 466 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - return $actions; |
|
| 470 | - } |
|
| 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 .= " col-$viewports[$viewport]-offset-$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 | + $classes[] = 'form-check'; |
|
| 219 | + |
|
| 220 | + if (in_array('inline', $classes)) { |
|
| 221 | + $field->inline(); |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // Filter classes according to field type |
|
| 226 | + if ($field->isButton()) { |
|
| 227 | + $classes = $this->filterButtonClasses($classes); |
|
| 228 | + } else { |
|
| 229 | + $classes = $this->filterFieldClasses($classes); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + // Add form-control class for text-type, textarea and select fields |
|
| 233 | + // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 234 | + if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array( |
|
| 235 | + 'file', |
|
| 236 | + 'plaintext', |
|
| 237 | + )) and !in_array('form-control', $classes) |
|
| 238 | + ) { |
|
| 239 | + $classes[] = 'form-control'; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + if ($this->app['former']->getErrors($field->getName())) { |
|
| 243 | + $classes[] = $this->errorState(); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return $this->addClassesToField($field, $classes); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Add group classes |
|
| 251 | + * |
|
| 252 | + * @return string A list of group classes |
|
| 253 | + */ |
|
| 254 | + public function getGroupClasses() |
|
| 255 | + { |
|
| 256 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 257 | + return 'form-group row'; |
|
| 258 | + } else { |
|
| 259 | + return 'form-group'; |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Add label classes |
|
| 265 | + * |
|
| 266 | + * @return string[] An array of attributes with the label class |
|
| 267 | + */ |
|
| 268 | + public function getLabelClasses() |
|
| 269 | + { |
|
| 270 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 271 | + return array('control-label', $this->labelWidth); |
|
| 272 | + } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 273 | + return array('sr-only'); |
|
| 274 | + } else { |
|
| 275 | + return array('control-label'); |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Add uneditable field classes |
|
| 281 | + * |
|
| 282 | + * @return string An array of attributes with the uneditable class |
|
| 283 | + */ |
|
| 284 | + public function getUneditableClasses() |
|
| 285 | + { |
|
| 286 | + return ''; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Add plain text field classes |
|
| 291 | + * |
|
| 292 | + * @return string An array of attributes with the plain text class |
|
| 293 | + */ |
|
| 294 | + public function getPlainTextClasses() |
|
| 295 | + { |
|
| 296 | + return 'form-control-static'; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Add form class |
|
| 301 | + * |
|
| 302 | + * @param string $type The type of form to add |
|
| 303 | + * |
|
| 304 | + * @return string|null |
|
| 305 | + */ |
|
| 306 | + public function getFormClasses($type) |
|
| 307 | + { |
|
| 308 | + return $type ? 'form-'.$type : null; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Add actions block class |
|
| 313 | + * |
|
| 314 | + * @return string|null |
|
| 315 | + */ |
|
| 316 | + public function getActionClasses() |
|
| 317 | + { |
|
| 318 | + if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 319 | + return 'form-group row'; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + return null; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + //////////////////////////////////////////////////////////////////// |
|
| 326 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 327 | + //////////////////////////////////////////////////////////////////// |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Render an help text |
|
| 331 | + * |
|
| 332 | + * @param string $text |
|
| 333 | + * @param array $attributes |
|
| 334 | + * |
|
| 335 | + * @return Element |
|
| 336 | + */ |
|
| 337 | + public function createHelp($text, $attributes = array()) |
|
| 338 | + { |
|
| 339 | + return Element::create('small', $text, $attributes)->addClass('text-muted'); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Render an validation error text |
|
| 344 | + * |
|
| 345 | + * @param string $text |
|
| 346 | + * @param array $attributes |
|
| 347 | + * |
|
| 348 | + * @return string |
|
| 349 | + */ |
|
| 350 | + public function createValidationError($text, $attributes = array()) |
|
| 351 | + { |
|
| 352 | + return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Render an help text |
|
| 357 | + * |
|
| 358 | + * @param string $text |
|
| 359 | + * @param array $attributes |
|
| 360 | + * |
|
| 361 | + * @return Element |
|
| 362 | + */ |
|
| 363 | + public function createBlockHelp($text, $attributes = array()) |
|
| 364 | + { |
|
| 365 | + return Element::create('small', $text, $attributes)->addClass('form-text text-muted'); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Render a disabled field |
|
| 370 | + * |
|
| 371 | + * @param Field $field |
|
| 372 | + * |
|
| 373 | + * @return Element |
|
| 374 | + */ |
|
| 375 | + public function createDisabledField(Field $field) |
|
| 376 | + { |
|
| 377 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Render a plain text field |
|
| 382 | + * |
|
| 383 | + * @param Field $field |
|
| 384 | + * |
|
| 385 | + * @return Element |
|
| 386 | + */ |
|
| 387 | + public function createPlainTextField(Field $field) |
|
| 388 | + { |
|
| 389 | + $label = $field->getLabel(); |
|
| 390 | + if ($label) { |
|
| 391 | + $label->for(''); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + //////////////////////////////////////////////////////////////////// |
|
| 398 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 399 | + //////////////////////////////////////////////////////////////////// |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Wrap an item to be prepended or appended to the current field |
|
| 403 | + * |
|
| 404 | + * @return Element A wrapped item |
|
| 405 | + */ |
|
| 406 | + public function placeAround($item) |
|
| 407 | + { |
|
| 408 | + // Render object |
|
| 409 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 410 | + $item = $item->__toString(); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + // Get class to use |
|
| 414 | + $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon'; |
|
| 415 | + |
|
| 416 | + return Element::create('span', $item)->addClass('input-group-'.$class); |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Wrap a field with prepended and appended items |
|
| 421 | + * |
|
| 422 | + * @param Field $field |
|
| 423 | + * @param array $prepend |
|
| 424 | + * @param array $append |
|
| 425 | + * |
|
| 426 | + * @return string A field concatented with prepended and/or appended items |
|
| 427 | + */ |
|
| 428 | + public function prependAppend($field, $prepend, $append) |
|
| 429 | + { |
|
| 430 | + $return = '<div class="input-group">'; |
|
| 431 | + $return .= join(null, $prepend); |
|
| 432 | + $return .= $field->render(); |
|
| 433 | + $return .= join(null, $append); |
|
| 434 | + $return .= '</div>'; |
|
| 435 | + |
|
| 436 | + return $return; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Wrap a field with potential additional tags |
|
| 441 | + * |
|
| 442 | + * @param Field $field |
|
| 443 | + * |
|
| 444 | + * @return Element A wrapped field |
|
| 445 | + */ |
|
| 446 | + public function wrapField($field) |
|
| 447 | + { |
|
| 448 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 449 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + return $field; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Wrap actions block with potential additional tags |
|
| 457 | + * |
|
| 458 | + * @param Actions $actions |
|
| 459 | + * |
|
| 460 | + * @return string A wrapped actions block |
|
| 461 | + */ |
|
| 462 | + public function wrapActions($actions) |
|
| 463 | + { |
|
| 464 | + // For horizontal forms, we wrap the actions in a div |
|
| 465 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 466 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + return $actions; |
|
| 470 | + } |
|
| 471 | 471 | } |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | |
| 351 | 351 | // In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if |
| 352 | 352 | // rendering inline. |
| 353 | - $class = ($this->app['former']->framework() == 'TwitterBootstrap3' || |
|
| 353 | + $class = ($this->app['former']->framework() == 'TwitterBootstrap3' || |
|
| 354 | 354 | $this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline; |
| 355 | 355 | |
| 356 | 356 | // Merge custom attributes with global attributes |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | $element = (is_object($field)) ? $field->render() : $field; |
| 382 | 382 | } elseif ($this->app['former']->framework() == 'TwitterBootstrap4') { |
| 383 | 383 | // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
| 384 | - $element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render(); |
|
| 384 | + $element = $field.Element::create('label', $label)->for($attributes['id'])->class($class)->render(); |
|
| 385 | 385 | |
| 386 | 386 | $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
| 387 | 387 | |
@@ -421,7 +421,7 @@ discard block |
||
| 421 | 421 | $this->app['former']->labels[] = $name; |
| 422 | 422 | |
| 423 | 423 | // Count number of fields with the same ID |
| 424 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 424 | + $where = array_filter($this->app['former']->labels, function($label) use ($name) { |
|
| 425 | 425 | return $label == $name; |
| 426 | 426 | }); |
| 427 | 427 | $unique = sizeof($where); |
@@ -12,560 +12,560 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class Checkable extends Field |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Renders the checkables as inline |
|
| 17 | - * |
|
| 18 | - * @var boolean |
|
| 19 | - */ |
|
| 20 | - protected $inline = false; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Add a text to a single element |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $text = null; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Renders the checkables as grouped |
|
| 31 | - * |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $grouped = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The checkable items currently stored |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $items = array(); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The type of checkable item |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 48 | - protected $checkable = null; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * An array of checked items |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $checked = array(); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The checkable currently being focused on |
|
| 59 | - * |
|
| 60 | - * @var integer |
|
| 61 | - */ |
|
| 62 | - protected $focus = null; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Whether this particular checkable is to be pushed |
|
| 66 | - * |
|
| 67 | - * @var boolean |
|
| 68 | - */ |
|
| 69 | - protected $isPushed = null; |
|
| 70 | - |
|
| 71 | - //////////////////////////////////////////////////////////////////// |
|
| 72 | - //////////////////////////// CORE METHODS ////////////////////////// |
|
| 73 | - //////////////////////////////////////////////////////////////////// |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Build a new checkable |
|
| 77 | - * |
|
| 78 | - * @param Container $app |
|
| 79 | - * @param string $type |
|
| 80 | - * @param array $name |
|
| 81 | - * @param $label |
|
| 82 | - * @param $value |
|
| 83 | - * @param $attributes |
|
| 84 | - */ |
|
| 85 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 86 | - { |
|
| 87 | - // Unify auto and chained methods of grouping checkboxes |
|
| 88 | - if (ends_with($name, '[]')) { |
|
| 89 | - $name = substr($name, 0, -2); |
|
| 90 | - $this->grouped(); |
|
| 91 | - } |
|
| 92 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 93 | - |
|
| 94 | - if (is_array($this->value)) { |
|
| 95 | - $this->items($this->value); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Apply methods to focused checkable |
|
| 101 | - * |
|
| 102 | - * @param string $method |
|
| 103 | - * @param array $parameters |
|
| 104 | - * |
|
| 105 | - * @return $this |
|
| 106 | - */ |
|
| 107 | - public function __call($method, $parameters) |
|
| 108 | - { |
|
| 109 | - $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0)); |
|
| 110 | - if ($focused) { |
|
| 111 | - return $this; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return parent::__call($method, $parameters); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Prints out the currently stored checkables |
|
| 119 | - */ |
|
| 120 | - public function render() |
|
| 121 | - { |
|
| 122 | - $html = null; |
|
| 123 | - |
|
| 124 | - $this->setFieldClasses(); |
|
| 125 | - |
|
| 126 | - // Multiple items |
|
| 127 | - if ($this->items) { |
|
| 128 | - unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 129 | - foreach ($this->items as $key => $item) { |
|
| 130 | - $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 131 | - $html .= $this->createCheckable($item, $value); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $html; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // Single item |
|
| 138 | - return $this->createCheckable(array( |
|
| 139 | - 'name' => $this->name, |
|
| 140 | - 'label' => $this->text, |
|
| 141 | - 'value' => $this->value, |
|
| 142 | - 'attributes' => $this->attributes, |
|
| 143 | - )); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - //////////////////////////////////////////////////////////////////// |
|
| 147 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Focus on a particular checkable |
|
| 152 | - * |
|
| 153 | - * @param integer $on The checkable to focus on |
|
| 154 | - * |
|
| 155 | - * @return $this |
|
| 156 | - */ |
|
| 157 | - public function on($on) |
|
| 158 | - { |
|
| 159 | - if (!isset($this->items[$on])) { |
|
| 160 | - return $this; |
|
| 161 | - } else { |
|
| 162 | - $this->focus = $on; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $this; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Set the checkables as inline |
|
| 170 | - */ |
|
| 171 | - public function inline($isInline = true) |
|
| 172 | - { |
|
| 173 | - $this->inline = $isInline; |
|
| 174 | - |
|
| 175 | - return $this; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Set the checkables as stacked |
|
| 180 | - */ |
|
| 181 | - public function stacked($isStacked = true) |
|
| 182 | - { |
|
| 183 | - $this->inline = !$isStacked; |
|
| 184 | - |
|
| 185 | - return $this; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Set the checkables as grouped |
|
| 190 | - */ |
|
| 191 | - public function grouped($isGrouped = true) |
|
| 192 | - { |
|
| 193 | - $this->grouped = $isGrouped; |
|
| 194 | - |
|
| 195 | - return $this; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Add text to a single checkable |
|
| 200 | - * |
|
| 201 | - * @param string $text The checkable label |
|
| 202 | - * |
|
| 203 | - * @return $this |
|
| 204 | - */ |
|
| 205 | - public function text($text) |
|
| 206 | - { |
|
| 207 | - // Translate and format |
|
| 208 | - $text = Helpers::translate($text); |
|
| 209 | - |
|
| 210 | - // Apply on focused if any |
|
| 211 | - $focused = $this->setOnFocused('label', $text); |
|
| 212 | - if ($focused) { |
|
| 213 | - return $this; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $this->text = $text; |
|
| 217 | - |
|
| 218 | - return $this; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Push this particular checkbox |
|
| 223 | - * |
|
| 224 | - * @param boolean $pushed |
|
| 225 | - * |
|
| 226 | - * @return $this |
|
| 227 | - */ |
|
| 228 | - public function push($pushed = true) |
|
| 229 | - { |
|
| 230 | - $this->isPushed = $pushed; |
|
| 231 | - |
|
| 232 | - return $this; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Check a specific item |
|
| 237 | - * |
|
| 238 | - * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 239 | - * |
|
| 240 | - * @return $this |
|
| 241 | - */ |
|
| 242 | - public function check($checked = true) |
|
| 243 | - { |
|
| 244 | - // If we're setting all the checked items at once |
|
| 245 | - if (is_array($checked)) { |
|
| 246 | - $this->checked = $checked; |
|
| 247 | - // Checking an item in particular |
|
| 248 | - } elseif (is_string($checked) or is_int($checked)) { |
|
| 249 | - $this->checked[$checked] = true; |
|
| 250 | - // Only setting a single item |
|
| 251 | - } else { |
|
| 252 | - $this->checked[$this->name] = (bool) $checked; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $this; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - //////////////////////////////////////////////////////////////////// |
|
| 259 | - ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 260 | - //////////////////////////////////////////////////////////////////// |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Creates a series of checkable items |
|
| 264 | - * |
|
| 265 | - * @param array $_items Items to create |
|
| 266 | - */ |
|
| 267 | - protected function items($_items) |
|
| 268 | - { |
|
| 269 | - // If passing an array |
|
| 270 | - if (sizeof($_items) == 1 and |
|
| 271 | - isset($_items[0]) and |
|
| 272 | - is_array($_items[0]) |
|
| 273 | - ) { |
|
| 274 | - $_items = $_items[0]; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // Fetch models if that's what we were passed |
|
| 278 | - if (isset($_items[0]) and is_object($_items[0])) { |
|
| 279 | - $_items = Helpers::queryToArray($_items); |
|
| 280 | - $_items = array_flip($_items); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - // Iterate through items, assign a name and a label to each |
|
| 284 | - $count = 0; |
|
| 285 | - foreach ($_items as $label => $name) { |
|
| 286 | - |
|
| 287 | - // Define a fallback name in case none is found |
|
| 288 | - $fallback = $this->isCheckbox() |
|
| 289 | - ? $this->name.'_'.$count |
|
| 290 | - : $this->name; |
|
| 291 | - |
|
| 292 | - // Grouped fields |
|
| 293 | - if ($this->isGrouped()) { |
|
| 294 | - $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 295 | - $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // If we haven't any name defined for the checkable, try to compute some |
|
| 299 | - if (!is_string($label) and !is_array($name)) { |
|
| 300 | - $label = $name; |
|
| 301 | - $name = $fallback; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - // If we gave custom information on the item, add them |
|
| 305 | - if (is_array($name)) { |
|
| 306 | - $attributes = $name; |
|
| 307 | - $name = array_get($attributes, 'name', $fallback); |
|
| 308 | - unset($attributes['name']); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // Store all informations we have in an array |
|
| 312 | - $item = array( |
|
| 313 | - 'name' => $name, |
|
| 314 | - 'label' => Helpers::translate($label), |
|
| 315 | - 'count' => $count, |
|
| 316 | - ); |
|
| 317 | - if (isset($attributes)) { |
|
| 318 | - $item['attributes'] = $attributes; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $this->items[] = $item; |
|
| 322 | - $count++; |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Renders a checkable |
|
| 328 | - * |
|
| 329 | - * @param string|array $item A checkable item |
|
| 330 | - * @param integer $fallbackValue A fallback value if none is set |
|
| 331 | - * |
|
| 332 | - * @return string |
|
| 333 | - */ |
|
| 334 | - protected function createCheckable($item, $fallbackValue = 1) |
|
| 335 | - { |
|
| 336 | - // Extract informations |
|
| 337 | - extract($item); |
|
| 338 | - |
|
| 339 | - // Set default values |
|
| 340 | - if (!isset($attributes)) { |
|
| 341 | - $attributes = array(); |
|
| 342 | - } |
|
| 343 | - if (isset($attributes['value'])) { |
|
| 344 | - $value = $attributes['value']; |
|
| 345 | - } |
|
| 346 | - if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 347 | - $value = $fallbackValue; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - // If inline items, add class |
|
| 351 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 352 | - |
|
| 353 | - // In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if |
|
| 354 | - // rendering inline. |
|
| 355 | - $class = ($this->app['former']->framework() == 'TwitterBootstrap3' || |
|
| 356 | - $this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline; |
|
| 357 | - |
|
| 358 | - // Merge custom attributes with global attributes |
|
| 359 | - $attributes = array_merge($this->attributes, $attributes); |
|
| 360 | - if (!isset($attributes['id'])) { |
|
| 361 | - $attributes['id'] = $name.$this->unique($name); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - // Create field |
|
| 365 | - $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes); |
|
| 366 | - if ($this->isChecked($item, $value)) { |
|
| 367 | - $field->checked('checked'); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - // Add hidden checkbox if requested |
|
| 371 | - if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 372 | - if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 373 | - $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 374 | - |
|
| 375 | - // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 376 | - // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 377 | - $this->app->instance('former.field', $this); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // If no label to wrap, return plain checkable |
|
| 382 | - if (!$label) { |
|
| 383 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 384 | - } elseif ($this->app['former']->framework() == 'TwitterBootstrap4') { |
|
| 385 | - // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 386 | - $element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render(); |
|
| 387 | - |
|
| 388 | - $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 15 | + /** |
|
| 16 | + * Renders the checkables as inline |
|
| 17 | + * |
|
| 18 | + * @var boolean |
|
| 19 | + */ |
|
| 20 | + protected $inline = false; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Add a text to a single element |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $text = null; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Renders the checkables as grouped |
|
| 31 | + * |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $grouped = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The checkable items currently stored |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $items = array(); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The type of checkable item |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | + protected $checkable = null; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * An array of checked items |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $checked = array(); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The checkable currently being focused on |
|
| 59 | + * |
|
| 60 | + * @var integer |
|
| 61 | + */ |
|
| 62 | + protected $focus = null; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Whether this particular checkable is to be pushed |
|
| 66 | + * |
|
| 67 | + * @var boolean |
|
| 68 | + */ |
|
| 69 | + protected $isPushed = null; |
|
| 70 | + |
|
| 71 | + //////////////////////////////////////////////////////////////////// |
|
| 72 | + //////////////////////////// CORE METHODS ////////////////////////// |
|
| 73 | + //////////////////////////////////////////////////////////////////// |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Build a new checkable |
|
| 77 | + * |
|
| 78 | + * @param Container $app |
|
| 79 | + * @param string $type |
|
| 80 | + * @param array $name |
|
| 81 | + * @param $label |
|
| 82 | + * @param $value |
|
| 83 | + * @param $attributes |
|
| 84 | + */ |
|
| 85 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 86 | + { |
|
| 87 | + // Unify auto and chained methods of grouping checkboxes |
|
| 88 | + if (ends_with($name, '[]')) { |
|
| 89 | + $name = substr($name, 0, -2); |
|
| 90 | + $this->grouped(); |
|
| 91 | + } |
|
| 92 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 93 | + |
|
| 94 | + if (is_array($this->value)) { |
|
| 95 | + $this->items($this->value); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Apply methods to focused checkable |
|
| 101 | + * |
|
| 102 | + * @param string $method |
|
| 103 | + * @param array $parameters |
|
| 104 | + * |
|
| 105 | + * @return $this |
|
| 106 | + */ |
|
| 107 | + public function __call($method, $parameters) |
|
| 108 | + { |
|
| 109 | + $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0)); |
|
| 110 | + if ($focused) { |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return parent::__call($method, $parameters); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Prints out the currently stored checkables |
|
| 119 | + */ |
|
| 120 | + public function render() |
|
| 121 | + { |
|
| 122 | + $html = null; |
|
| 123 | + |
|
| 124 | + $this->setFieldClasses(); |
|
| 125 | + |
|
| 126 | + // Multiple items |
|
| 127 | + if ($this->items) { |
|
| 128 | + unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 129 | + foreach ($this->items as $key => $item) { |
|
| 130 | + $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 131 | + $html .= $this->createCheckable($item, $value); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $html; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // Single item |
|
| 138 | + return $this->createCheckable(array( |
|
| 139 | + 'name' => $this->name, |
|
| 140 | + 'label' => $this->text, |
|
| 141 | + 'value' => $this->value, |
|
| 142 | + 'attributes' => $this->attributes, |
|
| 143 | + )); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + //////////////////////////////////////////////////////////////////// |
|
| 147 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Focus on a particular checkable |
|
| 152 | + * |
|
| 153 | + * @param integer $on The checkable to focus on |
|
| 154 | + * |
|
| 155 | + * @return $this |
|
| 156 | + */ |
|
| 157 | + public function on($on) |
|
| 158 | + { |
|
| 159 | + if (!isset($this->items[$on])) { |
|
| 160 | + return $this; |
|
| 161 | + } else { |
|
| 162 | + $this->focus = $on; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $this; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Set the checkables as inline |
|
| 170 | + */ |
|
| 171 | + public function inline($isInline = true) |
|
| 172 | + { |
|
| 173 | + $this->inline = $isInline; |
|
| 174 | + |
|
| 175 | + return $this; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Set the checkables as stacked |
|
| 180 | + */ |
|
| 181 | + public function stacked($isStacked = true) |
|
| 182 | + { |
|
| 183 | + $this->inline = !$isStacked; |
|
| 184 | + |
|
| 185 | + return $this; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Set the checkables as grouped |
|
| 190 | + */ |
|
| 191 | + public function grouped($isGrouped = true) |
|
| 192 | + { |
|
| 193 | + $this->grouped = $isGrouped; |
|
| 194 | + |
|
| 195 | + return $this; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Add text to a single checkable |
|
| 200 | + * |
|
| 201 | + * @param string $text The checkable label |
|
| 202 | + * |
|
| 203 | + * @return $this |
|
| 204 | + */ |
|
| 205 | + public function text($text) |
|
| 206 | + { |
|
| 207 | + // Translate and format |
|
| 208 | + $text = Helpers::translate($text); |
|
| 209 | + |
|
| 210 | + // Apply on focused if any |
|
| 211 | + $focused = $this->setOnFocused('label', $text); |
|
| 212 | + if ($focused) { |
|
| 213 | + return $this; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $this->text = $text; |
|
| 217 | + |
|
| 218 | + return $this; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Push this particular checkbox |
|
| 223 | + * |
|
| 224 | + * @param boolean $pushed |
|
| 225 | + * |
|
| 226 | + * @return $this |
|
| 227 | + */ |
|
| 228 | + public function push($pushed = true) |
|
| 229 | + { |
|
| 230 | + $this->isPushed = $pushed; |
|
| 231 | + |
|
| 232 | + return $this; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Check a specific item |
|
| 237 | + * |
|
| 238 | + * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 239 | + * |
|
| 240 | + * @return $this |
|
| 241 | + */ |
|
| 242 | + public function check($checked = true) |
|
| 243 | + { |
|
| 244 | + // If we're setting all the checked items at once |
|
| 245 | + if (is_array($checked)) { |
|
| 246 | + $this->checked = $checked; |
|
| 247 | + // Checking an item in particular |
|
| 248 | + } elseif (is_string($checked) or is_int($checked)) { |
|
| 249 | + $this->checked[$checked] = true; |
|
| 250 | + // Only setting a single item |
|
| 251 | + } else { |
|
| 252 | + $this->checked[$this->name] = (bool) $checked; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $this; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + //////////////////////////////////////////////////////////////////// |
|
| 259 | + ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 260 | + //////////////////////////////////////////////////////////////////// |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Creates a series of checkable items |
|
| 264 | + * |
|
| 265 | + * @param array $_items Items to create |
|
| 266 | + */ |
|
| 267 | + protected function items($_items) |
|
| 268 | + { |
|
| 269 | + // If passing an array |
|
| 270 | + if (sizeof($_items) == 1 and |
|
| 271 | + isset($_items[0]) and |
|
| 272 | + is_array($_items[0]) |
|
| 273 | + ) { |
|
| 274 | + $_items = $_items[0]; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Fetch models if that's what we were passed |
|
| 278 | + if (isset($_items[0]) and is_object($_items[0])) { |
|
| 279 | + $_items = Helpers::queryToArray($_items); |
|
| 280 | + $_items = array_flip($_items); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + // Iterate through items, assign a name and a label to each |
|
| 284 | + $count = 0; |
|
| 285 | + foreach ($_items as $label => $name) { |
|
| 286 | + |
|
| 287 | + // Define a fallback name in case none is found |
|
| 288 | + $fallback = $this->isCheckbox() |
|
| 289 | + ? $this->name.'_'.$count |
|
| 290 | + : $this->name; |
|
| 291 | + |
|
| 292 | + // Grouped fields |
|
| 293 | + if ($this->isGrouped()) { |
|
| 294 | + $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 295 | + $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // If we haven't any name defined for the checkable, try to compute some |
|
| 299 | + if (!is_string($label) and !is_array($name)) { |
|
| 300 | + $label = $name; |
|
| 301 | + $name = $fallback; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + // If we gave custom information on the item, add them |
|
| 305 | + if (is_array($name)) { |
|
| 306 | + $attributes = $name; |
|
| 307 | + $name = array_get($attributes, 'name', $fallback); |
|
| 308 | + unset($attributes['name']); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // Store all informations we have in an array |
|
| 312 | + $item = array( |
|
| 313 | + 'name' => $name, |
|
| 314 | + 'label' => Helpers::translate($label), |
|
| 315 | + 'count' => $count, |
|
| 316 | + ); |
|
| 317 | + if (isset($attributes)) { |
|
| 318 | + $item['attributes'] = $attributes; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $this->items[] = $item; |
|
| 322 | + $count++; |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Renders a checkable |
|
| 328 | + * |
|
| 329 | + * @param string|array $item A checkable item |
|
| 330 | + * @param integer $fallbackValue A fallback value if none is set |
|
| 331 | + * |
|
| 332 | + * @return string |
|
| 333 | + */ |
|
| 334 | + protected function createCheckable($item, $fallbackValue = 1) |
|
| 335 | + { |
|
| 336 | + // Extract informations |
|
| 337 | + extract($item); |
|
| 338 | + |
|
| 339 | + // Set default values |
|
| 340 | + if (!isset($attributes)) { |
|
| 341 | + $attributes = array(); |
|
| 342 | + } |
|
| 343 | + if (isset($attributes['value'])) { |
|
| 344 | + $value = $attributes['value']; |
|
| 345 | + } |
|
| 346 | + if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 347 | + $value = $fallbackValue; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + // If inline items, add class |
|
| 351 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 352 | + |
|
| 353 | + // In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if |
|
| 354 | + // rendering inline. |
|
| 355 | + $class = ($this->app['former']->framework() == 'TwitterBootstrap3' || |
|
| 356 | + $this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline; |
|
| 357 | + |
|
| 358 | + // Merge custom attributes with global attributes |
|
| 359 | + $attributes = array_merge($this->attributes, $attributes); |
|
| 360 | + if (!isset($attributes['id'])) { |
|
| 361 | + $attributes['id'] = $name.$this->unique($name); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + // Create field |
|
| 365 | + $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes); |
|
| 366 | + if ($this->isChecked($item, $value)) { |
|
| 367 | + $field->checked('checked'); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + // Add hidden checkbox if requested |
|
| 371 | + if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 372 | + if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 373 | + $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 374 | + |
|
| 375 | + // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 376 | + // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 377 | + $this->app->instance('former.field', $this); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // If no label to wrap, return plain checkable |
|
| 382 | + if (!$label) { |
|
| 383 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 384 | + } elseif ($this->app['former']->framework() == 'TwitterBootstrap4') { |
|
| 385 | + // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 386 | + $element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render(); |
|
| 387 | + |
|
| 388 | + $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 389 | 389 | |
| 390 | - $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 391 | - |
|
| 392 | - } else { |
|
| 393 | - // Original way is to add the 'input' inside the 'label' |
|
| 394 | - $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 398 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 399 | - $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 400 | - if ($this->getAttribute('disabled')) { |
|
| 401 | - $wrapper->addClass('disabled'); |
|
| 402 | - } |
|
| 403 | - $element = $wrapper->render(); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - // Return the field |
|
| 407 | - return $element; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - //////////////////////////////////////////////////////////////////// |
|
| 411 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 412 | - //////////////////////////////////////////////////////////////////// |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Generate an unique ID for a field |
|
| 416 | - * |
|
| 417 | - * @param string $name The field's name |
|
| 418 | - * |
|
| 419 | - * @return string A field number to use |
|
| 420 | - */ |
|
| 421 | - protected function unique($name) |
|
| 422 | - { |
|
| 423 | - $this->app['former']->labels[] = $name; |
|
| 424 | - |
|
| 425 | - // Count number of fields with the same ID |
|
| 426 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 427 | - return $label == $name; |
|
| 428 | - }); |
|
| 429 | - $unique = sizeof($where); |
|
| 430 | - |
|
| 431 | - // In case the field doesn't need to be numbered |
|
| 432 | - if ($unique < 2 or empty($this->items)) { |
|
| 433 | - return false; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $unique; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Set something on the currently focused checkable |
|
| 441 | - * |
|
| 442 | - * @param string $attribute The key to set |
|
| 443 | - * @param string $value Its value |
|
| 444 | - * |
|
| 445 | - * @return $this|bool |
|
| 446 | - */ |
|
| 447 | - protected function setOnFocused($attribute, $value) |
|
| 448 | - { |
|
| 449 | - if (is_null($this->focus)) { |
|
| 450 | - return false; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value); |
|
| 454 | - |
|
| 455 | - return $this; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Check if a checkable is checked |
|
| 460 | - * |
|
| 461 | - * @return boolean Checked or not |
|
| 462 | - */ |
|
| 463 | - protected function isChecked($item = null, $value = null) |
|
| 464 | - { |
|
| 465 | - if (isset($item['name'])) { |
|
| 466 | - $name = $item['name']; |
|
| 467 | - } |
|
| 468 | - if (empty($name)) { |
|
| 469 | - $name = $this->name; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 473 | - // Or if it's a single radio, simply see if we called check |
|
| 474 | - if ($this->isCheckbox() or |
|
| 475 | - !$this->isCheckbox() and !$this->items |
|
| 476 | - ) { |
|
| 477 | - $checked = array_get($this->checked, $name, false); |
|
| 478 | - |
|
| 479 | - // If there are multiple, search for the value |
|
| 480 | - // as the name are the same between radios |
|
| 481 | - } else { |
|
| 482 | - $checked = array_get($this->checked, $value, false); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - // Check the values and POST array |
|
| 486 | - if ($this->isGrouped()) { |
|
| 487 | - // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 488 | - $groupIndex = self::getGroupIndexFromItem($item); |
|
| 489 | - |
|
| 490 | - // Search using the bare name, not the individual item name |
|
| 491 | - $post = $this->app['former']->getPost($this->name); |
|
| 492 | - $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 493 | - |
|
| 494 | - if (isset($post[$groupIndex])) { |
|
| 495 | - $post = $post[$groupIndex]; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 500 | - * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 501 | - * |
|
| 502 | - * array( |
|
| 503 | - * 'name' = 'roles[foo]', |
|
| 504 | - * 'value' => 'foo', |
|
| 505 | - * ) |
|
| 506 | - */ |
|
| 507 | - if ($static instanceof Collection) { |
|
| 508 | - // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 509 | - foreach ($static as $staticItem) { |
|
| 510 | - $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 511 | - if ($staticItemValue == $groupIndex) { |
|
| 512 | - $static = $staticItemValue; |
|
| 513 | - break; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - } else if (isset($static[$groupIndex])) { |
|
| 517 | - $static = $static[$groupIndex]; |
|
| 518 | - } |
|
| 519 | - } else { |
|
| 520 | - $post = $this->app['former']->getPost($name); |
|
| 521 | - $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 525 | - $isChecked = ($post == $value); |
|
| 526 | - } elseif (!is_null($static)) { |
|
| 527 | - $isChecked = ($static == $value); |
|
| 528 | - } else { |
|
| 529 | - $isChecked = $checked; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - return $isChecked ? true : false; |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * Check if the current element is a checkbox |
|
| 537 | - * |
|
| 538 | - * @return boolean Checkbox or radio |
|
| 539 | - */ |
|
| 540 | - protected function isCheckbox() |
|
| 541 | - { |
|
| 542 | - return $this->checkable == 'checkbox'; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Check if the checkables are grouped or not |
|
| 547 | - * |
|
| 548 | - * @return boolean |
|
| 549 | - */ |
|
| 550 | - protected function isGrouped() |
|
| 551 | - { |
|
| 552 | - return |
|
| 553 | - $this->grouped == true or |
|
| 554 | - strpos($this->name, '[]') !== false; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @param array $item The item array, containing at least name and count keys. |
|
| 559 | - * |
|
| 560 | - * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 561 | - */ |
|
| 562 | - public static function getGroupIndexFromItem($item) |
|
| 563 | - { |
|
| 564 | - $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 565 | - if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 566 | - return $item['count']; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - return $groupIndex; |
|
| 570 | - } |
|
| 390 | + $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 391 | + |
|
| 392 | + } else { |
|
| 393 | + // Original way is to add the 'input' inside the 'label' |
|
| 394 | + $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 398 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 399 | + $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 400 | + if ($this->getAttribute('disabled')) { |
|
| 401 | + $wrapper->addClass('disabled'); |
|
| 402 | + } |
|
| 403 | + $element = $wrapper->render(); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + // Return the field |
|
| 407 | + return $element; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + //////////////////////////////////////////////////////////////////// |
|
| 411 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 412 | + //////////////////////////////////////////////////////////////////// |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Generate an unique ID for a field |
|
| 416 | + * |
|
| 417 | + * @param string $name The field's name |
|
| 418 | + * |
|
| 419 | + * @return string A field number to use |
|
| 420 | + */ |
|
| 421 | + protected function unique($name) |
|
| 422 | + { |
|
| 423 | + $this->app['former']->labels[] = $name; |
|
| 424 | + |
|
| 425 | + // Count number of fields with the same ID |
|
| 426 | + $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 427 | + return $label == $name; |
|
| 428 | + }); |
|
| 429 | + $unique = sizeof($where); |
|
| 430 | + |
|
| 431 | + // In case the field doesn't need to be numbered |
|
| 432 | + if ($unique < 2 or empty($this->items)) { |
|
| 433 | + return false; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $unique; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Set something on the currently focused checkable |
|
| 441 | + * |
|
| 442 | + * @param string $attribute The key to set |
|
| 443 | + * @param string $value Its value |
|
| 444 | + * |
|
| 445 | + * @return $this|bool |
|
| 446 | + */ |
|
| 447 | + protected function setOnFocused($attribute, $value) |
|
| 448 | + { |
|
| 449 | + if (is_null($this->focus)) { |
|
| 450 | + return false; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value); |
|
| 454 | + |
|
| 455 | + return $this; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Check if a checkable is checked |
|
| 460 | + * |
|
| 461 | + * @return boolean Checked or not |
|
| 462 | + */ |
|
| 463 | + protected function isChecked($item = null, $value = null) |
|
| 464 | + { |
|
| 465 | + if (isset($item['name'])) { |
|
| 466 | + $name = $item['name']; |
|
| 467 | + } |
|
| 468 | + if (empty($name)) { |
|
| 469 | + $name = $this->name; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 473 | + // Or if it's a single radio, simply see if we called check |
|
| 474 | + if ($this->isCheckbox() or |
|
| 475 | + !$this->isCheckbox() and !$this->items |
|
| 476 | + ) { |
|
| 477 | + $checked = array_get($this->checked, $name, false); |
|
| 478 | + |
|
| 479 | + // If there are multiple, search for the value |
|
| 480 | + // as the name are the same between radios |
|
| 481 | + } else { |
|
| 482 | + $checked = array_get($this->checked, $value, false); |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + // Check the values and POST array |
|
| 486 | + if ($this->isGrouped()) { |
|
| 487 | + // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 488 | + $groupIndex = self::getGroupIndexFromItem($item); |
|
| 489 | + |
|
| 490 | + // Search using the bare name, not the individual item name |
|
| 491 | + $post = $this->app['former']->getPost($this->name); |
|
| 492 | + $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 493 | + |
|
| 494 | + if (isset($post[$groupIndex])) { |
|
| 495 | + $post = $post[$groupIndex]; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 500 | + * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 501 | + * |
|
| 502 | + * array( |
|
| 503 | + * 'name' = 'roles[foo]', |
|
| 504 | + * 'value' => 'foo', |
|
| 505 | + * ) |
|
| 506 | + */ |
|
| 507 | + if ($static instanceof Collection) { |
|
| 508 | + // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 509 | + foreach ($static as $staticItem) { |
|
| 510 | + $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 511 | + if ($staticItemValue == $groupIndex) { |
|
| 512 | + $static = $staticItemValue; |
|
| 513 | + break; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + } else if (isset($static[$groupIndex])) { |
|
| 517 | + $static = $static[$groupIndex]; |
|
| 518 | + } |
|
| 519 | + } else { |
|
| 520 | + $post = $this->app['former']->getPost($name); |
|
| 521 | + $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 525 | + $isChecked = ($post == $value); |
|
| 526 | + } elseif (!is_null($static)) { |
|
| 527 | + $isChecked = ($static == $value); |
|
| 528 | + } else { |
|
| 529 | + $isChecked = $checked; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + return $isChecked ? true : false; |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * Check if the current element is a checkbox |
|
| 537 | + * |
|
| 538 | + * @return boolean Checkbox or radio |
|
| 539 | + */ |
|
| 540 | + protected function isCheckbox() |
|
| 541 | + { |
|
| 542 | + return $this->checkable == 'checkbox'; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Check if the checkables are grouped or not |
|
| 547 | + * |
|
| 548 | + * @return boolean |
|
| 549 | + */ |
|
| 550 | + protected function isGrouped() |
|
| 551 | + { |
|
| 552 | + return |
|
| 553 | + $this->grouped == true or |
|
| 554 | + strpos($this->name, '[]') !== false; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @param array $item The item array, containing at least name and count keys. |
|
| 559 | + * |
|
| 560 | + * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 561 | + */ |
|
| 562 | + public static function getGroupIndexFromItem($item) |
|
| 563 | + { |
|
| 564 | + $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 565 | + if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 566 | + return $item['count']; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + return $groupIndex; |
|
| 570 | + } |
|
| 571 | 571 | } |
@@ -1,207 +1,207 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | 2 | |
| 3 | - // Markup |
|
| 4 | - //////////////////////////////////////////////////////////////////// |
|
| 5 | - |
|
| 6 | - // Whether labels should be automatically computed from name |
|
| 7 | - 'automatic_label' => true, |
|
| 8 | - |
|
| 9 | - // The default form type |
|
| 10 | - 'default_form_type' => 'horizontal', |
|
| 11 | - |
|
| 12 | - // Validation |
|
| 13 | - //////////////////////////////////////////////////////////////////// |
|
| 14 | - |
|
| 15 | - // Whether Former should fetch errors from Session |
|
| 16 | - 'fetch_errors' => true, |
|
| 17 | - |
|
| 18 | - // Whether Former should try to apply Validator rules as attributes |
|
| 19 | - 'live_validation' => true, |
|
| 20 | - |
|
| 21 | - // Whether Former should automatically fetch error messages and |
|
| 22 | - // display them next to the matching fields |
|
| 23 | - 'error_messages' => true, |
|
| 24 | - |
|
| 25 | - // Checkables |
|
| 26 | - //////////////////////////////////////////////////////////////////// |
|
| 27 | - |
|
| 28 | - // Whether checkboxes should always be present in the POST data, |
|
| 29 | - // no matter if you checked them or not |
|
| 30 | - 'push_checkboxes' => false, |
|
| 31 | - |
|
| 32 | - // The value a checkbox will have in the POST array if unchecked |
|
| 33 | - 'unchecked_value' => 0, |
|
| 34 | - |
|
| 35 | - // Required fields |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - |
|
| 38 | - // The class to be added to required fields |
|
| 39 | - 'required_class' => 'required', |
|
| 40 | - |
|
| 41 | - // A facultative text to append to the labels of required fields |
|
| 42 | - 'required_text' => '<sup>*</sup>', |
|
| 43 | - |
|
| 44 | - // Translations |
|
| 45 | - //////////////////////////////////////////////////////////////////// |
|
| 46 | - |
|
| 47 | - // Where Former should look for translations |
|
| 48 | - 'translate_from' => 'validation.attributes', |
|
| 49 | - |
|
| 50 | - // Whether text that comes out of the translated |
|
| 51 | - // should be capitalized (ex: email => Email) automatically |
|
| 52 | - 'capitalize_translations' => true, |
|
| 53 | - |
|
| 54 | - // An array of attributes to automatically translate |
|
| 55 | - 'translatable' => array( |
|
| 56 | - 'help', |
|
| 57 | - 'inlineHelp', |
|
| 58 | - 'blockHelp', |
|
| 59 | - 'placeholder', |
|
| 60 | - 'data_placeholder', |
|
| 61 | - 'label', |
|
| 62 | - ), |
|
| 63 | - |
|
| 64 | - // Framework |
|
| 65 | - //////////////////////////////////////////////////////////////////// |
|
| 66 | - |
|
| 67 | - // The framework to be used by Former |
|
| 68 | - 'framework' => 'TwitterBootstrap3', |
|
| 69 | - |
|
| 70 | - 'TwitterBootstrap4' => array( |
|
| 71 | - |
|
| 72 | - // Map Former-supported viewports to Bootstrap 4 equivalents |
|
| 73 | - 'viewports' => array( |
|
| 74 | - 'large' => 'lg', |
|
| 75 | - 'medium' => 'md', |
|
| 76 | - 'small' => 'sm', |
|
| 77 | - 'mini' => 'xs', |
|
| 78 | - ), |
|
| 79 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 80 | - 'labelWidths' => array( |
|
| 81 | - 'large' => 2, |
|
| 82 | - 'small' => 4, |
|
| 83 | - ), |
|
| 84 | - // HTML markup and classes used by Bootstrap 5 for icons |
|
| 85 | - 'icon' => array( |
|
| 86 | - 'tag' => 'i', |
|
| 87 | - 'set' => 'fa', |
|
| 88 | - 'prefix' => 'fa', |
|
| 89 | - ), |
|
| 90 | - |
|
| 91 | - ), |
|
| 92 | - |
|
| 93 | - 'TwitterBootstrap3' => array( |
|
| 94 | - |
|
| 95 | - // Map Former-supported viewports to Bootstrap 3 equivalents |
|
| 96 | - 'viewports' => array( |
|
| 97 | - 'large' => 'lg', |
|
| 98 | - 'medium' => 'md', |
|
| 99 | - 'small' => 'sm', |
|
| 100 | - 'mini' => 'xs', |
|
| 101 | - ), |
|
| 102 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 103 | - 'labelWidths' => array( |
|
| 104 | - 'large' => 2, |
|
| 105 | - 'small' => 4, |
|
| 106 | - ), |
|
| 107 | - // HTML markup and classes used by Bootstrap 3 for icons |
|
| 108 | - 'icon' => array( |
|
| 109 | - 'tag' => 'span', |
|
| 110 | - 'set' => 'glyphicon', |
|
| 111 | - 'prefix' => 'glyphicon', |
|
| 112 | - ), |
|
| 113 | - |
|
| 114 | - ), |
|
| 115 | - |
|
| 116 | - 'Nude' => array( // No-framework markup |
|
| 117 | - 'icon' => array( |
|
| 118 | - 'tag' => 'i', |
|
| 119 | - 'set' => null, |
|
| 120 | - 'prefix' => 'icon', |
|
| 121 | - ), |
|
| 122 | - ), |
|
| 123 | - |
|
| 124 | - 'TwitterBootstrap' => array( // Twitter Bootstrap version 2 |
|
| 125 | - 'icon' => array( |
|
| 126 | - 'tag' => 'i', |
|
| 127 | - 'set' => null, |
|
| 128 | - 'prefix' => 'icon', |
|
| 129 | - ), |
|
| 130 | - ), |
|
| 131 | - |
|
| 132 | - 'ZurbFoundation5' => array( |
|
| 133 | - // Map Former-supported viewports to Foundation 5 equivalents |
|
| 134 | - 'viewports' => array( |
|
| 135 | - 'large' => 'large', |
|
| 136 | - 'medium' => null, |
|
| 137 | - 'small' => 'small', |
|
| 138 | - 'mini' => null, |
|
| 139 | - ), |
|
| 140 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 141 | - 'labelWidths' => array( |
|
| 142 | - 'small' => 3, |
|
| 143 | - ), |
|
| 144 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 145 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 146 | - // HTML markup and classes used by Foundation 5 for icons |
|
| 147 | - 'icon' => array( |
|
| 148 | - 'tag' => 'i', |
|
| 149 | - 'set' => null, |
|
| 150 | - 'prefix' => 'fi', |
|
| 151 | - ), |
|
| 152 | - // CSS for inline validation errors |
|
| 153 | - 'error_classes' => array('class' => 'error'), |
|
| 154 | - ), |
|
| 155 | - |
|
| 156 | - 'ZurbFoundation4' => array( |
|
| 157 | - // Foundation 4 also has an experimental "medium" breakpoint |
|
| 158 | - // explained at http://foundation.zurb.com/docs/components/grid.html |
|
| 159 | - 'viewports' => array( |
|
| 160 | - 'large' => 'large', |
|
| 161 | - 'medium' => null, |
|
| 162 | - 'small' => 'small', |
|
| 163 | - 'mini' => null, |
|
| 164 | - ), |
|
| 165 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 166 | - 'labelWidths' => array( |
|
| 167 | - 'small' => 3, |
|
| 168 | - ), |
|
| 169 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 170 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 171 | - // HTML markup and classes used by Foundation 4 for icons |
|
| 172 | - 'icon' => array( |
|
| 173 | - 'tag' => 'i', |
|
| 174 | - 'set' => 'general', |
|
| 175 | - 'prefix' => 'foundicon', |
|
| 176 | - ), |
|
| 177 | - // CSS for inline validation errors |
|
| 178 | - 'error_classes' => array('class' => 'alert-box radius warning'), |
|
| 179 | - ), |
|
| 180 | - |
|
| 181 | - 'ZurbFoundation' => array( // Foundation 3 |
|
| 182 | - 'viewports' => array( |
|
| 183 | - 'large' => '', |
|
| 184 | - 'medium' => null, |
|
| 185 | - 'small' => 'mobile-', |
|
| 186 | - 'mini' => null, |
|
| 187 | - ), |
|
| 188 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 189 | - 'labelWidths' => array( |
|
| 190 | - 'large' => 2, |
|
| 191 | - 'small' => 4, |
|
| 192 | - ), |
|
| 193 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 194 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 195 | - // HTML markup and classes used by Foundation 3 for icons |
|
| 196 | - 'icon' => array( |
|
| 197 | - 'tag' => 'i', |
|
| 198 | - 'set' => null, |
|
| 199 | - 'prefix' => 'fi', |
|
| 200 | - ), |
|
| 201 | - // CSS for inline validation errors |
|
| 202 | - // should work for Zurb 2 and 3 |
|
| 203 | - 'error_classes' => array('class' => 'alert-box alert error'), |
|
| 204 | - ), |
|
| 3 | + // Markup |
|
| 4 | + //////////////////////////////////////////////////////////////////// |
|
| 5 | + |
|
| 6 | + // Whether labels should be automatically computed from name |
|
| 7 | + 'automatic_label' => true, |
|
| 8 | + |
|
| 9 | + // The default form type |
|
| 10 | + 'default_form_type' => 'horizontal', |
|
| 11 | + |
|
| 12 | + // Validation |
|
| 13 | + //////////////////////////////////////////////////////////////////// |
|
| 14 | + |
|
| 15 | + // Whether Former should fetch errors from Session |
|
| 16 | + 'fetch_errors' => true, |
|
| 17 | + |
|
| 18 | + // Whether Former should try to apply Validator rules as attributes |
|
| 19 | + 'live_validation' => true, |
|
| 20 | + |
|
| 21 | + // Whether Former should automatically fetch error messages and |
|
| 22 | + // display them next to the matching fields |
|
| 23 | + 'error_messages' => true, |
|
| 24 | + |
|
| 25 | + // Checkables |
|
| 26 | + //////////////////////////////////////////////////////////////////// |
|
| 27 | + |
|
| 28 | + // Whether checkboxes should always be present in the POST data, |
|
| 29 | + // no matter if you checked them or not |
|
| 30 | + 'push_checkboxes' => false, |
|
| 31 | + |
|
| 32 | + // The value a checkbox will have in the POST array if unchecked |
|
| 33 | + 'unchecked_value' => 0, |
|
| 34 | + |
|
| 35 | + // Required fields |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + |
|
| 38 | + // The class to be added to required fields |
|
| 39 | + 'required_class' => 'required', |
|
| 40 | + |
|
| 41 | + // A facultative text to append to the labels of required fields |
|
| 42 | + 'required_text' => '<sup>*</sup>', |
|
| 43 | + |
|
| 44 | + // Translations |
|
| 45 | + //////////////////////////////////////////////////////////////////// |
|
| 46 | + |
|
| 47 | + // Where Former should look for translations |
|
| 48 | + 'translate_from' => 'validation.attributes', |
|
| 49 | + |
|
| 50 | + // Whether text that comes out of the translated |
|
| 51 | + // should be capitalized (ex: email => Email) automatically |
|
| 52 | + 'capitalize_translations' => true, |
|
| 53 | + |
|
| 54 | + // An array of attributes to automatically translate |
|
| 55 | + 'translatable' => array( |
|
| 56 | + 'help', |
|
| 57 | + 'inlineHelp', |
|
| 58 | + 'blockHelp', |
|
| 59 | + 'placeholder', |
|
| 60 | + 'data_placeholder', |
|
| 61 | + 'label', |
|
| 62 | + ), |
|
| 63 | + |
|
| 64 | + // Framework |
|
| 65 | + //////////////////////////////////////////////////////////////////// |
|
| 66 | + |
|
| 67 | + // The framework to be used by Former |
|
| 68 | + 'framework' => 'TwitterBootstrap3', |
|
| 69 | + |
|
| 70 | + 'TwitterBootstrap4' => array( |
|
| 71 | + |
|
| 72 | + // Map Former-supported viewports to Bootstrap 4 equivalents |
|
| 73 | + 'viewports' => array( |
|
| 74 | + 'large' => 'lg', |
|
| 75 | + 'medium' => 'md', |
|
| 76 | + 'small' => 'sm', |
|
| 77 | + 'mini' => 'xs', |
|
| 78 | + ), |
|
| 79 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 80 | + 'labelWidths' => array( |
|
| 81 | + 'large' => 2, |
|
| 82 | + 'small' => 4, |
|
| 83 | + ), |
|
| 84 | + // HTML markup and classes used by Bootstrap 5 for icons |
|
| 85 | + 'icon' => array( |
|
| 86 | + 'tag' => 'i', |
|
| 87 | + 'set' => 'fa', |
|
| 88 | + 'prefix' => 'fa', |
|
| 89 | + ), |
|
| 90 | + |
|
| 91 | + ), |
|
| 92 | + |
|
| 93 | + 'TwitterBootstrap3' => array( |
|
| 94 | + |
|
| 95 | + // Map Former-supported viewports to Bootstrap 3 equivalents |
|
| 96 | + 'viewports' => array( |
|
| 97 | + 'large' => 'lg', |
|
| 98 | + 'medium' => 'md', |
|
| 99 | + 'small' => 'sm', |
|
| 100 | + 'mini' => 'xs', |
|
| 101 | + ), |
|
| 102 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 103 | + 'labelWidths' => array( |
|
| 104 | + 'large' => 2, |
|
| 105 | + 'small' => 4, |
|
| 106 | + ), |
|
| 107 | + // HTML markup and classes used by Bootstrap 3 for icons |
|
| 108 | + 'icon' => array( |
|
| 109 | + 'tag' => 'span', |
|
| 110 | + 'set' => 'glyphicon', |
|
| 111 | + 'prefix' => 'glyphicon', |
|
| 112 | + ), |
|
| 113 | + |
|
| 114 | + ), |
|
| 115 | + |
|
| 116 | + 'Nude' => array( // No-framework markup |
|
| 117 | + 'icon' => array( |
|
| 118 | + 'tag' => 'i', |
|
| 119 | + 'set' => null, |
|
| 120 | + 'prefix' => 'icon', |
|
| 121 | + ), |
|
| 122 | + ), |
|
| 123 | + |
|
| 124 | + 'TwitterBootstrap' => array( // Twitter Bootstrap version 2 |
|
| 125 | + 'icon' => array( |
|
| 126 | + 'tag' => 'i', |
|
| 127 | + 'set' => null, |
|
| 128 | + 'prefix' => 'icon', |
|
| 129 | + ), |
|
| 130 | + ), |
|
| 131 | + |
|
| 132 | + 'ZurbFoundation5' => array( |
|
| 133 | + // Map Former-supported viewports to Foundation 5 equivalents |
|
| 134 | + 'viewports' => array( |
|
| 135 | + 'large' => 'large', |
|
| 136 | + 'medium' => null, |
|
| 137 | + 'small' => 'small', |
|
| 138 | + 'mini' => null, |
|
| 139 | + ), |
|
| 140 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 141 | + 'labelWidths' => array( |
|
| 142 | + 'small' => 3, |
|
| 143 | + ), |
|
| 144 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 145 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 146 | + // HTML markup and classes used by Foundation 5 for icons |
|
| 147 | + 'icon' => array( |
|
| 148 | + 'tag' => 'i', |
|
| 149 | + 'set' => null, |
|
| 150 | + 'prefix' => 'fi', |
|
| 151 | + ), |
|
| 152 | + // CSS for inline validation errors |
|
| 153 | + 'error_classes' => array('class' => 'error'), |
|
| 154 | + ), |
|
| 155 | + |
|
| 156 | + 'ZurbFoundation4' => array( |
|
| 157 | + // Foundation 4 also has an experimental "medium" breakpoint |
|
| 158 | + // explained at http://foundation.zurb.com/docs/components/grid.html |
|
| 159 | + 'viewports' => array( |
|
| 160 | + 'large' => 'large', |
|
| 161 | + 'medium' => null, |
|
| 162 | + 'small' => 'small', |
|
| 163 | + 'mini' => null, |
|
| 164 | + ), |
|
| 165 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 166 | + 'labelWidths' => array( |
|
| 167 | + 'small' => 3, |
|
| 168 | + ), |
|
| 169 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 170 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 171 | + // HTML markup and classes used by Foundation 4 for icons |
|
| 172 | + 'icon' => array( |
|
| 173 | + 'tag' => 'i', |
|
| 174 | + 'set' => 'general', |
|
| 175 | + 'prefix' => 'foundicon', |
|
| 176 | + ), |
|
| 177 | + // CSS for inline validation errors |
|
| 178 | + 'error_classes' => array('class' => 'alert-box radius warning'), |
|
| 179 | + ), |
|
| 180 | + |
|
| 181 | + 'ZurbFoundation' => array( // Foundation 3 |
|
| 182 | + 'viewports' => array( |
|
| 183 | + 'large' => '', |
|
| 184 | + 'medium' => null, |
|
| 185 | + 'small' => 'mobile-', |
|
| 186 | + 'mini' => null, |
|
| 187 | + ), |
|
| 188 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 189 | + 'labelWidths' => array( |
|
| 190 | + 'large' => 2, |
|
| 191 | + 'small' => 4, |
|
| 192 | + ), |
|
| 193 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 194 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 195 | + // HTML markup and classes used by Foundation 3 for icons |
|
| 196 | + 'icon' => array( |
|
| 197 | + 'tag' => 'i', |
|
| 198 | + 'set' => null, |
|
| 199 | + 'prefix' => 'fi', |
|
| 200 | + ), |
|
| 201 | + // CSS for inline validation errors |
|
| 202 | + // should work for Zurb 2 and 3 |
|
| 203 | + 'error_classes' => array('class' => 'alert-box alert error'), |
|
| 204 | + ), |
|
| 205 | 205 | |
| 206 | 206 | |
| 207 | 207 | ); |