mardi 29 décembre 2015

Using a CustomRequest for Form Validation in Laravel 5.1 fails?

I've created a custom Request called CustomerRequest that I want to use to validate the form fields when a new customer is created. I cannot get it to work, it appears to be continuing into the store() method even when it should fail.

I have three required fields: givenname, surname, email

Here is my CustomerRequest:

public function rules()
{
    return [
        'givenname' => 'required|max:3',
        'surname' => 'required',
        'email' => 'required|unique:customers,email',
    ];
}

Here is my CustomerController:

use pams\Http\Requests\CustomerRequest;

-----------------------------------------

public function store(CustomerRequest $request, Customer $customer)
{
    $request['created_by'] = Auth::user()->id;
    $request['modified_by'] = Auth::user()->id;

    $customer->create($request->all());

    return redirect('customers');
}

When I submit the form using a givenname of "Vince" it should fail because it is greater than 3 characters long, but instead I get this error:

FatalErrorException in CustomerController.php line 52: Cannot use object of type pams\Http\Requests\CustomerRequest as array

Line 52 in the controller is $request['created_by'] = Auth::user()->id;

From what I understand, if the CustomerRequest fails then the user should be redirected back to the customers.create page and not run the code contained in the store() method.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire