Skip to content

Commit a570783

Browse files
authored
Merge pull request #1957 from codeigniter4/stringrule
New generic string validation rule.
2 parents 34b7657 + 071e5ea commit a570783

3 files changed

Lines changed: 76 additions & 19 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: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -660,31 +660,30 @@ alpha_dash No Fails if field contains anything other than
660660
alpha_numeric No Fails if field contains anything other than alpha-numeric characters or numbers.
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.
663-
differs Yes Fails if field does not differ from the one in the parameter. differs[field_name]
664-
equals Yes Fails if field is not exactly the parameter value.
665-
exact_length Yes Fails if field is not exactly the parameter value in length. One or more comma-separated values. exact_length[5] or exact_length[5,8,12]
666-
greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]
667-
greater_than_equal_to Yes Fails if field is less than the parameter value, or not numeric. greater_than_equal_to[5]
668-
in_list Yes Fails if field is not within a predetermined list. in_list[red,blue,green]
663+
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]
665+
greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]
666+
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.
669+
in_list Yes Fails if field is not within a predetermined list. in_list[red,blue,green]
669670
integer No Fails if field contains anything other than an integer.
670671
is_natural No Fails if field contains anything other than a natural number: 0, 1, 2, 3, etc.
671672
is_natural_no_zero No Fails if field contains anything other than a natural number, except zero: 1, 2, 3, etc.
672-
less_than Yes Fails if field is greater than or equal to the parameter value or not numeric. less_than[8]
673-
less_then_equal_to Yes Fails if field is greater than the parameter value or not numeric. less_than_equal_to[8]
674-
matches Yes The value must match the value of the field in the parameter. matches[field]
675-
max_length Yes Fails if field is longer than the parameter value. max_length[8]
676-
min_length Yes Fails if field is shorter than the parameter value. min_length[3]
677-
not_equals Yes Fails if field is exactly the parameter value.
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.
675+
less_than Yes Fails if field is greater than or equal to the parameter value or not numeric. less_than[8]
676+
less_then_equal_to Yes Fails if field is greater than the parameter value or not numeric. less_than_equal_to[8]
677+
matches Yes The value must match the value of the field in the parameter. matches[field]
678+
max_length Yes Fails if field is longer than the parameter value. max_length[8]
679+
min_length Yes Fails if field is shorter than the parameter value. min_length[3]
678680
numeric No Fails if field contains anything other than numeric characters.
679-
regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/]
680-
if_exist No If this rule is present, validation will only return possible errors if the field key exists,
681-
regardless of its value.
681+
regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/]
682682
permit_empty No Allows the field to receive an empty array, empty string, null or false.
683683
required No Fails if the field is an empty array, empty string, null or false.
684-
required_with Yes The field is required when any of the other required fields are present in the data. required_with[field1,field2]
685-
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]
686-
is_unique Yes Checks if this field value exists in the database. Optionally set a is_unique[table.field,ignore_field,ignore_value]
687-
column and value to ignore, useful when updating records to ignore itself.
684+
required_with Yes The field is required when any of the other required fields are present in the data. required_with[field1,field2]
685+
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]
686+
string No A generic alternative to the alpha* rules that confirms the element is a string
688687
timezone No Fails if field does match a timezone per ``timezone_identifiers_list``
689688
valid_base64 No Fails if field contains anything other than valid Base64 characters.
690689
valid_json No Fails if field does not contain a valid JSON string.

0 commit comments

Comments
 (0)