Jump to content

Access errors field


Danny620

Recommended Posts

Hi,

 

I am building a restfull service and i would like to out put a error like the following

{
    "error": true,
    "message": "Validation failed",
    "code": 400,
    "errors": [
        {
            "field": "first_name",
            "error": "The first name field is required."
        },
        {
            "field": "first_name",
            "error": "The last name field is required."
        },
        {
            "field": "first_name",
            "error": "The gender field is required."
        },
        {
            "field": "first_name",
            "error": "The city id field is required."
        },
        {
            "field": "first_name",
            "error": "The postcode field is required."
        },
        {
            "field": "first_name",
            "error": "The email field is required."
        },
        {
            "field": "first_name",
            "error": "The password field is required."
        }
    ]
}

but i cant seem to read the array key as what the vailidation failed on

$validator = Validator::make(Input::all(), $rules);

    	if ($validator->fails())
    	{

    		$errors = array();
    		$messages = $validator->messages();

    		//print_r($messages);

    		foreach ($messages->all() as $value)
			{
    			$errors[] = array('field' => 'first_name',
    							  'error' => $value
    							 );
			}

			$response = Response::json(array(
	            'error'   => true,
	            'message' => 'Validation failed',
	            'code' 	  => 400,
	            'errors'  => $errors
	            				)
	        );

	        //$validator->messages()

    		return $response;
    	}
Link to comment
Share on other sites

I have no idea what your post is.  That first piece of code - what is it?  If it is supposed to represent an array, it's a syntax I am not familiar with.  If it's not, then what is it?  And what is your question exactly?  Output a message like what?

Link to comment
Share on other sites

I have no idea what your post is.  That first piece of code - what is it?  If it is supposed to represent an array, it's a syntax I am not familiar with.  If it's not, then what is it?  And what is your question exactly?  Output a message like what?

 

Hi where it says  'field' => 'first_name', I want that to be the field the error occured on and also the syntax is the laravel

Edited by Danny620
Link to comment
Share on other sites

When you ask for help here it is a good idea to not post blind code that has no context and for which you have not provided any English translation of its context.  What are we supposed to do - guess what you are trying to do as well as solve your problem?

 

Why not start again and tell us what you have posted and what it is supposed to be?  Then give us a synopsis of what you are expecting that code to do.

Link to comment
Share on other sites

if your first bit of code is in the variable $response then

$r = json_decode($response, 1);
foreach ($r['errors'] as $e) {
    echo "{$e['field']} : {$e['error']}<br>";
}

BTW, why are you hard-coding the field name as 'first_name' for all errors?

Hi Sean,

 

that is the bit i want to fix

 

BTW, why are you hard-coding the field name as 'first_name' for all errors?

 

i want it the be the field name with the error but i dont know how to access the field name in laravel errors can you help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.