Home > OS >  Laravel 9 - on the fly custom request triggers validation immediately
Laravel 9 - on the fly custom request triggers validation immediately

Time:12-20

I need to create a Laravel custom request on the fly. But when i do, the validation get triggered immediately. I have no idea why.This is my code:

// this is in my ProjectController class. When a project gets created, i want the customer contact to be saved with it.

public function store(StoreProjectRequest $request)
{
    //dd($request->only('contact')['contact']);
    $storeContactReq = app()->make(\App\Http\Requests\StoreContactRequest::class);
    //the validation gets triggered right here, before i have the chance to add my request data
    // even if i dd() here, i dont even reach this line of code
    $storeContactReq->request->add($request->only('contact')['contact']); //yes this is correct
    $storeContactReq->setMethod('POST');
       
    $cc = new ContactController();
    $con = $cc->store($storeContactReq);
    dd($con);
    //....
}

Here's my StoreContactRequest:

class StoreContactRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        //later
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            "lastname" => 'required',
            "firstname" => 'required',
            "email" => 'required|email',
            "phone" => 'required',
            "street" => 'required',
            "house" => 'required',
            "postcode" => 'required',
            "city" => 'required',
        ];
    }

    public function messages()
    {
        return [
            'firstname.required' => 'Vorname ist leer',
            'lastname.required' => 'Nachname ist leer',
            'street.required' => 'Straße ist leer',
            'house.required' => 'Hausnummer ist leer',
            'postcode.required' => 'Postleitzahl ist leer',
            'city.required' => 'Ort ist leer',
            'phone.required' => 'Telefon ist leer',
            'email.required' => 'Email ist leer',
            'email.email' => 'Email ist keine korrekte Mailadresse'
        ];
    }

    public function failedValidation(Validator $validator)
    {
        dd($validator->errors());
    }

The error message:

Illuminate\Support\MessageBag^ {#1359 // app/Http/Requests/StoreContactRequest.php:56
  #messages: array:8 [
    "lastname" => array:1 [
      0 => "Nachname ist leer"
    ]
    "firstname" => array:1 [
      0 => "Vorname ist leer"
    ]
    "email" => array:1 [
      0 => "Email ist leer"
    ]
    "phone" => array:1 [
      0 => "Telefon ist leer"
    ]
    "street" => array:1 [
      0 => "Straße ist leer"
    ]
    "house" => array:1 [
      0 => "Hausnummer ist leer"
    ]
    "postcode" => array:1 [
      0 => "Postleitzahl ist leer"
    ]
    "city" => array:1 [
      0 => "Ort ist leer"
    ]
  ]
  #format: ":message"
}

I googled a lot and looked into the documentation, but i just cant find anything about this problem...

I expect the validation to be triggered inside the ContactController::store() function, not when i manually create the StoreContactRequest and before i can add any data to it....

thx

CodePudding user response:

The data is already in the request. You are able to validate that with Request. You can manually add custom data within the StoreContactRequest rules() method if that's necessary or even you can create custom validation rules: https://laravel.com/docs/9.x/validation#custom-validation-rules

CodePudding user response:

I solved it by adding local validators in the store() methods

$validator = Validator::make($request->all(), [
            'lastname' => 'required',
            'firstname' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
            'street' => 'required',
            'house' => 'required',
            'postcode' => 'required',
            'city' => 'required',
        ]);
        $validated = $validator->validate();

        //$request->validate($request->all());
        $con = Contact::create($validated);

Thats not very convienient, and i guess ill get problems with auth here, but for now it works. Thx all

  • Related