-
-
Save uno-de-piera/d16e67c81d3566736d200bd25962c0f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export default { | |
data() { | |
return { | |
errors: [] | |
} | |
}, | |
methods: { | |
async submit(data) { | |
this.errors = []; | |
try { | |
await Vue.axios({ | |
method: 'POST', | |
data, | |
url | |
}); | |
// si estamos aquí todo ha ido bien! | |
} catch (e) { | |
// errores del servidor | |
if(e.response.data.hasOwnProperty("errors")) { | |
const errors = e.response.data.errors; | |
for(let error in errors) { | |
if (errors.hasOwnProperty(error)) { | |
this.errors.push(errors[error][0]); | |
} | |
} | |
} | |
} finally { | |
// al finalizar en cualquier caso | |
} | |
}, | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ResourceController extends Controller { | |
public function store () { | |
$validator = \Illuminate\Support\Facades\Validator::make( | |
request()->all(), | |
[ | |
'title' => 'required|unique:posts|max:200' | |
], [ | |
'title.unique' => 'El camp título ya existe y debe ser único', | |
'title.required' => 'El camp título es requerido', | |
'title.max' => 'El campo título debe tener una longitud máxima de 200' | |
] | |
); | |
if ($validator->fails()) { | |
return response()->json(["errors" => $validator->getMessageBag()], 422); | |
} | |
// TODO continúa aquí | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment