v2-hub icon indicating copy to clipboard operation
v2-hub copied to clipboard

Regex as validation rule in CP

Open subpixelch opened this issue 9 years ago • 3 comments

doesn't work when pipes are used. Only if they are written direct in the yaml inside quotation marks.

subpixelch avatar Jan 31 '16 07:01 subpixelch

Did you escape the pipes?

jackmcdade avatar Feb 16 '16 18:02 jackmcdade

We need to allow for this by using an array format. The Laravel docs say this might happen and to use an array.

jasonvarga avatar Feb 23 '16 21:02 jasonvarga

This can be fixed with a simple patch:

diff --git statamic/core/CP/Fieldset.php statamic/core/CP/Fieldset.php
index f8aa593..bf9a474 100644
--- statamic/core/CP/Fieldset.php
+++ statamic/core/CP/Fieldset.php
@@ -639,7 +639,15 @@ class Fieldset implements FieldsetContract
             return array_merge($this->preProcessField($config), [
                 'display' => $this->getDisplayText($name, $config),
                 'instructions' => $this->getInstructionsText($name, $config),
-                'required' => Str::contains(array_get($config, 'validate'), 'required'),
+                'required' => tap(array_get($config, 'validate'), function (&$rules) {
+                    if (is_string($rules)) {
+                        $rules = explode('|', $rules);
+                    }
+
+                    $rules = !collect($rules)->filter(function ($rule) {
+                        return is_string($rule) ? starts_with($rule, 'required') : false;
+                    })->isEmpty();
+                }),
             ]);
         })->all();
     }

FrittenKeeZ avatar Feb 14 '20 15:02 FrittenKeeZ