1
votes

I am working on updating data in API endpoint. As, I am using Form Request Validation to keep the validation separate from Controller.

That works fine with the Store Request.

But, when I am trying to update a single data as mentioned below, the response is returning the following response data.

As, the data are already stored in database. When I send request for updating data.. that should return the updated data. But, It's not returning.

Need help to solve that.

JSON Data in Body:

{
    "title": "Question Changed 2"
}

Current Response:

{
  "question_type": [
    "Question Type is Required!"
  ],
  "question": [
    "Question is Required!"
  ],
  "is_required": [
    "Is Require Value is Required"
  ]
}

QuestionRequest:

public function rules()
    {
        return [
            'question_type' => 'required | min:3 | max:10',
            'title' => 'required | min:5 | max:100',
            'question' => 'required | min:5 | max:255',
            'description' => 'min:10 | max:255',
            'is_required' => 'required',
        ];
    }

Update:

public function update(QuestionRequest $request, Question $question)
    {
        $question = $question->update($request->all());

        return response()->json($question, 200);
    }
2
what are you expecting to be returned from your controller method? as update returns a boolean - lagbox
@lagbox, I want to return the data, which has been updated - tanjiya

2 Answers

1
votes

You need to use the sometimes validation rule as mentioned in Docs

In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list

You can change the rules array as

        return [
            'question_type' => 'sometimes|required | min:3 | max:10',
            'title' => 'sometimes|required | min:5 | max:100',
            'question' => 'sometimes|required | min:5 | max:255',
            'description' => 'sometimes|min:10 | max:255',
            'is_required' => 'sometimes|required',
        ];

This way the validation on these fields will only be run if they are present in the request.

0
votes

If anyone need the solution, I am updating the answer:

I have simply put the conditional logic in Request Validation Rules as following. Added sometime according to @ascsoftw for updating data.

public function rules()
    {
        switch($this->method())
        {
            case 'POST':
                {
                    return [
                        'question_type' => 'required | min:3 | max:10',
                        'title' => 'required | min:5 | max:100',
                        'question' => 'required | min:5 | max:255',
                        'description' => 'min:10 | max:255',
                        'is_required' => 'required',
                    ];
                }
                break;

            case 'PUT':
                {
                    return [
                        'question_type' => 'sometimes | required | min:3 | max:10',
                        'title' => 'sometimes | required | min:5 | max:100',
                        'question' => 'sometimes | required | min:5 | max:255',
                        'description' => 'sometimes | min:10 | max:255',
                        'is_required' => 'sometimes | required',
                    ];
                }
                break;

            default:
                break;
        }
    }