Skip to content

Commit d57a465

Browse files
committed
Updated validation test to properly split rules. Fixes #1201
1 parent 4acbd31 commit d57a465

2 files changed

Lines changed: 75 additions & 53 deletions

File tree

system/Validation/Validation.php

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2828
* THE SOFTWARE.
2929
*
30-
* @package CodeIgniter
31-
* @author CodeIgniter Dev Team
32-
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
33-
* @license https://opensource.org/licenses/MIT MIT License
34-
* @link https://codeigniter.com
35-
* @since Version 3.0.0
30+
* @package CodeIgniter
31+
* @author CodeIgniter Dev Team
32+
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
33+
* @license https://opensource.org/licenses/MIT MIT License
34+
* @link https://codeigniter.com
35+
* @since Version 3.0.0
3636
* @filesource
3737
*/
38+
3839
use CodeIgniter\HTTP\RequestInterface;
3940
use CodeIgniter\Validation\Exceptions\ValidationException;
4041
use CodeIgniter\View\RendererInterface;
@@ -100,7 +101,7 @@ class Validation implements ValidationInterface
100101
* Validation constructor.
101102
*
102103
* @param \Config\Validation $config
103-
* @param RendererInterface $view
104+
* @param RendererInterface $view
104105
*/
105106
public function __construct($config, RendererInterface $view)
106107
{
@@ -176,8 +177,9 @@ public function check($value, string $rule, array $errors = []): bool
176177
{
177178
$this->reset();
178179
$this->setRule('check', null, $rule, $errors);
180+
179181
return $this->run([
180-
'check' => $value
182+
'check' => $value,
181183
]);
182184
}
183185

@@ -204,7 +206,7 @@ protected function processRules(string $field, string $label = null, $value, $ru
204206
{
205207
// and the current field does not exists in the input data
206208
// we can return true. Ignoring all other rules to this field.
207-
if ( ! array_key_exists($field, $data))
209+
if (! array_key_exists($field, $data))
208210
{
209211
return true;
210212
}
@@ -214,7 +216,7 @@ protected function processRules(string $field, string $label = null, $value, $ru
214216

215217
if (in_array('permit_empty', $rules))
216218
{
217-
if ( ! in_array('required', $rules) && (is_array($value) ? empty($value) : (trim($value) === '')))
219+
if (! in_array('required', $rules) && (is_array($value) ? empty($value) : (trim($value) === '')))
218220
{
219221
return true;
220222
}
@@ -225,13 +227,13 @@ protected function processRules(string $field, string $label = null, $value, $ru
225227
foreach ($rules as $rule)
226228
{
227229
$callable = is_callable($rule);
228-
$passed = false;
230+
$passed = false;
229231

230232
// Rules can contain parameters: max_length[5]
231233
$param = false;
232-
if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
234+
if (! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))
233235
{
234-
$rule = $match[1];
236+
$rule = $match[1];
235237
$param = $match[2];
236238
}
237239

@@ -250,20 +252,21 @@ protected function processRules(string $field, string $label = null, $value, $ru
250252
// Check in our rulesets
251253
foreach ($this->ruleSetInstances as $set)
252254
{
253-
if ( ! method_exists($set, $rule))
255+
if (! method_exists($set, $rule))
254256
{
255257
continue;
256258
}
257259

258260
$found = true;
259261

260-
$passed = $param === false ? $set->$rule($value, $error) : $set->$rule($value, $param, $data, $error);
262+
$passed = $param === false ? $set->$rule($value, $error)
263+
: $set->$rule($value, $param, $data, $error);
261264
break;
262265
}
263266

264267
// If the rule wasn't found anywhere, we
265268
// should throw an exception so the developer can find it.
266-
if ( ! $found)
269+
if (! $found)
267270
{
268271
throw ValidationException::forRuleNotFound($rule);
269272
}
@@ -272,7 +275,8 @@ protected function processRules(string $field, string $label = null, $value, $ru
272275
// Set the error message if we didn't survive.
273276
if ($passed === false)
274277
{
275-
$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param) : $error;
278+
$this->errors[$field] = is_null($error) ? $this->getErrorMessage($rule, $field, $label, $param)
279+
: $error;
276280

277281
return false;
278282
}
@@ -332,11 +336,11 @@ public function withRequest(RequestInterface $request): ValidationInterface
332336
public function setRule(string $field, string $label = null, string $rules, array $errors = [])
333337
{
334338
$this->rules[$field] = [
335-
'label' => $label,
336-
'rules' => $rules,
339+
'label' => $label,
340+
'rules' => $rules,
337341
];
338-
$this->customErrors = array_merge($this->customErrors, [
339-
$field => $errors
342+
$this->customErrors = array_merge($this->customErrors, [
343+
$field => $errors,
340344
]);
341345

342346
return $this;
@@ -426,12 +430,12 @@ public function hasRule(string $field): bool
426430
*/
427431
public function getRuleGroup(string $group): array
428432
{
429-
if ( ! isset($this->config->$group))
433+
if (! isset($this->config->$group))
430434
{
431435
throw ValidationException::forGroupNotFound($group);
432436
}
433437

434-
if ( ! is_array($this->config->$group))
438+
if (! is_array($this->config->$group))
435439
{
436440
throw ValidationException::forGroupNotArray($group);
437441
}
@@ -451,10 +455,10 @@ public function getRuleGroup(string $group): array
451455
*/
452456
public function setRuleGroup(string $group)
453457
{
454-
$rules = $this->getRuleGroup($group);
458+
$rules = $this->getRuleGroup($group);
455459
$this->rules = $rules;
456460

457-
$errorName = $group . '_errors';
461+
$errorName = $group.'_errors';
458462
if (isset($this->config->$errorName))
459463
{
460464
$this->customErrors = $this->config->$errorName;
@@ -470,13 +474,13 @@ public function setRuleGroup(string $group)
470474
*/
471475
public function listErrors(string $template = 'list'): string
472476
{
473-
if ( ! array_key_exists($template, $this->config->templates))
477+
if (! array_key_exists($template, $this->config->templates))
474478
{
475479
throw ValidationException::forInvalidTemplate($template);
476480
}
477481

478482
return $this->view->setVar('errors', $this->getErrors())
479-
->render($this->config->templates[$template]);
483+
->render($this->config->templates[$template]);
480484
}
481485

482486
//--------------------------------------------------------------------
@@ -491,18 +495,18 @@ public function listErrors(string $template = 'list'): string
491495
*/
492496
public function showError(string $field, string $template = 'single'): string
493497
{
494-
if ( ! array_key_exists($field, $this->errors))
498+
if (! array_key_exists($field, $this->errors))
495499
{
496500
return '';
497501
}
498502

499-
if ( ! array_key_exists($template, $this->config->templates))
503+
if (! array_key_exists($template, $this->config->templates))
500504
{
501505
throw ValidationException::forInvalidTemplate($template);
502506
}
503507

504508
return $this->view->setVar('error', $this->getError($field))
505-
->render($this->config->templates[$template]);
509+
->render($this->config->templates[$template]);
506510
}
507511

508512
//--------------------------------------------------------------------
@@ -543,12 +547,12 @@ protected function loadRuleGroup(string $group = null)
543547
return;
544548
}
545549

546-
if ( ! isset($this->config->$group))
550+
if (! isset($this->config->$group))
547551
{
548552
throw ValidationException::forGroupNotFound($group);
549553
}
550554

551-
if ( ! is_array($this->config->$group))
555+
if (! is_array($this->config->$group))
552556
{
553557
throw ValidationException::forGroupNotArray($group);
554558
}
@@ -557,7 +561,7 @@ protected function loadRuleGroup(string $group = null)
557561

558562
// If {group}_errors exists in the config file,
559563
// then override our custom errors with them.
560-
$errorName = $group . '_errors';
564+
$errorName = $group.'_errors';
561565

562566
if (isset($this->config->$errorName))
563567
{
@@ -615,11 +619,11 @@ public function getError(string $field = null): string
615619
* ]
616620
*
617621
* @return array
618-
*
622+
*
619623
* Excluded from code coverage because that it always run as cli
620-
*
624+
*
621625
* @codeCoverageIgnore
622-
*
626+
*
623627
*/
624628
public function getErrors(): array
625629
{
@@ -629,7 +633,7 @@ public function getErrors(): array
629633
if (empty($this->errors) && ! is_cli())
630634
{
631635
// Start up the session if it's not already
632-
if ( ! isset($_SESSION))
636+
if (! isset($_SESSION))
633637
{
634638
session();
635639
}
@@ -684,7 +688,7 @@ protected function getErrorMessage(string $rule, string $field, string $label =
684688
// Try to grab a localized version of the message...
685689
// lang() will return the rule name back if not found,
686690
// so there will always be a string being returned.
687-
$message = lang('Validation.' . $rule);
691+
$message = lang('Validation.'.$rule);
688692
}
689693

690694
$message = str_replace('{field}', $label ?? $field, $message);
@@ -703,35 +707,41 @@ protected function getErrorMessage(string $rule, string $field, string $label =
703707
*/
704708
protected function splitRules(string $rules): array
705709
{
706-
$_rules = [];
710+
$_rules = [];
707711
$pipe_pos = strpos($rules, '|');
708-
while ($pipe_pos !== false) {
709-
712+
while ($pipe_pos !== false)
713+
{
710714
// the if is for the regex_match
711715
// if the pattern contains | (pipe) the split was incorrect
712716
// so we make sure that, if the pipe is in a pattern we find the separator and
713717
// grab the string up to the separator + closing bracket
714718
$open_bracket_pos = strpos($rules, '[');
715-
if ($open_bracket_pos !== false && $open_bracket_pos < $pipe_pos) {
716-
$separator = $rules[$open_bracket_pos + 1];
719+
if ($open_bracket_pos !== false && $open_bracket_pos < $pipe_pos)
720+
{
721+
$separator = $rules[$open_bracket_pos+1];
722+
723+
$regex_end_pos = strpos($rules, $separator.']');
717724

718-
$regex_end_pos = strpos($rules, $separator . ']');
719-
$_rules[] = substr($rules, 0, $regex_end_pos + 2);
725+
if ($regex_end_pos !== false)
726+
{
727+
$_rules[] = substr($rules, 0, $regex_end_pos+2);
720728

721-
$rules = substr($rules, $regex_end_pos + 3);
729+
$rules = substr($rules, $regex_end_pos+3);
722730

723-
$pipe_pos = strpos($rules, '|');
724-
continue;
731+
$pipe_pos = strpos($rules, '|');
732+
continue;
733+
}
725734
}
726735

727736
$_rules[] = substr($rules, 0, $pipe_pos);
728-
$rules = substr($rules, $pipe_pos + 1);
737+
$rules = substr($rules, $pipe_pos+1);
729738

730739
$pipe_pos = strpos($rules, '|');
731740
}
732741

733742
// if there is another rule remaining but no separator just add it to the list
734-
if (!empty($rules)) {
743+
if (! empty($rules))
744+
{
735745
$_rules[] = $rules;
736746
}
737747

@@ -751,9 +761,9 @@ protected function splitRules(string $rules): array
751761
*/
752762
public function reset(): ValidationInterface
753763
{
754-
$this->data = [];
755-
$this->rules = [];
756-
$this->errors = [];
764+
$this->data = [];
765+
$this->rules = [];
766+
$this->errors = [];
757767
$this->customErrors = [];
758768

759769
return $this;

tests/system/Validation/ValidationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,16 @@ public function testSplitRulesFalse()
533533

534534
$this->assertFalse($result);
535535
}
536+
537+
/**
538+
* @see https://github.com/bcit-ci/CodeIgniter4/issues/1201
539+
*/
540+
public function testSplitNotRegex()
541+
{
542+
$method = $this->getPrivateMethodInvoker($this->validation, 'splitRules');
543+
544+
$result = $method('uploaded[avatar]|max_size[avatar,1024]');
545+
546+
$this->assertEquals('uploaded[avatar]', $result[0]);
547+
}
536548
}

0 commit comments

Comments
 (0)