1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- // Or use axios directly instead of this.$http.
- Vue.prototype.$http = axios;
- class Errors {
- constructor() {
- this.clear();
- }
- any() {
- return Object.keys(this.errors).length != 0;
- }
- clear(name) {
- console.log("clearing", name);
- this.errors = {};
- }
- get(field) {
- if (this.errors[field]) {
- return this.errors[field];
- }
- }
- has(field) {
- return this.errors.hasOwnProperty(field);
- }
- record(errors) {
- this.errors = errors;
- }
- }
- const app = new Vue({
- el: '#root',
- data: {
- name: '',
- description: '',
- errors: new Errors(),
- },
- methods: {
- /**
- *
- * @param {Event} e
- */
- onSubmit(e) {
- // or use @submit.prevent at the HTML call point
- // e.preventDefault();
- this.$http.post('index.php/projects', this.$data)
- .then(this.onSuccess)
- .catch(error => this.errors.record({'name': error.response.data}));
- },
- onSuccess() {
- alert('Success');
- }
- }
- });
|