Skip to content

Instantly share code, notes, and snippets.

@uno-de-piera
Last active August 13, 2019 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uno-de-piera/d16e67c81d3566736d200bd25962c0f4 to your computer and use it in GitHub Desktop.
Save uno-de-piera/d16e67c81d3566736d200bd25962c0f4 to your computer and use it in GitHub Desktop.
<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>
<?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