-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathformly.php
551 lines (499 loc) · 13 KB
/
formly.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
namespace Flare;
use \Form;
use \Input;
use \Session;
use \URI;
/**
* Form generation based on Twitter Bootstrap with some added goodness.
*
* @package Bundles
* @subpackage Forms
* @author JonoB
* @version 1.0.2
*
* @see http://github.com/JonoB/flare-formly
* @see http://twitter.github.com/bootstrap/
*/
class Formly
{
/**
* The default values for the form
*/
public $defaults = array();
/**
* Default twitter form class
*
* Options are form-vertical, form-horizontal, form-inline, form-search
*/
public $form_class = 'form-horizontal';
/**
* Automatically add a csrf token to the form_open method
*/
public $auto_token = true;
/**
* Automatically create an id for each field based on the field name
*/
public $name_as_id = true;
/**
* Prefix for field id or class
*/
public $id_prefix = 'field_';
/**
* Text string to identify the required label
*/
public $required_label = '.req';
/**
* Extra text added before the label for required fields
*/
public $required_prefix = '';
/**
* Extra text added after the label for required fields
*/
public $required_suffix = ' *';
/**
* Extra class added to the label for required fields
*/
public $required_class = 'label-required';
/**
* Display a class for the control group if an input field fails validation
*/
public $control_group_error = 'error';
/**
* Display inline validation error text
*/
public $display_inline_errors = false;
/**
* Class constructor
*
* @param array $options
* @return void
*/
public function __construct($defaults = array())
{
$this->set_defaults($defaults);
}
/**
* Static function to instantiate the class
*
* @param object $defaults
* @return class
*/
public static function make($defaults = '')
{
$form = new Formly($defaults);
return $form;
}
/**
* Set the default options for the class
* @param array $defaults
*/
public function set_options($options = '')
{
if (count($options) > 0)
{
foreach ($options as $key => $value)
{
$this->$key = $value;
}
}
return $this;
}
/**
* Set form defaults
*
* This would usually be done via the static make() function
*
* @param array $defaults
*/
public function set_defaults($defaults = '')
{
if (count($defaults) > 0)
{
$this->defaults = (object)$defaults;
}
return $this;
}
/**
* Overrides the base form open method to allow for automatic insertion of csrf tokens
* and form class
*
* @param string $action Defaults to the current uri
* @param string $method Defaults to POST
* @param array $attributes
* @param bool $https
* @return string
*/
public function open($action = null, $method = 'POST', $attributes = array(), $https = null, $for_files = false)
{
// If an action has not been specified, use the current url
$action = $action ?: URI::current();
// Add in the form class if necessary
if (empty($attributes['class']))
{
$attributes['class'] = $this->form_class;
}
elseif (strpos($attributes['class'], 'form-') === false)
{
$attributes['class'] .= ' ' . $this->form_class;
}
$out = Form::open($action, $method, $attributes, $https);
if ($this->auto_token)
{
$out .= Form::token();
}
return $out;
}
public function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = null)
{
$attributes['enctype'] = 'multipart/form-data';
return $this->open($action, $method, $attributes, $https);
}
/**
* Create a HTML hidden input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function hidden($name, $value = null, $attributes = array())
{
$value = $this->calculate_value($name, $value);
return Form::input('hidden', $name, $value, $attributes);
}
/**
* Create a HTML text input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function text($name, $label = '', $value = null, $attributes = array())
{
$value = $this->calculate_value($name, $value);
$attributes = $this->set_attributes($name, $attributes);
$field = Form::text($name, $value, $attributes);
return $this->build_wrapper($field, $name, $label);
}
/**
* Create a HTML textarea input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function textarea($name, $label = '', $value = null, $attributes = array())
{
$value = $this->calculate_value($name, $value);
$attributes = $this->set_attributes($name, $attributes);
if ( ! isset($attributes['rows']))
{
$attributes['rows'] = 4;
}
$field = Form::textarea($name, $value, $attributes);
return $this->build_wrapper($field, $name, $label);
}
/**
* Create a HTML password input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function password($name, $label = '', $attributes = array())
{
$attributes = $this->set_attributes($name, $attributes);
$field = Form::password($name, $attributes);
return $this->build_wrapper($field, $name, $label);
}
/**
* Create a HTML select element.
*
* @param string $name
* @param string $label
* @param array $options
* @param string $selected
* @param array $attributes
* @return string
*/
public function select($name, $label = '', $options = array(), $selected = null, $attributes = array())
{
$selected = $this->calculate_value($name, $selected);
$attributes = $this->set_attributes($name, $attributes);
$field = Form::select($name, $options, $selected, $attributes);
return $this->build_wrapper($field, $name, $label);
}
/**
* Create a HTML checkbox input element.
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $checked
* @param array $attributes
* @return string
*/
public function checkbox($name, $label = '', $value = 1, $checked = false, $attributes = array())
{
$checked = $this->calculate_value($name, $checked);
$attributes = $this->set_attributes($name, $attributes);
$field = Form::checkbox($name, $value, $checked, $attributes);
return $this->build_wrapper($field, $name, $label, true);
}
/**
* Create a HTML file input element.
*
* @param string $name
* @param array $attributes
* @return string
*/
public function file($name, $label, $attributes = array())
{
$attributes = $this->set_attributes($name, $attributes);
$field = Form::file($name, $attributes);
return $this->build_wrapper($field, $name, $label);
}
/**
* Builds the Twitter Bootstrap control wrapper
*
* @param string $field The html for the field
* @param string $name The name of the field
* @param string $label The label name
* @param boolean $checkbox
* @return string
*/
protected function build_wrapper($field, $name, $label = '', $checkbox = false)
{
$error = (isset(Session::get('errors')->messages[$name][0])) ? Session::get('errors')->messages[$name][0] : '';
$class = 'control-group';
if ( ! empty($this->control_group_error) && ! empty($error))
{
$class .= ' ' . $this->control_group_error;
}
$id = ($this->name_as_id) ? ' id="control-group-'.$name.'"' : '';
$out = '<div class="'.$class.'"'.$id.'>';
$out .= $this->build_label($name, $label, false);
$out .= '<div class="controls">'.PHP_EOL;
$out .= ($checkbox === true) ? '<label class="checkbox">' : '';
$out .= $field;
if ($this->display_inline_errors && ! empty($error))
{
$out .= '<span class="help-inline">'.$error.'</span>';
}
$out .= ($checkbox === true) ? '</label>' : '';
$out .= '</div>';
$out .= '</div>'.PHP_EOL;
return $out;
}
/**
* Builds the label html
*
* @param string $name The name of the html field
* @param string $label The label name
* @param boolean $required
* @return string
*/
protected function build_label($name, $label = '')
{
$out = '';
$name = $this->id_prefix . $name;
if ( ! empty($label))
{
$class = 'control-label';
if ( ! empty($this->required_label) && substr($label, -strlen($this->required_label)) == $this->required_label)
{
$label = $this->required_prefix . str_replace($this->required_label, '', $label) . $this->required_suffix;
$class .= ' ' . $this->required_class;
}
$out .= Form::label($name, $label, array('class' => $class));
}
return $out;
}
/**
* Automatically populate the form field value
*
* @todo Note that there is s small error with checkboxes that are selected by default
* and then unselected by the user. If validation fails, then the checkbox will be
* selected again, because unselected checkboxes are not posted and there is no way
* to get this value after the redirect.
*
* @param string $name Html form field to populate
* @param string $value The default value for the field
* @return string
*/
protected function calculate_value($name, $value = '')
{
$result = '';
// First check if there is post data
// This assumes that you are redirecting after failed post
// and that you have flashed the data
// @see http://laravel.com/docs/input#old-input
if (Input::old($name) !== null)
{
$result = Input::old($name, $value);
}
// check if there is a default value set specifically for this field
elseif ( ! empty($value))
{
$result = $value;
}
// lastly, check if any defaults have been set for the form as a whole
elseif ( ! empty($this->defaults->$name))
{
$result = $this->defaults->$name;
}
return $result;
}
/**
* Create an id attribute for each field
* @param string $name The field name
* @param array $attributes
*/
protected function set_attributes($name, $attributes = array())
{
if ( ! $this->name_as_id or isset($attributes['id']))
{
return $attributes;
}
$attributes['id'] = $this->id_prefix . $name;
return $attributes;
}
/**
* Create a group of form actions (buttons).
*
* @param mixed $buttons String or array of HTML buttons.
* @return string
*/
public function actions($buttons)
{
$out = '<div class="form-actions">';
$out .= is_array($buttons) ? implode('', $buttons) : $buttons;
$out .= '</div>';
return $out;
}
/**
* Create a HTML submit input element.
*
* @param string $value
* @param array $attributes
* @return string
*/
public function submit($value, $attributes = array(), $btn_class = 'btn')
{
$attributes['type'] = 'submit';
if ($btn_class != 'btn')
{
$btn_class = 'btn btn-' . $btn_class;
}
if ( ! isset($attributes['class']))
{
$attributes['class'] = $btn_class;
}
elseif (strpos($attributes['class'], $btn_class) === false)
{
$attributes['class'] .= ' ' . $btn_class;
}
return Form::button($value, $attributes);
}
/**
* Shortcut method for creating a default submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_default($value, $attributes = array())
{
return $this->submit($value, $attributes);
}
/**
* Shortcut method for creating a primary submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_primary($value, $attributes = array())
{
return $this->submit($value, $attributes, 'primary');
}
/**
* Shortcut method for creating an info submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_info($value, $attributes = array())
{
return $this->submit($value, $attributes, 'info');
}
/**
* Shortcut method for creating a success submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_success($value, $attributes = array())
{
return $this->submit($value, $attributes, 'success');
}
/**
* Shortcut method for creating a warning submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_warning($value, $attributes = array())
{
return $this->submit($value, $attributes, 'warning');
}
/**
* Shortcut method for creating a danger submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_danger($value, $attributes = array())
{
return $this->submit($value, $attributes, 'danger');
}
/**
* Shortcut method for creating an inverse submit button
*
* @param string $value
* @param array $attributes
* @return [type]
*/
public function submit_inverse($value, $attributes = array())
{
return $this->submit($value, $attributes, 'inverse');
}
/**
* Create a HTML reset input element.
*
* @param string $value
* @param array $attributes
* @return string
*/
public function reset($value, $attributes = array())
{
$attributes['type'] = 'reset';
$attributes['class'] = array_get($attributes, 'class').' btn';
return Form::button($value, $attributes);
}
/**
* Create a Form close element
*/
public function close()
{
return Form::close();
}
}