Phone Rule

0
Правило валидации для номера телефона
app/Rules/Phone.php
                            
<?php
 
namespace App\Rules;
 
use Illuminate\Contracts\Validation\Rule;
 
class Phone implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
 
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return preg_match("/^((\+7|7|8)+([0-9]){10})$/", $value);
}
 
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute is not correct phone number';
}
}
app/Http/Requests/UserProfileRequest.php
                            
<?php
 
namespace App\Http\Requests;
 
use App\Rules\Phone;
use Illuminate\Foundation\Http\FormRequest as HttpFormRequest;
 
class UserProfileRequest extends HttpFormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
 
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
"name" => "required|max:255",
"email" => 'sometimes|bail|required|email|unique:users,email,' . auth()->id(),
"password" => 'sometimes|nullable|min:6|required_with:password_confirmation|same:password_confirmation',
"phone" => [new Phone]
];
}
}