Skip to content

[5.8] Add ends_with validation rule #28455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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