Skip to content

Commit e487bff

Browse files
committed
Add ends_with validation rule
1 parent ada3503 commit e487bff

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Illuminate/Validation/Concerns/ReplacesAttributes.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,24 @@ protected function replaceDimensions($message, $attribute, $rule, $parameters)
470470
return $message;
471471
}
472472

473+
/**
474+
* Replace all place-holders for the ends_with rule.
475+
*
476+
* @param string $message
477+
* @param string $attribute
478+
* @param string $rule
479+
* @param array $parameters
480+
* @return string
481+
*/
482+
protected function replaceEndsWith($message, $attribute, $rule, $parameters)
483+
{
484+
foreach ($parameters as &$parameter) {
485+
$parameter = $this->getDisplayableValue($attribute, $parameter);
486+
}
487+
488+
return str_replace(':values', implode(', ', $parameters), $message);
489+
}
490+
473491
/**
474492
* Replace all place-holders for the starts_with rule.
475493
*

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,19 @@ public function validateSometimes()
15271527
return true;
15281528
}
15291529

1530+
/**
1531+
* Validate the attribute starts with a given substring.
1532+
*
1533+
* @param string $attribute
1534+
* @param mixed $value
1535+
* @param array $parameters
1536+
* @return bool
1537+
*/
1538+
public function validateEndsWith($attribute, $value, $parameters)
1539+
{
1540+
return Str::endsWith($value, $parameters);
1541+
}
1542+
15301543
/**
15311544
* Validate the attribute starts with a given substring.
15321545
*

tests/Validation/ValidationValidatorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,33 @@ public function testValidateAccepted()
12351235
$this->assertTrue($v->passes());
12361236
}
12371237

1238+
public function testValidateEndsWith()
1239+
{
1240+
$trans = $this->getIlluminateArrayTranslator();
1241+
$v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:hello']);
1242+
$this->assertFalse($v->passes());
1243+
1244+
$trans = $this->getIlluminateArrayTranslator();
1245+
$v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world']);
1246+
$this->assertTrue($v->passes());
1247+
1248+
$trans = $this->getIlluminateArrayTranslator();
1249+
$v = new Validator($trans, ['x' => 'hello world'], ['x' => 'ends_with:world,hello']);
1250+
$this->assertTrue($v->passes());
1251+
1252+
$trans = $this->getIlluminateArrayTranslator();
1253+
$trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en');
1254+
$v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http']);
1255+
$this->assertFalse($v->passes());
1256+
$this->assertEquals('The url must end with one of the following values http', $v->messages()->first('url'));
1257+
1258+
$trans = $this->getIlluminateArrayTranslator();
1259+
$trans->addLines(['validation.ends_with' => 'The :attribute must end with one of the following values :values'], 'en');
1260+
$v = new Validator($trans, ['url' => 'laravel.com'], ['url' => 'ends_with:http,https']);
1261+
$this->assertFalse($v->passes());
1262+
$this->assertEquals('The url must end with one of the following values http, https', $v->messages()->first('url'));
1263+
}
1264+
12381265
public function testValidateStartsWith()
12391266
{
12401267
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)