mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-12 14:12:44 +01:00
Convest rule to validationrule
This commit is contained in:
parent
3e4cf6f261
commit
a88b4d5d8d
@ -11,32 +11,26 @@
|
||||
|
||||
namespace App\Http\ValidationRules\Account;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
/**
|
||||
* Class EmailBlackListRule.
|
||||
*/
|
||||
class EmailBlackListRule implements Rule
|
||||
class EmailBlackListRule implements ValidationRule
|
||||
{
|
||||
public array $blacklist = [
|
||||
|
||||
'noddy@invoiceninja.com',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
return ! in_array($value, $this->blacklist);
|
||||
|
||||
if (in_array($value, $this->blacklist)) {
|
||||
$fail('This email address is blacklisted, if you think this is in error, please email contact@invoiceninja.com');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return 'This email address is blacklisted, if you think this is in error, please email contact@invoiceninja.com';
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,9 @@
|
||||
|
||||
namespace Tests\Unit\ValidationRules;
|
||||
|
||||
use App\Http\ValidationRules\Account\BlackListRule;
|
||||
use Tests\TestCase;
|
||||
use App\Http\ValidationRules\Account\BlackListRule;
|
||||
use App\Http\ValidationRules\Account\EmailBlackListRule;
|
||||
|
||||
/**
|
||||
* @test
|
||||
@ -25,6 +26,35 @@ class BlacklistValidationTest extends TestCase
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testValidEmailRule3()
|
||||
{
|
||||
$rules = [
|
||||
'email' => [new EmailBlackListRule],
|
||||
];
|
||||
|
||||
$data = [
|
||||
'email' => 'contact@invoiceninja.com',
|
||||
];
|
||||
|
||||
$v = $this->app['validator']->make($data, $rules);
|
||||
$this->assertTrue($v->passes());
|
||||
}
|
||||
|
||||
|
||||
public function testValidEmailRule2()
|
||||
{
|
||||
$rules = [
|
||||
'email' => [new EmailBlackListRule],
|
||||
];
|
||||
|
||||
$data = [
|
||||
'email' => 'noddy@invoiceninja.com',
|
||||
];
|
||||
|
||||
$v = $this->app['validator']->make($data, $rules);
|
||||
$this->assertFalse($v->passes());
|
||||
}
|
||||
|
||||
public function testValidEmailRule()
|
||||
{
|
||||
$rules = [
|
||||
|
Loading…
Reference in New Issue
Block a user