Skip to content

Commit f091a77

Browse files
committed
New generic string validation rule.
1 parent eb31af2 commit f091a77

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

system/Validation/FormatRules.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ public function alpha_numeric_space(string $str = null): bool
121121

122122
//--------------------------------------------------------------------
123123

124+
/**
125+
* Any type of string
126+
*
127+
* Note: we specifically do NOT type hint $str here so that
128+
* it doesn't convert numbers into strings.
129+
*
130+
* @param string|null $str
131+
*
132+
* @return boolean
133+
*/
134+
public function string($str = null): bool
135+
{
136+
return is_string($str);
137+
}
138+
139+
//--------------------------------------------------------------------
140+
124141
/**
125142
* Decimal number
126143
*

tests/system/Validation/FormatRulesTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,47 @@ public function ipProvider()
325325

326326
//--------------------------------------------------------------------
327327

328+
/**
329+
* @dataProvider stringProvider
330+
*
331+
* @param $str
332+
* @param $expected
333+
*/
334+
public function testString($str, $expected)
335+
{
336+
$data = [
337+
'foo' => $str,
338+
];
339+
340+
$this->validation->setRules([
341+
'foo' => 'string',
342+
]);
343+
344+
$this->assertEquals($expected, $this->validation->run($data));
345+
}
346+
347+
//--------------------------------------------------------------------
348+
349+
public function stringProvider()
350+
{
351+
return [
352+
[
353+
'123',
354+
true,
355+
],
356+
[
357+
123,
358+
false,
359+
],
360+
[
361+
'hello',
362+
true,
363+
],
364+
];
365+
}
366+
367+
//--------------------------------------------------------------------
368+
328369
/**
329370
* @dataProvider alphaProvider
330371
*

user_guide_src/source/libraries/validation.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,28 +661,29 @@ alpha_numeric No Fails if field contains anything other than
661661
alpha_numeric_space No Fails if field contains anything other than alpha-numeric characters, numbers or space.
662662
decimal No Fails if field contains anything other than a decimal number.
663663
differs Yes Fails if field does not differ from the one in the parameter. differs[field_name]
664-
exact_length Yes Fails if field is not exactly the parameter value. One or more comma-separated values. exact_length[5] or exact_length[5,8,12]
664+
exact_length Yes Fails if field is not exactly the parameter value. One or more comma-separated values. exact_length[5] or exact_length[5,8,12]
665665
greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]
666666
greater_than_equal_to Yes Fails if field is less than the parameter value, or not numeric. greater_than_equal_to[5]
667+
if_exist No If this rule is present, validation will only return possible errors if the field key exists,
668+
regardless of its value.
667669
in_list Yes Fails if field is not within a predetermined list. in_list[red,blue,green]
668670
integer No Fails if field contains anything other than an integer.
669671
is_natural No Fails if field contains anything other than a natural number: 0, 1, 2, 3, etc.
670672
is_natural_no_zero No Fails if field contains anything other than a natural number, except zero: 1, 2, 3, etc.
673+
is_unique Yes Checks if this field value exists in the database. Optionally set a is_unique[table.field,ignore_field,ignore_value]
674+
column and value to ignore, useful when updating records to ignore itself.
671675
less_than Yes Fails if field is greater than or equal to the parameter value or not numeric. less_than[8]
672676
less_then_equal_to Yes Fails if field is greater than the parameter value or not numeric. less_than_equal_to[8]
673677
matches Yes The value must match the value of the field in the parameter. matches[field]
674678
max_length Yes Fails if field is longer than the parameter value. max_length[8]
675679
min_length Yes Fails if field is shorter than the parameter value. min_length[3]
676680
numeric No Fails if field contains anything other than numeric characters.
677681
regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/]
678-
if_exist No If this rule is present, validation will only return possible errors if the field key exists,
679-
regardless of its value.
680682
permit_empty No Allows the field to receive an empty array, empty string, null or false.
681683
required No Fails if the field is an empty array, empty string, null or false.
682684
required_with Yes The field is required when any of the other required fields are present in the data. required_with[field1,field2]
683685
required_without Yes The field is required when all of the other fields are present in the data but not required. required_without[field1,field2]
684-
is_unique Yes Checks if this field value exists in the database. Optionally set a is_unique[table.field,ignore_field,ignore_value]
685-
column and value to ignore, useful when updating records to ignore itself.
686+
string No A generic alternative to the alpha* rules that confirms the element is a string
686687
timezone No Fails if field does match a timezone per ``timezone_identifiers_list``
687688
valid_base64 No Fails if field contains anything other than valid Base64 characters.
688689
valid_json No Fails if field does not contain a valid JSON string.

0 commit comments

Comments
 (0)