Giter Site home page Giter Site logo

Comments (7)

xpepermint avatar xpepermint commented on June 22, 2024 1

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.

xpepermint avatar xpepermint commented on June 22, 2024 1

That should do the trick.

from vue-rawmodel.

travisstaloch avatar travisstaloch commented on June 22, 2024 1

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.

travisstaloch avatar travisstaloch commented on June 22, 2024

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.

travisstaloch avatar travisstaloch commented on June 22, 2024

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.

xpepermint avatar xpepermint commented on June 22, 2024

Just have multiple models.

from vue-rawmodel.

travisstaloch avatar travisstaloch commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.