diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index a8f3b11a1126..d95059f769b1 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -1196,6 +1196,19 @@ public function validateUppercase($attribute, $value, $parameters) return Str::upper($value) === $value; } + /** + * Validate that an attribute is snakecase. + * + * @param string $attribute + * @param mixed $value + * @param array $parameters + * @return bool + */ + public function validateSnakecase($attribute, $value, $parameters) + { + return Str::snake($value) === $value; + } + /** * Validate the MIME type of a file is an image MIME type. * diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index f73b8865ab0b..9371800f71b8 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1857,6 +1857,42 @@ public function testUppercase() ], $v->messages()->keys()); } + public function testSnakecase() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, [ + 'lower' => 'lowercase', + 'mixed' => 'MixedCase', + 'upper' => 'UPPERCASE', + 'lower_multibyte' => 'carácter_multibyte', + 'mixed_multibyte' => 'carÁcter_multibyte', + 'upper_multibyte' => 'CARÁCTER_MULTIBYTE', + 'lower_multibyte_space' => 'carácter multibyte', + 'mixed_multibyte_space' => 'carÁcter multibyte', + 'upper_multibyte_space' => 'CARÁCTER MULTIBYTE', + ], [ + 'lower' => 'snakecase', + 'mixed' => 'snakecase', + 'upper' => 'snakecase', + 'lower_multibyte' => 'snakecase', + 'mixed_multibyte' => 'snakecase', + 'upper_multibyte' => 'snakecase', + 'lower_multibyte_space' => 'snakecase', + 'mixed_multibyte_space' => 'snakecase', + 'upper_multibyte_space' => 'snakecase', + ]); + + $this->assertSame([ + 'mixed', + 'upper', + 'mixed_multibyte', + 'upper_multibyte', + 'lower_multibyte_space', + 'mixed_multibyte_space', + 'upper_multibyte_space', + ], $v->messages()->keys()); + } + public function testLessThan() { $trans = $this->getIlluminateArrayTranslator();