Comments (7)
Hey @travisstaloch. This package provides only a wrapper around RawmodelJS which adds VueJS reactivity to Model
. Reactivity is only needed for reactive forms (reactive form validation). RawmodelJS works on the server and in the browser thus you can share your main logic between the server and the browser.
// client
import {ReactiveModel} from 'vue-rawmodel';
class User extends ReactiveModel {
// main logic here
}
// server
import {Model} from 'rawmodel';
class User extends Model {
// main logic here
}
from vue-rawmodel.
That should do the trick.
from vue-rawmodel.
I was hoping to avoid that. I'm thinking I can use a condition() in the password2 validation which does some sort of check to switch it off, maybe check if typeof this !== 'SignupModelServer'
from vue-rawmodel.
Ok thanks. I didn't realize that its only a wrapper for reactivity.
Is it possible to put the validation logic in one place then like this?
class User extends ReactiveModel {
constructor(data = {}) {
defineValidations(this)
}
}
class User extends Model {
constructor(data = {}) {
defineValidations(this)
}
}
from vue-rawmodel.
Is it possible to ignore a field defined in a model? I have SignupModel < ReactiveModel with fields email, password, and confirmPassword which shares validation definitions with SignupModelServer < Model as described above. I want to ignore the confirmPassword field on the server. Is there an easy way to achieve this?
from vue-rawmodel.
Just have multiple models.
from vue-rawmodel.
I think I found a solution: obj isInstanceOf ReactiveModel
is more generic and can be reused in different models. Thought I would share to help out as it also shows a working defineValidator to compare with another field in the model which took a little bit of research to get working.
signup.js
import { ReactiveModel } from 'vue-rawmodel'
import { Model } from 'rawmodel'
export class SignupModel extends ReactiveModel {
constructor(data = {}) {
super(data)
defineValidations(this, data)
}
}
export class SignupModelServer extends Model {
constructor(data = {}) {
super(data)
defineValidations(this, data)
}
}
function defineValidations(that, data) {
that.defineField('email', {
type: 'String',
validate: [
{
validator: 'presence',
message: 'is required'
},
{
validator: 'stringEmail',
message: 'is invalid'
}
]
})
that.defineField('password', {
type: 'String',
validate: [
{
validator: 'presence',
message: 'is required'
},
{
validator: 'stringLength',
message: 'is too short or too long (%{min} to %{max} characters)',
min: 4,
max: 33
}
]
})
that.defineValidator('equalsPassword', v => {
return v === that.getField(['password']).value
})
that.defineField('password2', {
type: 'String',
validate: [
{
validator: 'presence',
message: 'is required',
condition() {
return that instanceof ReactiveModel
}
},
{
validator: 'equalsPassword',
message: 'is different from password',
condition() {
return that instanceof ReactiveModel
}
}
]
})
that.populate(data)
}
from vue-rawmodel.
Related Issues (13)
- Example on loading data into model HOT 4
- How to set up a date field properly? HOT 4
- Nested models and reactivity
- Update for current rawmodel? HOT 3
- Required if validator HOT 6
- Clarification of how to use nested schemas/models HOT 5
- Saving only changed and valid fields HOT 4
- Docs for use with server rendering HOT 2
- Using Vuex HOT 1
- new API and renaming from vue-contextable to vue-rawmodel
- Decorator support HOT 2
- Any way to check if the field is dirty? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-rawmodel.