Laravel form request

From AWVVO
Jump to navigationJump to search

FormRequest template

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class StoreFmeoRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        // Update this to implement your authorization logic.
        // Returning true allows all authenticated users to make this request.
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'username' => 'nullable|string|max:255',
            'password' => 'nullable|string|max:255',
            'url' => 'nullable|url|max:255',
            'notes' => 'nullable|string',
        ];
    }

    /**
     * Optional: Customize the error messages for validation rules.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'title.required' => 'The title field is required.',
            'title.string' => 'The title must be a string.',
            'title.max' => 'The title may not be greater than 255 characters.',
            'username.string' => 'The username must be a string.',
            'username.max' => 'The username may not be greater than 255 characters.',
            'password.string' => 'The password must be a string.',
            'password.max' => 'The password may not be greater than 255 characters.',
            'url.url' => 'The URL format is invalid.',
            'url.max' => 'The URL may not be greater than 255 characters.',
            'notes.string' => 'The notes must be a string.',
        ];
    }


}

When not getting sql errors back

// add header in request
 'Accept': 'application/json'