Skip to content

Instantly share code, notes, and snippets.

@JamesRagonesi
Created November 3, 2019 17:55
Show Gist options
  • Save JamesRagonesi/a334036695191735cb3d12bd74d55972 to your computer and use it in GitHub Desktop.
Save JamesRagonesi/a334036695191735cb3d12bd74d55972 to your computer and use it in GitHub Desktop.
Reusable Forms - Model
import { Model } from "@vuex-orm/core";
import { required, maxLength, minLength } from "vuelidate/lib/validators";
export default class Comment extends Model {
static fields() {
return {
id: this.attr(null),
text: this.string(""),
};
}
// Keep validations within the model
static get validations() {
return {
form: {
text: { required, maxLength: maxLength(24), minLength: minLength(6) },
}
};
}
// Call to send new POST request
toCreatePayload() {
return {
text: this.text,
};
}
// Call to send new PUT request
toUpdatePayload() {
return {
...this.toCreatePayload(),
id: this.$id
};
}
}
Comment.entity = "comments";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment