Skip to content

Commit 4624c2d

Browse files
committed
Add rule EthereumAddress
1 parent 751e0bf commit 4624c2d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

resources/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'credit_card' => 'The :attribute must be a valid credit card number.',
1111
'data_uri' => 'The :attribute must have data uri format.',
1212
'divisible_by' => 'The :attribute must be divisible by :number.',
13+
'ethereum_address' => 'The :attribute must be an Ethereum address.',
1314
'float_number' => 'The :attribute must be a float number',
1415
'image_url' => 'The :attribute must be a valid image URL.',
1516
'phone' => 'The :attribute must be a valid phone number.',

src/Rules/EthereumAddress.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Arifszn\AdvancedValidation\Rules;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
/**
8+
* The field under validation must be an Ethereum address. Does not validate address checksums.
9+
*
10+
* @package Arifszn\AdvancedValidation\Rules
11+
*/
12+
class EthereumAddress implements Rule
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $errorMessage;
18+
19+
/**
20+
* Create a new rule instance.
21+
*
22+
* @param string|null $errorMessage Custom error message.
23+
* @return void
24+
*/
25+
public function __construct(string $errorMessage = null)
26+
{
27+
$this->errorMessage = $errorMessage;
28+
}
29+
30+
/**
31+
* Determine if the validation rule passes.
32+
*
33+
* @param string $attribute
34+
* @param mixed $value
35+
* @return bool
36+
*/
37+
public function passes($attribute, $value)
38+
{
39+
return preg_match('/^(0x)[0-9a-f]{40}$/i', $value);
40+
}
41+
42+
/**
43+
* Get the validation error message.
44+
*
45+
* @return string
46+
*/
47+
public function message()
48+
{
49+
return $this->errorMessage ? $this->errorMessage : trans('advancedValidation::validation.ethereum_address');
50+
}
51+
}

0 commit comments

Comments
 (0)