Код: Выделить всё
{{ form:contact_us }}
{{ if success }}
Thanks!! We will be in touch soon.
{{ elseif errors }}
There are errors! Please fix these bellow:
{{ errors }}
{{ value }}
{{ /errors }}
{{ /if }}
{{ fields }}
{{ display }}[i]*[/i]:
{{ field }}
{{ /fields }}
{{ submit_request }}
{{ /form:contact_us }}
Правильно ли положить '/!/form contact_us '? < /p>
Код: Выделить всё
Route::post('/!/forms/contact_us', [ContactController::class, 'store'])->name('contact.store');
Код: Выделить всё
class ContactFormRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email',
'message' => 'required|string',
'phone' => ['required', new PhoneNumberValidation],
'country_code' => 'sometimes|string|size:2',
];
}
}
Код: Выделить всё
class PhoneNumberValidation implements Rule
{
protected $countryCode;
public function __construct($countryCode = 'US') // Default to US
{
$this->countryCode = $countryCode;
}
public function passes($attribute, $value): bool
{
try {
$phoneUtil = PhoneNumberUtil::getInstance();
$parsedNumber = $phoneUtil->parse($value, null);
return $phoneUtil->isValidNumber($parsedNumber);
} catch (NumberParseException $e) {
return false;
}
}
public function message(): string
{
return 'The phone number format is invalid.';
}
}
class ContactController extends Controller
{
public function store(ContactFormRequest $request)
{
$validated = $request->validated();
return redirect()->back()->with('success', 'Message sent successfully!');
}
}
< /code>
< /li>
< /ol>
Как сохранить проверенные данные запроса в store () (без взвешенного вмешательства. Как получить массив ошибок в шаблоне?
Подробнее здесь: https://stackoverflow.com/questions/794 ... eed-a-help
Мобильная версия