Completed
Pull Request — master (#208)
by
unknown
14:08
created

Form::setLanguageName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Kris\LaravelFormBuilder;
2
3
use Illuminate\Contracts\Validation\Factory as ValidatorFactory;
4
use Illuminate\Contracts\Validation\Validator;
5
use Illuminate\Http\Request;
6
use Kris\LaravelFormBuilder\Fields\FormField;
7
8
class Form
9
{
10
11
    /**
12
     * All fields that are added
13
     *
14
     * @var array
15
     */
16
    protected $fields = [];
17
18
    /**
19
     * Model to use
20
     *
21
     * @var mixed
22
     */
23
    protected $model = [];
24
25
    /**
26
     * @var FormHelper
27
     */
28
    protected $formHelper;
29
30
    /**
31
     * Form options
32
     *
33
     * @var array
34
     */
35
    protected $formOptions = [
36
        'method' => 'GET',
37
        'url' => null
38
    ];
39
40
    /**
41
     * Additional data which can be used to build fields
42
     *
43
     * @var array
44
     */
45
    protected $data = [];
46
47
    /**
48
     * Should errors for each field be shown when called form($form) or form_rest($form) ?
49
     *
50
     * @var bool
51
     */
52
    protected $showFieldErrors = true;
53
54
    /**
55
     * Enable html5 validation
56
     *
57
     * @var bool
58
     */
59
    protected $clientValidationEnabled = true;
60
61
    /**
62
     * Name of the parent form if any
63
     *
64
     * @var string|null
65
     */
66
    protected $name = null;
67
68
    /**
69
     * @var FormBuilder
70
     */
71
    protected $formBuilder;
72
73
    /**
74
     * @var ValidatorFactory
75
     */
76
    protected $validatorFactory;
77
78
    /**
79
     * @var Validator
80
     */
81
    protected $validator = null;
82
83
    /**
84
     * @var Request
85
     */
86
    protected $request;
87
88
    /**
89
     * List of fields to not render
90
     *
91
     * @var array
92
     **/
93
    protected $exclude = [];
94
95
    /**
96
     * Are form being rebuilt?
97
     *
98
     * @var bool
99
     */
100
    protected $rebuilding = false;
101
102
    /**
103
     * @var string
104
     */
105
    protected $templatePrefix;
106
107
    /**
108
     * @var string
109
     */
110
    protected $languageName;
111
112
    /**
113
     * Build the form
114
     *
115
     * @return mixed
116
     */
117 2
    public function buildForm()
118
    {
119 2
    }
120
121
    /**
122
     * Rebuild the form from scratch
123
     *
124
     * @return $this
125
     */
126 11
    public function rebuildForm()
127
    {
128 11
        $this->rebuilding = true;
129
        // If form is plain, buildForm method is empty, so we need to take
130
        // existing fields and add them again
131 11
        if (get_class($this) === 'Kris\LaravelFormBuilder\Form') {
132 10
            foreach ($this->fields as $name => $field) {
133 10
                $this->add($name, $field->getType(), $field->getOptions());
134
            }
135
        } else {
136 3
            $this->buildForm();
137
        }
138 11
        $this->rebuilding = false;
139
140 11
        return $this;
141
    }
142
143
    /**
144
     * Create the FormField object
145
     *
146
     * @param string $name
147
     * @param string $type
148
     * @param array  $options
149
     * @return FormField
150
     */
151 36
    protected function makeField($name, $type = 'text', array $options = [])
152
    {
153 36
        $this->setupFieldOptions($name, $options);
154
155 36
        $fieldName = $this->getFieldName($name);
156
157 36
        $fieldType = $this->getFieldType($type);
158
159 35
        return new $fieldType($fieldName, $type, $this, $options);
160
    }
161
162
    /**
163
     * Create a new field and add it to the form
164
     *
165
     * @param string $name
166
     * @param string $type
167
     * @param array  $options
168
     * @param bool   $modify
169
     * @return $this
170
     */
171 37
    public function add($name, $type = 'text', array $options = [], $modify = false)
172
    {
173 37
        if (!$name || trim($name) == '') {
174 1
            throw new \InvalidArgumentException(
175 1
                'Please provide valid field name for class ['. get_class($this) .']'
176
            );
177
        }
178
179 36
        if ($this->rebuilding && !$this->has($name)) {
180
            return $this;
181
        }
182
183 36
        $this->addField($this->makeField($name, $type, $options), $modify);
184
185 32
        return $this;
186
    }
187
188
    /**
189
     * Add a FormField to the form's fields
190
     *
191
     * @param FormField $field
192
     * @return $this
193
     */
194 32
    protected function addField(FormField $field, $modify = false)
195
    {
196 32
        if (!$modify && !$this->rebuilding) {
197 32
            $this->preventDuplicate($field->getRealName());
198
        }
199
200
201 32
        if ($field->getType() == 'file') {
202 3
            $this->formOptions['files'] = true;
203
        }
204
205 32
        $this->fields[$field->getRealName()] = $field;
206
207 32
        return $this;
208
    }
209
210
    /**
211
     * Add field before another field
212
     *
213
     * @param string  $name         Name of the field before which new field is added
214
     * @param string  $fieldName    Field name which will be added
215
     * @param string  $type
216
     * @param array   $options
217
     * @param boolean $modify
218
     * @return $this
219
     */
220 1
    public function addBefore($name, $fieldName, $type = 'text', $options = [], $modify = false)
221
    {
222 1
        $offset = array_search($name, array_keys($this->fields));
223
224 1
        $beforeFields = array_slice($this->fields, 0, $offset);
225 1
        $afterFields = array_slice($this->fields, $offset);
226
227 1
        $this->fields = $beforeFields;
228
229 1
        $this->add($fieldName, $type, $options, $modify);
230
231 1
        $this->fields += $afterFields;
232
233 1
        return $this;
234
    }
235
236
    /**
237
     * Add field before another field
238
     * @param string  $name         Name of the field after which new field is added
239
     * @param string  $fieldName    Field name which will be added
240
     * @param string  $type
241
     * @param array   $options
242
     * @param boolean $modify
243
     * @return $this
244
     */
245 1
    public function addAfter($name, $fieldName, $type = 'text', $options = [], $modify = false)
246
    {
247 1
        $offset = array_search($name, array_keys($this->fields));
248
249 1
        $beforeFields = array_slice($this->fields, 0, $offset + 1);
250 1
        $afterFields = array_slice($this->fields, $offset + 1);
251
252 1
        $this->fields = $beforeFields;
253
254 1
        $this->add($fieldName, $type, $options, $modify);
255
256 1
        $this->fields += $afterFields;
257
258 1
        return $this;
259
    }
260
261
    /**
262
     * Take another form and add it's fields directly to this form
263
     * @param mixed   $class        Form to merge
264
     * @param array   $options
265
     * @param boolean $modify
266
     * @return $this
267
     */
268 1
    public function compose($class, array $options = [], $modify = false)
269
    {
270 1
        $options['class'] = $class;
271
272
        // If we pass a ready made form just extract the fields
273 1
        if ($class instanceof Form) {
274 1
            $fields = $class->getFields();
275
        } elseif ($class instanceof Fields\ChildFormType) {
276
            $fields = $class->getForm()->getFields();
277
        } elseif (is_string($class)) {
278
            // If its a string of a class make it the usual way
279
            $options['model'] = $this->model;
280
            $options['name'] = $this->name;
281
282
            $form = $this->formBuilder->create($class, $options);
283
            $fields = $form->getFields();
284
        } else {
285
            throw new \InvalidArgumentException(
286
                "[{$class}] is invalid. Please provide either a full class name, Form or ChildFormType"
287
            );
288
        }
289
290 1
        foreach ($fields as $field) {
291 1
            $this->addField($field, $modify);
292
        }
293
294 1
        return $this;
295
    }
296
297
    /**
298
     * Remove field with specified name from the form
299
     *
300
     * @param $name
301
     * @return $this
302
     */
303 2
    public function remove($name)
304
    {
305 2
        if ($this->has($name)) {
306 2
            unset($this->fields[$name]);
307
        }
308
309 2
        return $this;
310
    }
311
312
    /**
313
     * Modify existing field. If it doesn't exist, it is added to form
314
     *
315
     * @param        $name
316
     * @param string $type
317
     * @param array  $options
318
     * @param bool   $overwriteOptions
319
     * @return Form
320
     */
321 1
    public function modify($name, $type = 'text', array $options = [], $overwriteOptions = false)
322
    {
323
        // If we don't want to overwrite options, we merge them with old options
324 1
        if ($overwriteOptions === false && $this->has($name)) {
325 1
            $options = $this->formHelper->mergeOptions(
326 1
                $this->getField($name)->getOptions(),
327
                $options
328
            );
329
        }
330
331 1
        return $this->add($name, $type, $options, true);
332
    }
333
334
    /**
335
     * Render full form
336
     *
337
     * @param array $options
338
     * @param bool  $showStart
339
     * @param bool  $showFields
340
     * @param bool  $showEnd
341
     * @return string
342
     */
343 6
    public function renderForm(array $options = [], $showStart = true, $showFields = true, $showEnd = true)
344
    {
345 6
        return $this->render($options, $this->fields, $showStart, $showFields, $showEnd);
346
    }
347
348
    /**
349
     * Render rest of the form
350
     *
351
     * @param bool $showFormEnd
352
     * @param bool $showFields
353
     * @return string
354
     */
355 1
    public function renderRest($showFormEnd = true, $showFields = true)
356
    {
357 1
        $fields = $this->getUnrenderedFields();
358
359 1
        return $this->render([], $fields, false, $showFields, $showFormEnd);
360
    }
361
362
    /**
363
     * Renders the rest of the form up until the specified field name
364
     *
365
     * @param string $field_name
366
     * @param bool   $showFormEnd
367
     * @param bool   $showFields
368
     * @return string
369
     */
370 2
    public function renderUntil($field_name, $showFormEnd = true, $showFields = true)
371
    {
372 2
        if (!$this->has($field_name)) {
373 1
            $this->fieldDoesNotExist($field_name);
374
        }
375
376 1
        $fields = $this->getUnrenderedFields();
377
378 1
        $i = 1;
379 1
        foreach ($fields as $key => $value) {
380 1
            if ($value->getRealName() == $field_name) {
381 1
                break;
382
            }
383 1
            $i++;
384
        }
385
386 1
        $fields = array_slice($fields, 0, $i, true);
387
388 1
        return $this->render([], $fields, false, $showFields, $showFormEnd);
389
    }
390
391
    /**
392
     * Get single field instance from form object
393
     *
394
     * @param $name
395
     * @return FormField
396
     */
397 21
    public function getField($name)
398
    {
399 21
        if ($this->has($name)) {
400 20
            return $this->fields[$name];
401
        }
402
403 1
        $this->fieldDoesNotExist($name);
404
    }
405
406
    /**
407
     * Check if form has field
408
     *
409
     * @param $name
410
     * @return bool
411
     */
412 32
    public function has($name)
413
    {
414 32
        return array_key_exists($name, $this->fields);
415
    }
416
417
    /**
418
     * Get all form options
419
     *
420
     * @return array
421
     */
422 2
    public function getFormOptions()
423
    {
424 2
        return $this->formOptions;
425
    }
426
427
    /**
428
     * Get single form option
429
     *
430
     * @param string $option
431
     * @param $default
432
     * @return mixed
433
     */
434 82
    public function getFormOption($option, $default = null)
435
    {
436 82
        return array_get($this->formOptions, $option, $default);
437
    }
438
439
    /**
440
     * Set single form option on form
441
     *
442
     * @param string $option
443
     * @param mixed $value
444
     *
445
     * @return $this
446
     */
447 2
    public function setFormOption($option, $value)
448
    {
449 2
        $this->formOptions[$option] = $value;
450
451 2
        return $this;
452
    }
453
454
    /**
455
     * Set form options
456
     *
457
     * @param array $formOptions
458
     * @return $this
459
     */
460 82
    public function setFormOptions($formOptions)
461
    {
462 82
        $this->formOptions = $this->formHelper->mergeOptions($this->formOptions, $formOptions);
463 82
        $this->checkIfNamedForm();
464 82
        $this->pullFromOptions('data', 'addData');
465 82
        $this->pullFromOptions('model', 'setupModel');
466 82
        $this->pullFromOptions('errors_enabled', 'setErrorsEnabled');
467 82
        $this->pullFromOptions('client_validation', 'setClientValidationEnabled');
468 82
        $this->pullFromOptions('template_prefix', 'setTemplatePrefix');
469 82
        $this->pullFromOptions('language_name', 'setLanguageName');
470
471 82
        return $this;
472
    }
473
474
    /**
475
     * Get an option from provided options and call method with that value
476
     *
477
     * @param $name
478
     * @param $method
479
     */
480 82
    protected function pullFromOptions($name, $method)
481
    {
482 82
        if (array_get($this->formOptions, $name) !== null) {
483 13
            $this->{$method}(array_pull($this->formOptions, $name));
484
        }
485 82
    }
486
487
    /**
488
     * Get form http method
489
     *
490
     * @return string
491
     */
492 3
    public function getMethod()
493
    {
494 3
        return $this->formOptions['method'];
495
    }
496
497
    /**
498
     * Set form http method
499
     *
500
     * @param string $method
501
     * @return $this
502
     */
503 1
    public function setMethod($method)
504
    {
505 1
        $this->formOptions['method'] = $method;
506
507 1
        return $this;
508
    }
509
510
    /**
511
     * Get form action url
512
     *
513
     * @return string
514
     */
515 3
    public function getUrl()
516
    {
517 3
        return $this->formOptions['url'];
518
    }
519
520
    /**
521
     * Set form action url
522
     *
523
     * @param string $url
524
     * @return $this
525
     */
526 1
    public function setUrl($url)
527
    {
528 1
        $this->formOptions['url'] = $url;
529
530 1
        return $this;
531
    }
532
533
    /**
534
     * @return string|null
535
     */
536 41
    public function getName()
537
    {
538 41
        return $this->name;
539
    }
540
541
    /**
542
     * @param string $name
543
     * @param bool $rebuild
544
     *
545
     * @return $this
546
     */
547 5
    public function setName($name, $rebuild = true)
548
    {
549 5
        $this->name = $name;
550
551 5
        if ($rebuild) {
552 5
            $this->rebuildForm();
553
        }
554
555 5
        return $this;
556
    }
557
558
    /**
559
     * Get model that is bind to form object
560
     *
561
     * @return mixed
562
     */
563 58
    public function getModel()
564
    {
565 58
        return $this->model;
566
    }
567
568
    /**
569
     * Set model to form object
570
     *
571
     * @param mixed $model
572
     * @return $this
573
     * @deprecated deprecated since 1.6.31, will be removed in 1.7 - pass model as option when creating a form
574
     */
575
    public function setModel($model)
576
    {
577
        $this->model = $model;
578
579
        $this->setupNamedModel();
580
581
        $this->rebuildForm();
582
583
        return $this;
584
    }
585
586
    /**
587
     * Setup model for form, add namespace if needed for child forms
588
     * @return $this
589
     */
590 9
    protected function setupModel($model)
591
    {
592 9
        $this->model = $model;
593
594 9
        $this->setupNamedModel();
595
596 9
        return $this;
597
    }
598
599
    /**
600
     * Get all fields
601
     *
602
     * @return FormField[]
603
     */
604 15
    public function getFields()
605
    {
606 15
        return $this->fields;
607
    }
608
609
    /**
610
     * Get field dynamically
611
     *
612
     * @param $name
613
     * @return FormField
614
     */
615 16
    public function __get($name)
616
    {
617 16
        if ($this->has($name)) {
618 15
            return $this->getField($name);
619
        }
620 3
    }
621
622
    /**
623
     * Set the form helper only on first instantiation
624
     *
625
     * @param FormHelper $formHelper
626
     * @return $this
627
     */
628 82
    public function setFormHelper(FormHelper $formHelper)
629
    {
630 82
        $this->formHelper = $formHelper;
631
632 82
        return $this;
633
    }
634
635
    /**
636
     * Get form helper
637
     *
638
     * @return FormHelper
639
     */
640 62
    public function getFormHelper()
641
    {
642 62
        return $this->formHelper;
643
    }
644
645
    /**
646
     * Add custom field
647
     *
648
     * @param $name
649
     * @param $class
650
     */
651 2
    public function addCustomField($name, $class)
652
    {
653 2
        $this->formHelper->addCustomField($name, $class);
654 2
    }
655
656
    /**
657
     * Should form errors be shown under every field ?
658
     *
659
     * @return bool
660
     */
661 62
    public function haveErrorsEnabled()
662
    {
663 62
        return $this->showFieldErrors;
664
    }
665
666
    /**
667
     * Enable or disable showing errors under fields
668
     *
669
     * @param boolean $enabled
670
     * @return $this
671
     */
672 1
    public function setErrorsEnabled($enabled)
673
    {
674 1
        $this->showFieldErrors = (boolean) $enabled;
675
676 1
        return $this;
677
    }
678
679
    /**
680
     * Is client validation enabled?
681
     *
682
     * @return boolean
683
     */
684 62
    public function clientValidationEnabled()
685
    {
686 62
        return $this->clientValidationEnabled;
687
    }
688
689
    /**
690
     * Enable/disable client validation
691
     *
692
     * @param boolean $enable
693
     * @return $this
694
     */
695 1
    public function setClientValidationEnabled($enable)
696
    {
697 1
        $this->clientValidationEnabled = (boolean) $enable;
698
699 1
        return $this;
700
    }
701
702
    /**
703
     * Add any aditional data that field needs (ex. array of choices)
704
     *
705
     * @deprecated deprecated since 1.6.20, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data
706
     * will be switched to protected in 1.7
707
     * @param string $name
708
     * @param mixed $data
709
     */
710
    public function setData($name, $data)
711
    {
712
        $this->data[$name] = $data;
713
    }
714
715
    /**
716
     * Get single additional data
717
     *
718
     * @param string $name
719
     * @param null   $default
720
     * @return mixed
721
     */
722 10
    public function getData($name = null, $default = null)
723
    {
724 10
        if (is_null($name)) {
725 9
            return $this->data;
726
        }
727
728 1
        return array_get($this->data, $name, $default);
729
    }
730
731
    /**
732
     * Add multiple peices of data at once
733
     *
734
     * @deprecated deprecated since 1.6.12, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data
735
     * will be switched to protected in 1.7
736
     * @param $data
737
     * @return $this
738
     **/
739
    public function addData(array $data)
740
    {
741
        foreach ($data as $key => $value) {
742
            $this->setData($key, $value);
0 ignored issues
show
Deprecated Code introduced by
The method Kris\LaravelFormBuilder\Form::setData() has been deprecated with message: deprecated since 1.6.20, will be removed in 1.7 - use 3rd param on create, or 2nd on plain method to pass data
will be switched to protected in 1.7

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
743
        }
744
745
        return $this;
746
    }
747
748
    /**
749
     * Get current request
750
     *
751
     * @return \Illuminate\Http\Request
752
     */
753 62
    public function getRequest()
754
    {
755 62
        return $this->request;
756
    }
757
758
    /**
759
     * Set request on form
760
     *
761
     * @param Request $request
762
     * @return $this
763
     */
764 82
    public function setRequest(Request $request)
765
    {
766 82
        $this->request = $request;
767
768 82
        return $this;
769
    }
770
771
    /**
772
     * Get template prefix that is prepended to all template paths
773
     *
774
     * @return string
775
     */
776 28
    public function getTemplatePrefix()
777
    {
778 28
        if ($this->templatePrefix !== null) {
779 4
            return $this->templatePrefix;
780
        }
781
782 24
        return $this->formHelper->getConfig('template_prefix');
783
    }
784
785
    /**
786
     * Set a template prefix for the form and its fields
787
     *
788
     * @param string $prefix
789
     * @return $this
790
     */
791 4
    public function setTemplatePrefix($prefix)
792
    {
793 4
        $this->templatePrefix = (string) $prefix;
794
795 4
        return $this;
796
    }
797
798
    /**
799
     * Get the 'lookup' key for a localizable label name
800
     *
801
     * @param string $label
802
     * @return $this
803
     */
804 63
    public function getLocalizableName($label)
805
    {
806 63
        return ($this->languageName ? $this->languageName . '.' : '') . $label;
807
    }
808
809
    /**
810
     * Set a language name, used as prefix for translated strings
811
     *
812
     * @param string $prefix
813
     * @return $this
814
     */
815 1
    public function setLanguageName($prefix)
816
    {
817 1
        $this->languageName = (string) $prefix;
818
819 1
        return $this;
820
    }
821
822
    /**
823
     * Render the form
824
     *
825
     * @param $options
826
     * @param $fields
827
     * @param boolean $showStart
828
     * @param boolean $showFields
829
     * @param boolean $showEnd
830
     * @return string
831
     */
832 8
    protected function render($options, $fields, $showStart, $showFields, $showEnd)
833
    {
834 8
        $formOptions = $this->formHelper->mergeOptions($this->formOptions, $options);
835
836 8
        $this->setupNamedModel();
837
838 8
        return $this->formHelper->getView()
839 8
            ->make($this->getTemplate())
840 8
            ->with(compact('showStart', 'showFields', 'showEnd'))
841 8
            ->with('formOptions', $formOptions)
842 8
            ->with('fields', $fields)
843 8
            ->with('model', $this->getModel())
844 8
            ->with('exclude', $this->exclude)
845 8
            ->with('form', $this)
846 8
            ->render();
847
    }
848
849
    /**
850
     * Get template from options if provided, otherwise fallback to config
851
     *
852
     * @return mixed
853
     */
854 8
    protected function getTemplate()
855
    {
856 8
        return $this->getTemplatePrefix() . $this->getFormOption('template', $this->formHelper->getConfig('form'));
857
    }
858
859
    /**
860
     * Get all fields that are not rendered
861
     *
862
     * @return array
863
     */
864 2
    protected function getUnrenderedFields()
865
    {
866 2
        $unrenderedFields = [];
867
868 2
        foreach ($this->fields as $field) {
869 2
            if (!$field->isRendered()) {
870 2
                $unrenderedFields[] = $field;
871 2
                continue;
872
            }
873
        }
874
875 2
        return $unrenderedFields;
876
    }
877
878
    /**
879
     * Prevent adding fields with same name
880
     *
881
     * @param string $name
882
     */
883 32
    protected function preventDuplicate($name)
884
    {
885 32
        if ($this->has($name)) {
886 1
            throw new \InvalidArgumentException('Field ['.$name.'] already exists in the form '.get_class($this));
887
        }
888 32
    }
889
890
    /**
891
     * @param string $type
892
     * @return string
893
     */
894 36
    protected function getFieldType($type)
895
    {
896 36
        $fieldType = $this->formHelper->getFieldType($type);
897
898 35
        return $fieldType;
899
    }
900
901
    /**
902
     * Check if form is named form
903
     */
904 82
    protected function checkIfNamedForm()
905
    {
906 82
        if ($this->getFormOption('name')) {
907 5
            $this->name = array_pull($this->formOptions, 'name', $this->name);
908
        }
909 82
    }
910
911
    /**
912
     * Set up options on single field depending on form options
913
     *
914
     * @param string $name
915
     * @param $options
916
     */
917 36
    protected function setupFieldOptions($name, &$options)
918
    {
919 36
        $options['real_name'] = $name;
920
921 36
        if (!$this->getName()) {
922 36
            return;
923
        }
924
925 9
        if (!isset($options['label'])) {
926 7
            $options['label'] = $this->formHelper->formatLabel($this->getLocalizableName($name));
927
        }
928 9
    }
929
930
    /**
931
     * Set namespace to model if form is named so the data is bound properly
932
     * Returns true if model is changed, otherwise false
933
     *
934
     * @return bool
935
     */
936 23
    protected function setupNamedModel()
937
    {
938 23
        if (!$this->getModel() || !$this->getName()) {
939 22
            return false;
940
        }
941
942 4
        $dotName = $this->formHelper->transformToDotSyntax($this->getName());
943 4
        $model = $this->formHelper->convertModelToArray($this->getModel());
944
945 4
        if (!array_get($model, $dotName)) {
946 4
            $newModel = [];
947 4
            array_set($newModel, $dotName, $model);
948 4
            $this->model = $newModel;
949
950 4
            return true;
951
        }
952
953 3
        return false;
954
    }
955
956
957
    /**
958
     * Set form builder instance on helper so we can use it later
959
     *
960
     * @param FormBuilder $formBuilder
961
     * @return $this
962
     */
963 82
    public function setFormBuilder(FormBuilder $formBuilder)
964
    {
965 82
        $this->formBuilder = $formBuilder;
966
967 82
        return $this;
968
    }
969
970
    /**
971
     * @return FormBuilder
972
     */
973 9
    public function getFormBuilder()
974
    {
975 9
        return $this->formBuilder;
976
    }
977
978
    /**
979
     * @param ValidatorFactory $validator
980
     * @return $this
981
     */
982 82
    public function setValidator(ValidatorFactory $validator)
983
    {
984 82
        $this->validatorFactory = $validator;
985
986 82
        return $this;
987
    }
988
989
    /**
990
     * Exclude some fields from rendering
991
     *
992
     * @return $this
993
     */
994
    public function exclude(array $fields)
995
    {
996
        $this->exclude = array_merge($this->exclude, $fields);
997
998
        return $this;
999
    }
1000
1001
1002
    /**
1003
     * If form is named form, modify names to be contained in single key (parent[child_field_name])
1004
     *
1005
     * @param string $name
1006
     * @return string
1007
     */
1008 36
    protected function getFieldName($name)
1009
    {
1010 36
        if ($this->getName() !== null) {
1011 9
            return $this->getName().'['.$name.']';
1012
        }
1013
1014 36
        return $name;
1015
    }
1016
1017
    /**
1018
     * Disable all fields in a form
1019
     */
1020 1
    public function disableFields()
1021
    {
1022 1
        foreach ($this->fields as $field) {
1023 1
            $field->disable();
1024
        }
1025 1
    }
1026
1027
    /**
1028
     * Enable all fields in a form
1029
     */
1030 1
    public function enableFields()
1031
    {
1032 1
        foreach ($this->fields as $field) {
1033 1
            $field->enable();
1034
        }
1035 1
    }
1036
1037
    /**
1038
     * Validate the form
1039
     *
1040
     * @param array $validationRules
1041
     * @param array $messages
1042
     * @return Validator
1043
     */
1044 3
    public function validate($validationRules = [], $messages = [])
1045
    {
1046 3
        $fieldRules = $this->formHelper->mergeFieldsRules($this->fields);
1047 3
        $rules = array_merge($fieldRules['rules'], $validationRules);
1048
1049 3
        $this->validator = $this->validatorFactory->make($this->getRequest()->all(), $rules, $messages);
1050 3
        $this->validator->setAttributeNames($fieldRules['attributes']);
1051
1052 3
        return $this->validator;
1053
    }
1054
1055
    /**
1056
     * Get validatdion rules for the form
1057
     *
1058
     * @param array $overrideRules
1059
     * @return array
1060
     */
1061 1
    public function getRules($overrideRules = [])
1062
    {
1063 1
        $fieldRules = $this->formHelper->mergeFieldsRules($this->fields);
1064
1065 1
        return array_merge($fieldRules['rules'], $overrideRules);
1066
    }
1067
1068
    /**
1069
     * Check if the form is valid
1070
     *
1071
     * @return bool
1072
     */
1073 3
    public function isValid()
1074
    {
1075 3
        if (!$this->validator) {
1076 2
            $this->validate();
1077
        }
1078
1079 3
        return !$this->validator->fails();
1080
    }
1081
1082
    /**
1083
     * Get validation errors
1084
     *
1085
     * @return array
1086
     */
1087 3
    public function getErrors()
1088
    {
1089 3
        if (!$this->validator || !$this->validator instanceof Validator) {
1090 1
            throw new \InvalidArgumentException(
1091
                sprintf(
1092 1
                    'Form %s was not validated. To validate it, call "isValid" method before retrieving the errors',
1093
                    get_class($this)
1094
                )
1095
            );
1096
        }
1097
1098 2
        return $this->validator->getMessageBag()->getMessages();
1099
    }
1100
1101
    /**
1102
     * Throw an exception indicating a field does not exist on the class
1103
     *
1104
     * @param string $name
1105
     * @throws \InvalidArgumentException
1106
     */
1107 2
    protected function fieldDoesNotExist($name)
1108
    {
1109 2
        throw new \InvalidArgumentException('Field ['.$name.'] does not exist in '.get_class($this));
1110
    }
1111
}
1112