mercredi 22 juin 2016

Laravel 5.2 validation of PUT/PATCH request on REST controller

I have problem with laravel throwing me 405 MethodNotAllowedHttpException. I have been trying to add validation on update request to my resource PointController. However validation if its in @update method is ignored(and Point is updated without validation). So by anwsers i found on laracast I created something like this:

class PointRequest extends Request{
public function authorize()
{
        return true;
}
public function rules()
{
   switch($this->method())
    {
    case 'GET':
    case 'DELETE':
    {
     return[];
    }
    case 'POST':
    {
     return [
            'name' =>'required|min:4', 
            'latitude' => 'required|numeric|unique_with:points,longitude',
            'longitude' => 'required|numeric'
            ];
    }
    case 'PUT':
    case 'PATCH':
    {
    return [
            'name' =>'required|min:4',
            'latitude' => 'required|numeric|unique_with:points,longitude',
            'longitude' => 'required|numeric|unique_with:points,latitude'//will add ignore current row
            ];
    }
     default:break;
    }
}
}

Sadly it doesn't work. To test my application I am using POSTMAN with x-www-form-urlencoded. General idea of my project is that there is native android app that will communicate with server app (laravel) by jsons. So I don't think this is relevant in my case:

form action="/foo/bar" method="POST" 
input type="hidden" name="_method" value="PUT"

Here is part of PointController

public function update(PointRequest $request, $id)
{//code//

Did I make some grave mistake somewhere? How I can make laravel validate my PUT/PATCH request? It is my first question here so I hope I asked the right way.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire