1

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 Commented Nov 7, 2019 at 11:01
  • @lagbox, I want to return the data, which has been updated Commented Nov 7, 2019 at 11:03

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

in that case.. i need to put a default value form fields.. which is not present in the array. What about, if I don't want to put a default value?
If you only update title, then you only need to send title in the Request Body. Laravel will only validate title and would not validate other fields. It will then update title related to Question Model.
In that case, that's working. But, my question is: when I am trying to store data and not putting the title in array: that demand a default value: 1364 Field 'title' doesn't have a default value(showing the error). How to deal with that?
Then you should make that column nullable in your migration.
if I put nullable in migration and required that field in Request Validation, That shows the same error: 1364 Field 'title' doesn't have a default value
|
0

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;
        }
    }

Comments

0
`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',
    ];

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.