Danny620 Posted October 15, 2014 Share Posted October 15, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/ Share on other sites More sharing options...
Danny620 Posted October 15, 2014 Author Share Posted October 15, 2014 anyhelp? Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493587 Share on other sites More sharing options...
ginerjm Posted October 15, 2014 Share Posted October 15, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493588 Share on other sites More sharing options...
Danny620 Posted October 15, 2014 Author Share Posted October 15, 2014 (edited) 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 October 15, 2014 by Danny620 Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493589 Share on other sites More sharing options...
ginerjm Posted October 15, 2014 Share Posted October 15, 2014 Good. Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493590 Share on other sites More sharing options...
Danny620 Posted October 15, 2014 Author Share Posted October 15, 2014 Good. helpful? Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493591 Share on other sites More sharing options...
ginerjm Posted October 15, 2014 Share Posted October 15, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493593 Share on other sites More sharing options...
Barand Posted October 15, 2014 Share Posted October 15, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493611 Share on other sites More sharing options...
Danny620 Posted October 16, 2014 Author Share Posted October 16, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291633-access-errors-field/#findComment-1493807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.