From e487bff6e3cda0678b4adb8feb07c56070d88943 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 8 May 2019 16:53:04 -0400 Subject: [PATCH] Add ends_with validation rule --- .../Concerns/ReplacesAttributes.php | 18 +++++++++++++ .../Concerns/ValidatesAttributes.php | 13 +++++++++ tests/Validation/ValidationValidatorTest.php | 27 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index fd2c16658be3..103c4f1ba525 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -470,6 +470,24 @@ protected function replaceDimensions($message, $attribute, $rule, $parameters) return $message; } + /** + * Replace all place-holders for the ends_with rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceEndsWith($message, $attribute, $rule, $parameters) + { + foreach ($parameters as &$parameter) { + $parameter = $this->getDisplayableValue($attribute, $parameter); + } + + return str_replace(':values', implode(', ', $parameters), $message); + } + /** * Replace all place-holders for the starts_with rule. * diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 98eda67680aa..584aec72d67a 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -1527,6 +1527,19 @@ public function validateSometimes() return true; } + /** + * Validate the attribute starts with a given substring. + * + * @param string $attribute + * @param mixed $value + * @param array $parameters + * @return bool + */ + public function validateEndsWith($attribute, $value, $parameters) + { + return Str::endsWith($value, $parameters); + } + /** * Validate the attribute starts with a given substring. * diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index a615df2e37db..3bdc082a9350 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1235,6 +1235,33 @@ public function testValidateAccepted() $this->assertTrue($v->passes()); } + public function testValidateEndsWith() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:hello']); + $this->assertFalse($v->passes()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world']); + $this->assertTrue($v->passes()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world,hello']); + $this->assertTrue($v->passes()); + + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en'); + $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http']); + $this->assertFalse($v->passes()); + $this->assertEquals('The url must end with one of the following values http', $v->messages()->first('url')); + + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en'); + $v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http,https']); + $this->assertFalse($v->passes()); + $this->assertEquals('The url must end with one of the following values http, https', $v->messages()->first('url')); + } + public function testValidateStartsWith() { $trans = $this->getIlluminateArrayTranslator();