John
John

Reputation: 121

Redux Form sync validation but get constant rules from server

I have read the documentation how to use sync validation.

const validate = values => {
  const errors = {}

  ....

  if (!values.age) {
    errors.age = 'Required'
  } else if (isNaN(Number(values.age))) {
    errors.age = 'Must be a number'
  } else if (Number(values.age) < 18) {
    errors.age = 'Sorry, you must be at least 18 years old'
  }
  return errors
}

My need is I want the number '18' is retrieved from server? I need to setup all constants of the validation on server, and read it on page load only then used in this validation section.

Any help would be appreicated. thank you

Upvotes: 1

Views: 47

Answers (1)

relhomosapiens
relhomosapiens

Reputation: 11

I don't know if I understand the question correctly.
why don't you initiate validate method on page load after you fetch all constants from server.

validateConstants.js

export const validateConstants = fetch()

validation.js

import { validateConstants } from 'validateConstants'
const validate = values => {
  const errors = {}

  ....

  if (!values.age) {
    errors.age = 'Required'
  } else if (isNaN(Number(values.age))) {
    errors.age = 'Must be a number'
  } else if (Number(values.age) < validateConstants.age) {
    errors.age = 'Sorry, you must be at least 18 years old'
  }
  return errors
}

Upvotes: 1

Related Questions