Last active
June 9, 2025 03:34
-
-
Save kura1420/c37eefb874d660476d65c27e94abe286 to your computer and use it in GitHub Desktop.
Laravel Rule Coordinate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Rules; | |
| use Closure; | |
| use Illuminate\Contracts\Validation\ValidationRule; | |
| class CoordinateRule implements ValidationRule | |
| { | |
| /** | |
| * Run the validation rule. | |
| * | |
| * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | |
| */ | |
| public function validate(string $attribute, mixed $value, Closure $fail): void | |
| { | |
| // | |
| $coordinates = $value['coordinates']; | |
| if ( | |
| ! isset($coordinates) | |
| || ! is_array($coordinates) | |
| || count($coordinates) != 2 | |
| || ! isset($coordinates[0]) | |
| || ! isset($coordinates[1]) | |
| || ! is_numeric($coordinates[0]) | |
| || ! is_numeric($coordinates[1]) | |
| || $coordinates[0] < -90 | |
| || $coordinates[0] > 90 | |
| || $coordinates[1] < -180 | |
| || $coordinates[1] > 180 | |
| ) { | |
| $fail("Format :attribute is wrong"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment