4
$rules = ['email' => 'required', 'password' => 'required'];
        $message = ['email.required' => 'Email cannot be empty', 'password.required' => 'Password cannot be empty'];

        $v = Validator::make($request->all(), $rules, $message);

        $error = $v->messages();
        if ($v->fails()) {
            return response()->json(["code" => "400", "error" => ["code" => "10", "title"=>"" ,"message" => $error->first()
            ]]);
        }

The code above would return an error block in my JSON

error : {
    code: 11
    message: "Some error message"
}

However I would add a title field like below.

error : {
    code: 11
    title: "Some error title"
    message: "Some error message"
}

How should I do this validation? I need the .required

Can anyone please explain a little bit please?

8
  • 1
    Yes because your title is null Commented Sep 12, 2015 at 8:10
  • 1
    What do you want to be the title? Commented Sep 12, 2015 at 8:15
  • I want something like. for example: if the email is empty. I want the title to be "Email is Empty" and the message "Please fill out your email" Commented Sep 12, 2015 at 8:16
  • Can you try this return response()->json(["code" => "400", "error" => ["code" => "10", "title"=> $message ,"message" => $rules]]); Commented Sep 12, 2015 at 8:24
  • It wouldn't work. @aldrin27 Commented Sep 12, 2015 at 8:27

1 Answer 1

1

if you want this kind of validation error message format, I think you should build something a little bit more "complex" of what actually you have out of the box.

Here's what you get with a simple validation test.

This is the code I used.

Route::get('/', function () {

    $data = ['name' => ''];
    $rules = ['name' => 'required'];

    $v = \Validator::make($data, $rules);

    dd($v->errors());

});

... and that's the output.

MessageBag {#145 ▼
  #messages: array:1 [▼
    "name" => array:1 [▼
      0 => "The name field is required."
    ]
  ]
  #format: ":message"
}

"Ok, so what I could do?"

First of all, some assumptions. I see that you're taking only the first error. So, you would get something like this:

"The name field is required."

Working this way, you lose the "name" information. All you have is a simple string message. Not a big deal, considering what you want to achieve.

So, I would work different. There are many ways to do what I'm going to do, I will just show you a procedure.

First of all, let's take the first element of the array.

$errors = $v->errors();
$firstElement = reset($errors);

dd($firstElement);

I will get something like this:

array:1 [▼
  "name" => array:1 [▼
    0 => "The name field is required."
  ]
]

Now, starting from this you could do something like

$key = key($firstElement);
$value = reset($firstElement)[0];

To get the key and the value.

Finally, you should use them to build your response.

Here's an example:

return response()->json(
[
    "code" => "400", 
    "error" => 
    [
        "code" => "10", 
        "title"=> "Field $key is empty." ,
        "message" => $value
    ]
]);

Obviously I suggest you to create a class to implement the entire mechanism, in order to isolate responsibilities.

Hope it helps.

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

Comments

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.