Я пробовал использовать следующие правила, в которых я изменяю правило di git на строку, так как laravel не имеет правила для цифр плюс da sh.
'mobile' => 'nullable | string | min: 10 | max: 10 | regex: / ^ \ d {1} - \ d {4} - \ d {3} $ /',
Возможно выше Решение помогает вам.
Обновление
Чтобы показать пользователю соответствующее сообщение об ошибке, вам необходимо создать пользовательское правило
Выполните следующую команду
php artisan make: rule NumericDa sh
Обновите файл NumericDa sh. php со следующим содержимым
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class NumericDash implements Rule
{
private $message = "The :attribute format is invalid.";
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($digitsWithoutDash, $regex)
{
$this->digitsWithoutDash = $digitsWithoutDash;
$this->regex = $regex;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (strlen(preg_replace('/[^0-9]/', '', $value)) !== $this->digitsWithoutDash) {
$this->message = "The :attribute must include {$this->digitsWithoutDash} digits.";
return false;
}
if (!preg_match($this->regex, $value)) {
return false;
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
}
и используйте правило, как показано ниже
'mobile' => ['nullable', 'string', new NumericDa sh (8, '/ ^ \ d { 1} - \ d {4} - \ d {3} $ / ')],