Skip to content
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

Added validation doesnt_end_with rule #43518

Merged
merged 1 commit into from Aug 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Expand Up @@ -610,6 +610,24 @@ protected function replaceEndsWith($message, $attribute, $rule, $parameters)
return str_replace(':values', implode(', ', $parameters), $message);
}

/**
* Replace all place-holders for the doesnt_end_with rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,string> $parameters
* @return string
*/
protected function replaceDoesntEndWith($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
Expand Up @@ -1976,6 +1976,19 @@ public function validateEndsWith($attribute, $value, $parameters)
return Str::endsWith($value, $parameters);
}

/**
* Validate the attribute does not end with a given substring.
*
* @param string $attribute
* @param mixed $value
* @param array<int, int|string> $parameters
* @return bool
*/
public function validateDoesntEndWith($attribute, $value, $parameters)
{
return ! Str::endsWith($value, $parameters);
}

/**
* Validate that an attribute is a string.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -2131,6 +2131,17 @@ public function testValidateEndsWith()
$this->assertSame('The url must end with one of the following values http, https', $v->messages()->first('url'));
}

public function testValidateDoesntEndWith()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'hello world'], ['x' => 'doesnt_end_with:hello']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['x' => 'hello world'], ['x' => 'doesnt_end_with:world']);
$this->assertFalse($v->passes());
}

public function testValidateStartsWith()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down