Skip to content

Instantly share code, notes, and snippets.

@kura1420
Last active June 9, 2025 03:34
Show Gist options
  • Select an option

  • Save kura1420/c37eefb874d660476d65c27e94abe286 to your computer and use it in GitHub Desktop.

Select an option

Save kura1420/c37eefb874d660476d65c27e94abe286 to your computer and use it in GitHub Desktop.
Laravel Rule Coordinate
<?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