Skip to content

Commit

Permalink
Added validation doesnt_end_with rule (laravel#43518)
Browse files Browse the repository at this point in the history
  • Loading branch information
kichetof authored and Ken committed Aug 9, 2022
1 parent 3465d1e commit e20eaac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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

0 comments on commit e20eaac

Please sign in to comment.