Skip to content

Commit

Permalink
Add ends_with validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed May 8, 2019
1 parent ada3503 commit e487bff
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e487bff

Please sign in to comment.