Completed
Push — master ( 67e977...5a3a1c )
by Rudie
02:04
created

FormField::getValidationRules()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7.0035

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 0
dl 0
loc 36
ccs 23
cts 24
cp 0.9583
crap 7.0035
rs 8.4106
c 0
b 0
f 0
1
<?php
2
3
namespace Kris\LaravelFormBuilder\Fields;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Kris\LaravelFormBuilder\Filters\Exception\FilterAlreadyBindedException;
8
use Kris\LaravelFormBuilder\Filters\FilterInterface;
9
use Kris\LaravelFormBuilder\Filters\FilterResolver;
10
use Kris\LaravelFormBuilder\Form;
11
use Kris\LaravelFormBuilder\FormHelper;
12
use Kris\LaravelFormBuilder\Rules;
13
14
/**
15
 * Class FormField
16
 *
17
 * @package Kris\LaravelFormBuilder\Fields
18
 */
19
abstract class FormField
20
{
21
    /**
22
     * Name of the field.
23
     *
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * Type of the field.
30
     *
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * All options for the field.
37
     *
38
     * @var array
39
     */
40
    protected $options = [];
41
42
    /**
43
     * Is field rendered.
44
     *
45
     * @var bool
46
     */
47
    protected $rendered = false;
48
49
    /**
50
     * @var Form
51
     */
52
    protected $parent;
53
54
    /**
55
     * @var string
56
     */
57
    protected $template;
58
59
    /**
60
     * @var FormHelper
61
     */
62
    protected $formHelper;
63
64
    /**
65
     * Name of the property for value setting.
66
     *
67
     * @var string
68
     */
69
    protected $valueProperty = 'value';
70
71
    /**
72
     * Name of the property for default value.
73
     *
74
     * @var string
75
     */
76
    protected $defaultValueProperty = 'default_value';
77
78
    /**
79
     * Is default value set?
80
     *
81
     * @var bool|false
82
     */
83
    protected $hasDefault = false;
84
85
    /**
86
     * @var \Closure|null
87
     */
88
    protected $valueClosure = null;
89
90
    /**
91
     * Array of filters key(alias/name) => objects.
92
     *
93
     * @var array
94
     */
95
    protected $filters = [];
96
97
    /**
98
     * Raw/unfiltered field value.
99
     *
100
     * @var mixed $rawValues
101
     */
102
    protected $rawValue;
103
104
    /**
105
     * Override filters with same alias/name for field.
106
     *
107
     * @var bool
108
     */
109
    protected $filtersOverride = false;
110
111
    /**
112
     * @param string $name
113
     * @param string $type
114
     * @param Form $parent
115
     * @param array $options
116
     */
117 101
    public function __construct($name, $type, Form $parent, array $options = [])
118
    {
119 101
        $this->name = $name;
120 101
        $this->type = $type;
121 101
        $this->parent = $parent;
122 101
        $this->formHelper = $this->parent->getFormHelper();
123 101
        $this->setTemplate();
124 101
        $this->setDefaultOptions($options);
125 101
        $this->setupValue();
126 96
        $this->initFilters();
127 96
    }
128
129
130
    /**
131
     * Setup the value of the form field.
132
     *
133
     * @return void
134
     */
135 101
    protected function setupValue()
136
    {
137 101
        $value = $this->getOption($this->valueProperty);
138 101
        $isChild = $this->getOption('is_child');
139
140 101
        if ($value instanceof \Closure) {
141
            $this->valueClosure = $value;
142
        }
143
144 101
        if (($value === null || $value instanceof \Closure) && !$isChild) {
145 90
            $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name));
146 23
        } elseif (!$isChild) {
147 14
            $this->hasDefault = true;
148
        }
149 96
    }
150
151
    /**
152
     * Get the template, can be config variable or view path.
153
     *
154
     * @return string
155
     */
156
    abstract protected function getTemplate();
157
158
    /**
159
     * @return string
160
     */
161 35
    protected function getViewTemplate()
162
    {
163 35
        return $this->parent->getTemplatePrefix() . $this->getOption('template', $this->template);
164
    }
165
166
    /**
167
     * Render the field.
168
     *
169
     * @param array $options
170
     * @param bool  $showLabel
171
     * @param bool  $showField
172
     * @param bool  $showError
173
     * @return string
174
     */
175 35
    public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
176
    {
177 35
        $this->prepareOptions($options);
178 35
        $value = $this->getValue();
179 35
        $defaultValue = $this->getDefaultValue();
180
181 35
        if ($showField) {
182 35
            $this->rendered = true;
183
        }
184
185
        // Override default value with value
186 35
        if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) {
187
            $this->setOption($this->valueProperty, $defaultValue);
188
        }
189
190 35
        if (!$this->needsLabel()) {
191 11
            $showLabel = false;
192
        }
193
194 35
        if ($showError) {
195 34
            $showError = $this->parent->haveErrorsEnabled();
196
        }
197
198 35
        $data = $this->getRenderData();
199
200 35
        return $this->formHelper->getView()->make(
201 35
            $this->getViewTemplate(),
202
            $data + [
203 35
                'name' => $this->name,
204 35
                'nameKey' => $this->getNameKey(),
205 35
                'type' => $this->type,
206 35
                'options' => $this->options,
207 35
                'showLabel' => $showLabel,
208 35
                'showField' => $showField,
209 35
                'showError' => $showError,
210 35
                'errorBag'  => $this->parent->getErrorBag(),
211 35
                'translationTemplate' => $this->parent->getTranslationTemplate(),
212
            ]
213 35
        )->render();
214
    }
215
216
    /**
217
     * Return the extra render data for this form field, passed into the field's template directly.
218
     *
219
     * @return array
220
     */
221 35
    protected function getRenderData() {
222 35
        return [];
223
    }
224
225
    /**
226
     * Get the attribute value from the model by name.
227
     *
228
     * @param mixed $model
229
     * @param string $name
230
     * @return mixed
231
     */
232 92
    protected function getModelValueAttribute($model, $name)
233
    {
234 92
        $transformedName = $this->transformKey($name);
235 92
        if (is_string($model)) {
236
            return $model;
237 92
        } elseif (is_object($model)) {
238 3
            return object_get($model, $transformedName);
239 92
        } elseif (is_array($model)) {
240 91
            return Arr::get($model, $transformedName);
241
        }
242 5
    }
243
244
    /**
245
     * Transform array like syntax to dot syntax.
246
     *
247
     * @param string $key
248
     * @return mixed
249
     */
250 101
    protected function transformKey($key)
251
    {
252 101
        return $this->formHelper->transformToDotSyntax($key);
253
    }
254
255
    /**
256
     * Prepare options for rendering.
257
     *
258
     * @param array $options
259
     * @return array
260
     */
261 101
    protected function prepareOptions(array $options = [])
262
    {
263 101
        $helper = $this->formHelper;
264 101
        $rulesParser = $helper->createRulesParser($this);
265 101
        $rules = $this->getOption('rules');
266 101
        $parsedRules = $rules ? $rulesParser->parse($rules) : [];
267
268 101
        $this->options = $helper->mergeOptions($this->options, $options);
269
270 101
        foreach (['attr', 'label_attr', 'wrapper'] as $appendable) {
271
            // Append values to the 'class' attribute
272 101
            if ($this->getOption("{$appendable}.class_append")) {
273
                // Combine the current class attribute with the appends
274 3
                $append = $this->getOption("{$appendable}.class_append");
275 3
                $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append;
276 3
                $this->setOption("{$appendable}.class", $classAttribute);
277
278
                // Then remove the class_append option to prevent it from showing up as an attribute in the HTML
279 3
                $this->setOption("{$appendable}.class_append", null);
280
            }
281
        }
282
283 101
        if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) {
284 2
            $this->name = $this->name.'[]';
285 2
            $this->setOption('tmp.multipleBracesSet', true);
286
        }
287
288 101
        if ($this->parent->haveErrorsEnabled()) {
289 101
            $this->addErrorClass();
290
        }
291
292 101
        if ($this->getOption('required') === true || isset($parsedRules['required'])) {
293 4
            $lblClass = $this->getOption('label_attr.class', '');
294 4
            $requiredClass = $this->getConfig('defaults.required_class', 'required');
295
296 4
            if (! Str::contains($lblClass, $requiredClass)) {
297 4
                $lblClass .= ' '.$requiredClass;
298 4
                $this->setOption('label_attr.class', $lblClass);
299
            }
300
301 4
            if ($this->parent->clientValidationEnabled()) {
302 3
                $this->setOption('attr.required', 'required');
303
            }
304
        }
305
306 101
        if ($this->parent->clientValidationEnabled() && $parsedRules) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsedRules of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
307 1
            $attrs = $this->getOption('attr') + $parsedRules;
308 1
            $this->setOption('attr', $attrs);
309
        }
310
311 101
        $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
312 101
        $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));
313
314 101
        if ($this->getOption('help_block.text')) {
315 1
            $this->setOption(
316 1
                'help_block.helpBlockAttrs',
317 1
                $helper->prepareAttributes($this->getOption('help_block.attr'))
318
            );
319
        }
320
321 101
        return $this->options;
322
    }
323
324
    /**
325
     * Get name of the field.
326
     *
327
     * @return string
328
     */
329 38
    public function getName()
330
    {
331 38
        return $this->name;
332
    }
333
334
    /**
335
     * Set name of the field.
336
     *
337
     * @param string $name
338
     * @return $this
339
     */
340 11
    public function setName($name)
341
    {
342 11
        $this->name = $name;
343
344 11
        return $this;
345
    }
346
347
    /**
348
     * Get dot notation key for fields.
349
     *
350
     * @return string
351
     **/
352 54
    public function getNameKey()
353
    {
354 54
        return $this->transformKey($this->name);
355
    }
356
357
    /**
358
     * Get field options.
359
     *
360
     * @return array
361
     */
362 12
    public function getOptions()
363
    {
364 12
        return $this->options;
365
    }
366
367
    /**
368
     * Get single option from options array. Can be used with dot notation ('attr.class').
369
     *
370
     * @param string $option
371
     * @param mixed|null $default
372
     * @return mixed
373
     */
374 101
    public function getOption($option, $default = null)
375
    {
376 101
        return Arr::get($this->options, $option, $default);
377
    }
378
379
    /**
380
     * Set field options.
381
     *
382
     * @param array $options
383
     * @return $this
384
     */
385 11
    public function setOptions($options)
386
    {
387 11
        $this->options = $this->prepareOptions($options);
388
389 11
        return $this;
390
    }
391
392
    /**
393
     * Set single option on the field.
394
     *
395
     * @param string $name
396
     * @param mixed $value
397
     * @return $this
398
     */
399 101
    public function setOption($name, $value)
400
    {
401 101
        Arr::set($this->options, $name, $value);
402
403 101
        return $this;
404
    }
405
406
    /**
407
     * Get the type of the field.
408
     *
409
     * @return string
410
     */
411 65
    public function getType()
412
    {
413 65
        return $this->type;
414
    }
415
416
    /**
417
     * Set type of the field.
418
     *
419
     * @param mixed $type
420
     * @return $this
421
     */
422 1
    public function setType($type)
423
    {
424 1
        if ($this->formHelper->getFieldType($type)) {
425 1
            $this->type = $type;
426
        }
427
428 1
        return $this;
429
    }
430
431
    /**
432
     * @return Form
433
     */
434 101
    public function getParent()
435
    {
436 101
        return $this->parent;
437
    }
438
439
    /**
440
     * Check if the field is rendered.
441
     *
442
     * @return bool
443
     */
444 4
    public function isRendered()
445
    {
446 4
        return $this->rendered;
447
    }
448
449
    /**
450
     * Default options for field.
451
     *
452
     * @return array
453
     */
454 75
    protected function getDefaults()
455
    {
456 75
        return [];
457
    }
458
459
    /**
460
     * Defaults used across all fields.
461
     *
462
     * @return array
463
     */
464 101
    private function allDefaults()
465
    {
466
        return [
467 101
            'wrapper' => ['class' => $this->getConfig('defaults.wrapper_class')],
468 101
            'attr' => ['class' => $this->getConfig('defaults.field_class')],
469 101
            'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [
470 101
                'class' => $this->getConfig('defaults.help_block_class')
471
            ]],
472
            'value' => null,
473
            'default_value' => null,
474
            'label' => null,
475
            'label_show' => true,
476
            'is_child' => false,
477 101
            'label_attr' => ['class' => $this->getConfig('defaults.label_class')],
478 101
            'errors' => ['class' => $this->getConfig('defaults.error_class')],
479
            'rules' => [],
480
            'error_messages' => []
481
        ];
482
    }
483
484
    /**
485
     * Get real name of the field without form namespace.
486
     *
487
     * @return string
488
     */
489 100
    public function getRealName()
490
    {
491 100
        return $this->getOption('real_name', $this->name);
492
    }
493
494
    /**
495
     * @param $value
496
     * @return $this
497
     */
498 93
    public function setValue($value)
499
    {
500 93
        if ($this->hasDefault) {
501 1
            return $this;
502
        }
503
504 93
        $closure = $this->valueClosure;
505
506 93
        if ($closure instanceof \Closure) {
507
            $value = $closure($value ?: null);
508
        }
509
510 93
        if (!$this->isValidValue($value)) {
511 90
            $value = $this->getOption($this->defaultValueProperty);
512
        }
513
514 93
        $this->options[$this->valueProperty] = $value;
515
516 93
        return $this;
517
    }
518
519
    /**
520
     * Set the template property on the object.
521
     *
522
     * @return void
523
     */
524 101
    private function setTemplate()
525
    {
526 101
        $this->template = $this->getConfig($this->getTemplate(), $this->getTemplate());
527 101
    }
528
529
    /**
530
     * Add error class to wrapper if validation errors exist.
531
     *
532
     * @return void
533
     */
534 101
    protected function addErrorClass()
535
    {
536 101
        $errors = $this->parent->getRequest()->session()->get('errors');
537 101
        $errorBag = $this->parent->getErrorBag();
538
539 101
        if ($errors && $errors->hasBag($errorBag) && $errors->getBag($errorBag)->has($this->getNameKey())) {
540
            $fieldErrorClass = $this->getConfig('defaults.field_error_class');
541
            $fieldClass = $this->getOption('attr.class');
542
543
            if ($fieldErrorClass && !str_contains($fieldClass, $fieldErrorClass)) {
544
                $fieldClass .= ' ' . $fieldErrorClass;
545
                $this->setOption('attr.class', $fieldClass);
546
            }
547
548
            $wrapperErrorClass = $this->getConfig('defaults.wrapper_error_class');
549
            $wrapperClass = $this->getOption('wrapper.class');
550
551
            if ($wrapperErrorClass && $this->getOption('wrapper') && !str_contains($wrapperClass, $wrapperErrorClass)) {
552
                $wrapperClass .= ' ' . $wrapperErrorClass;
553
                $this->setOption('wrapper.class', $wrapperClass);
554
            }
555
        }
556 101
    }
557
558
    /**
559
     * Merge all defaults with field specific defaults and set template if passed.
560
     *
561
     * @param array $options
562
     */
563 101
    protected function setDefaultOptions(array $options = [])
564
    {
565 101
        $this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults());
566 101
        $this->options = $this->prepareOptions($options);
567
568 101
        $defaults = $this->setDefaultClasses($options);
569 101
        $this->options = $this->formHelper->mergeOptions($this->options, $defaults);
570
571 101
        $this->setupLabel();
572 101
    }
573
574
    /**
575
     * Creates default wrapper classes for the form element.
576
     *
577
     * @param array $options
578
     * @return array
579
     */
580 101
    protected function setDefaultClasses(array $options = [])
581
    {
582 101
        $wrapper_class = $this->getConfig('defaults.' . $this->type . '.wrapper_class', '');
583 101
        $label_class = $this->getConfig('defaults.' . $this->type . '.label_class', '');
584 101
        $field_class = $this->getConfig('defaults.' . $this->type . '.field_class', '');
585
586 101
        $defaults = [];
587 101
        if ($wrapper_class && !Arr::get($options, 'wrapper.class')) {
588
            $defaults['wrapper']['class'] = $wrapper_class;
589
        }
590 101
        if ($label_class && !Arr::get($options, 'label_attr.class')) {
591
            $defaults['label_attr']['class'] = $label_class;
592
        }
593 101
        if ($field_class && !Arr::get($options, 'attr.class')) {
594 1
            $defaults['attr']['class'] = $field_class;
595
        }
596 101
        return $defaults;
597
    }
598
599
    /**
600
     * Setup the label for the form field.
601
     *
602
     * @return void
603
     */
604 101
    protected function setupLabel()
605
    {
606 101
        if ($this->getOption('label') !== null) {
607 24
            return;
608
        }
609
610 99
        if ($template = $this->parent->getTranslationTemplate()) {
611 3
            $label = str_replace(
612 3
                ['{name}', '{type}'],
613 3
                [$this->getRealName(), 'label'],
614 3
                $template
615
            );
616 96
        } elseif ($langName = $this->parent->getLanguageName()) {
617 4
            $label = sprintf('%s.%s', $langName, $this->getRealName());
618
        } else {
619 93
            $label = $this->getRealName();
620
        }
621
622 99
        $this->setOption('label', $this->formHelper->formatLabel($label));
623 99
    }
624
625
    /**
626
     * Check if fields needs label.
627
     *
628
     * @return bool
629
     */
630 35
    protected function needsLabel()
631
    {
632
        // If field is <select> and child of choice, we don't need label for it
633 35
        $isChildSelect = $this->type == 'select' && $this->getOption('is_child') === true;
634
635 35
        if ($this->type == 'hidden' || $isChildSelect) {
636 11
            return false;
637
        }
638
639 31
        return true;
640
    }
641
642
    /**
643
     * Disable field.
644
     *
645
     * @return $this
646
     */
647 1
    public function disable()
648
    {
649 1
        $this->setOption('attr.disabled', 'disabled');
650
651 1
        return $this;
652
    }
653
654
    /**
655
     * Enable field.
656
     *
657
     * @return $this
658
     */
659 1
    public function enable()
660
    {
661 1
        Arr::forget($this->options, 'attr.disabled');
662
663 1
        return $this;
664
    }
665
666
    /**
667
     * Get validation rules for a field if any with label for attributes.
668
     *
669
     * @return array|null
670
     */
671 9
    public function getValidationRules()
672
    {
673 9
        $rules = $this->getOption('rules', []);
674 9
        $name = $this->getNameKey();
675 9
        $messages = $this->getOption('error_messages', []);
676 9
        $formName = $this->formHelper->transformToDotSyntax($this->parent->getName());
677
678 9
        if ($messages && $formName) {
679 1
            $newMessages = [];
680 1
            foreach ($messages as $messageKey => $message) {
681 1
                $messageKey = sprintf('%s.%s', $formName, $messageKey);
682 1
                $newMessages[$messageKey] = $message;
683
            }
684 1
            $messages = $newMessages;
685
        }
686
687 9
        if (!$rules) {
688 2
            return (new Rules([]))->setFieldName($this->getNameKey());
689
        }
690
691 8
        if (is_array($rules)) {
692 2
            $rules = array_map(function ($rule) use ($name) {
693 2
                if ($rule instanceof \Closure) {
694
                    return $rule($name);
695
                }
696
697 2
                return $rule;
698 2
            }, $rules);
699
        }
700
701 8
        return (new Rules(
702 8
            [$name => $rules],
703 8
            [$name => $this->getOption('label')],
704 8
            $messages
705 8
        ))->setFieldName($this->getNameKey());
706
    }
707
708
    /**
709
     * Get this field's attributes, probably just one.
710
     *
711
     * @return array
712
     */
713 3
    public function getAllAttributes()
714
    {
715 3
        return [$this->getNameKey()];
716
    }
717
718
    /**
719
     * Get value property.
720
     *
721
     * @param mixed|null $default
722
     * @return mixed
723
     */
724 38
    public function getValue($default = null)
725
    {
726 38
        return $this->getOption($this->valueProperty, $default);
727
    }
728
729
    /**
730
     * Get default value property.
731
     *
732
     * @param mixed|null $default
733
     * @return mixed
734
     */
735 35
    public function getDefaultValue($default = null)
736
    {
737 35
        return $this->getOption($this->defaultValueProperty, $default);
738
    }
739
740
    /**
741
     * Check if provided value is valid for this type.
742
     *
743
     * @return bool
744
     */
745 93
    protected function isValidValue($value)
746
    {
747 93
        return $value !== null;
748
    }
749
750
    /**
751
     * Method initFilters used to initialize filters
752
     * from field options and bind it to the same.
753
     *
754
     * @return $this
755
     */
756 96
    protected function initFilters()
757
    {
758
        // If override status is set in field options to true
759
        // we will change filtersOverride property value to true
760
        // so we can override existing filters with registered
761
        // alias/name in addFilter method.
762 96
        $overrideStatus = $this->getOption('filters_override', false);
763 96
        if ($overrideStatus) {
764 2
            $this->setFiltersOverride(true);
765
        }
766
767
        // Get filters and bind it to field.
768 96
        $filters = $this->getOption('filters', []);
769 96
        foreach ($filters as $filter) {
770 8
            $this->addFilter($filter);
771
        }
772
773 96
        return $this;
774
    }
775
776
    /**
777
     * Method setFilters used to set filters to current filters property.
778
     *
779
     * @param  array $filters
780
     *
781
     * @return \Kris\LaravelFormBuilder\Fields\FormField
782
     */
783
    public function setFilters(array $filters)
784
    {
785
        $this->clearFilters();
786
        foreach ($filters as $filter) {
787
            $this->addFilter($filter);
788
        }
789
790
        return $this;
791
    }
792
793
    /**
794
     * Method getFilters returns array of binded filters
795
     * if there are any binded. Otherwise empty array.
796
     *
797
     * @return array
798
     */
799 23
    public function getFilters()
800
    {
801 23
        return $this->filters;
802
    }
803
804
    /**
805
     * @param  string|FilterInterface $filter
806
     *
807
     * @return \Kris\LaravelFormBuilder\Fields\FormField
808
     *
809
     * @throws FilterAlreadyBindedException
810
     */
811 8
    public function addFilter($filter)
812
    {
813
        // Resolve filter object from string/object or throw Ex.
814 8
        $filterObj = FilterResolver::instance($filter);
815
816
        // If filtersOverride is allowed we will override filter
817
        // with same alias/name if there is one with new resolved filter.
818 8
        if ($this->getFiltersOverride()) {
819 1
            if ($key = array_search($filterObj->getName(), $this->getFilters())) {
820
                $this->filters[$key] = $filterObj;
821
            } else {
822 1
                $this->filters[$filterObj->getName()] = $filterObj;
823
            }
824
        } else {
825
            // If filtersOverride is disabled and we found
826
            // equal alias defined we will throw Ex.
827 7
            if (array_key_exists($filterObj->getName(), $this->getFilters())) {
828 1
                $ex = new FilterAlreadyBindedException($filterObj->getName(), $this->getName());
829 1
                throw $ex;
830
            }
831
832
            // Filter with resolvedFilter alias/name doesn't exist
833
            // so we will bind it as new one to field.
834 7
            $this->filters[$filterObj->getName()] = $filterObj;
835
        }
836
837 8
        return $this;
838
    }
839
840
    /**
841
     * Method removeFilter used to remove filter by provided alias/name.
842
     *
843
     * @param  string $name
844
     *
845
     * @return \Kris\LaravelFormBuilder\Fields\FormField
846
     */
847 1
    public function removeFilter($name)
848
    {
849 1
        $filters = $this->getFilters();
850 1
        if (array_key_exists($name, $filters)) {
851 1
            unset($filters[$name]);
852 1
            $this->filters = $filters;
853
        }
854
855 1
        return $this;
856
    }
857
858
    /**
859
     * Method removeFilters used to remove filters by provided aliases/names.
860
     *
861
     * @param  array $filterNames
862
     *
863
     * @return \Kris\LaravelFormBuilder\Fields\FormField
864
     */
865 1
    public function removeFilters(array $filterNames)
866
    {
867 1
        $filters = $this->getFilters();
868 1
        foreach ($filterNames as $filterName) {
869 1
            if (array_key_exists($filterName, $filters)) {
870 1
                unset($filters[$filterName]);
871 1
                $this->filters = $filters;
872
            }
873
        }
874
875 1
        return $this;
876
    }
877
878
    /**
879
     * Method clearFilters used to empty current filters property.
880
     *
881
     * @return \Kris\LaravelFormBuilder\Fields\FormField
882
     */
883 1
    public function clearFilters()
884
    {
885 1
        $this->filters = [];
886 1
        return $this;
887
    }
888
889
    /**
890
     * Method used to set FiltersOverride status to provided value.
891
     *
892
     * @param $status
893
     *
894
     * @return \Kris\LaravelFormBuilder\Fields\FormField
895
     */
896 2
    public function setFiltersOverride($status)
897
    {
898 2
        $this->filtersOverride = $status;
899 2
        return $this;
900
    }
901
902
    /**
903
     * @return bool
904
     */
905 9
    public function getFiltersOverride()
906
    {
907 9
        return $this->filtersOverride;
908
    }
909
910
    /**
911
     * Method used to set Unfiltered/Unmutated field value.
912
     * Method is called before field value mutating starts - request value filtering.
913
     *
914
     * @param mixed $value
915
     *
916
     * @return \Kris\LaravelFormBuilder\Fields\FormField
917
     */
918 1
    public function setRawValue($value)
919
    {
920 1
        $this->rawValue = $value;
921 1
        return $this;
922
    }
923
924
    /**
925
     * Returns unfiltered raw value of field.
926
     *
927
     * @return mixed
928
     */
929
    public function getRawValue()
930
    {
931
        return $this->rawValue;
932
    }
933
934
    /**
935
     * Get config from the form.
936
     *
937
     * @return mixed
938
     */
939 101
    private function getConfig($key = null, $default = null)
940
    {
941 101
        return $this->parent->getConfig($key, $default);
942
    }
943
}
944