ManuelDeLeon / viewmodel-react

Create your React components with view models.
MIT License
24 stars 3 forks source link

How to specify default value along with validation #41

Open antoninadert opened 6 years ago

antoninadert commented 6 years ago

Hello Manuel,

Looking at the doc for validation: https://viewmodel.org/#AdvancedValidation it seems that it's not possible to define both default value and validation property simultaneously.

Is the best approach to just specify defaults in the created() lifecycle when used along with validation?

Maybe it's worth showing an example in the doc for new comers...

davidsavoie1 commented 6 years ago

@antoninadert You actually can set both validation and default value to a prop:

{
  name: ViewModel.property.string.notBlank.default('Antonin'),
}

Is this what you mean?

antoninadert commented 6 years ago

I missed it in the doc or it was not there but this is very neat!

davidsavoie1 commented 6 years ago

In fact, all the properties listed in Must know, Should know, etc. can be combined in any fashion and, as far as I know, in any order you want. And event though afterUpdate is in the If you're insane category, I've found it pretty useful to trigger some functions specific to a prop state without resorting to autorun. The title kept me away from this function for some time, eheh! 😉