| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Riclep\StoryblokForms; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Str; |
||||||
| 6 | use Riclep\StoryblokForms\Blocks\LsfConditionalSelect; |
||||||
| 7 | use Riclep\StoryblokForms\Traits\HasNames; |
||||||
| 8 | use Riclep\StoryblokForms\Traits\InFieldset; |
||||||
| 9 | use Riclep\StoryblokForms\Traits\ToJson; |
||||||
| 10 | |||||||
| 11 | class Input extends \Riclep\Storyblok\Block |
||||||
| 12 | { |
||||||
| 13 | use HasNames, InFieldset, ToJson; |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 14 | |||||||
| 15 | protected $key; |
||||||
| 16 | |||||||
| 17 | /** |
||||||
| 18 | * Casting validators to Validators allow us to take control of how we process the |
||||||
| 19 | * JSON from Storyblok by breaking out of the standard nested Blocks format |
||||||
| 20 | * |
||||||
| 21 | * @var string[] All the Validators for this Input |
||||||
| 22 | */ |
||||||
| 23 | protected array $_casts = ['validators' => Validators::class]; |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * Creates a key to be used for the VueJS :key on this input |
||||||
| 27 | * |
||||||
| 28 | * @param $key |
||||||
| 29 | * @return $this |
||||||
| 30 | */ |
||||||
| 31 | public function loopKey($key): Input |
||||||
| 32 | { |
||||||
| 33 | $this->key = $key; |
||||||
| 34 | |||||||
| 35 | return $this; |
||||||
| 36 | } |
||||||
| 37 | |||||||
| 38 | |||||||
| 39 | /** |
||||||
| 40 | * All the Validation rules for this Input |
||||||
| 41 | * |
||||||
| 42 | * @return mixed |
||||||
| 43 | */ |
||||||
| 44 | public function validationRules(): mixed |
||||||
| 45 | { |
||||||
| 46 | if (!$this->validators) { |
||||||
|
0 ignored issues
–
show
The property
validators does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 47 | return []; |
||||||
| 48 | } |
||||||
| 49 | |||||||
| 50 | $rules = $this->validators->validationRules(); |
||||||
| 51 | |||||||
| 52 | if ($this->parent() instanceof LsfConditionalSelect) { |
||||||
| 53 | if (is_array($this->settings('lsf_is_conditional')['when_parent_is'])) { |
||||||
|
0 ignored issues
–
show
The method
settings() does not exist on Riclep\StoryblokForms\Input.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 54 | $requiredWhen = implode(',', $this->settings('lsf_is_conditional')['when_parent_is']); |
||||||
| 55 | } else { |
||||||
| 56 | $requiredWhen = $this->settings('lsf_is_conditional')['when_parent_is']; |
||||||
| 57 | } |
||||||
| 58 | |||||||
| 59 | foreach ($rules as $key => $rule) { |
||||||
| 60 | if (in_array('required', $rule, true)) { |
||||||
| 61 | $requiredKey = array_search('required', $rule); |
||||||
| 62 | |||||||
| 63 | $rules[$key][$requiredKey] = 'required_if:' . $this->parent()->input_dot_name . '.selected,' . $requiredWhen; |
||||||
|
0 ignored issues
–
show
The property
input_dot_name does not exist on Riclep\StoryblokForms\Blocks\LsfConditionalSelect. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 64 | } |
||||||
| 65 | } |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | return $rules; |
||||||
| 69 | } |
||||||
| 70 | |||||||
| 71 | /** |
||||||
| 72 | * All the error messages for this Input |
||||||
| 73 | * |
||||||
| 74 | * @return mixed |
||||||
| 75 | */ |
||||||
| 76 | public function errorMessages(): mixed |
||||||
| 77 | { |
||||||
| 78 | $messages = $this->validators->errorMessages(); |
||||||
|
0 ignored issues
–
show
The property
validators does not exist on Riclep\StoryblokForms\Input. Since you implemented __get, consider adding a @property annotation.
Loading history...
The method
errorMessages() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 79 | |||||||
| 80 | /** |
||||||
| 81 | * Rewrite required to required_if for items inside conditional selects |
||||||
| 82 | */ |
||||||
| 83 | if ($this->parent() instanceof LsfConditionalSelect) { |
||||||
| 84 | foreach ($messages as $key => $rule) { |
||||||
| 85 | if (Str::endsWith($key, 'required')) { |
||||||
| 86 | $messages[$key . '_if'] = $rule; |
||||||
| 87 | |||||||
| 88 | unset($messages[$key]); |
||||||
| 89 | } |
||||||
| 90 | } |
||||||
| 91 | } |
||||||
| 92 | |||||||
| 93 | return $messages; |
||||||
| 94 | } |
||||||
| 95 | } |