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
https://forums.phpfreaks.com/topic/291633-access-errors-field/
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
https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493588
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

Link to comment
https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493589
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
https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493593
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?

Link to comment
https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493611
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
https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493807
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.