Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formik - Use uppercase first letter field name in error messages

Is it possible to use uppercase field names in Formik error messages without having to define your own custom error message for every field? I'm using react native.

I have had to just name the Formik field values with capital letters but I would prefer not to do that.

formik enhancer code with uppercase first letter field names:

const formikEnhancer = withFormik({
  validationSchema: Yup.object().shape({
    Name: Yup.string().required(),
    Brand: Yup.string().required(),
    GroceryStore: Yup.object()
      .shape({
        city: Yup.string(),
        latitude: Yup.number(),
        longitude: Yup.number(),
        name: Yup.string(),
        place_id: Yup.string(),
        street: Yup.string(),
        street_number: Yup.string(),
        suburb: Yup.string()
      })
      .required(),
    Image: Yup.object().shape({
      uri: Yup.string(),
      name: Yup.string(),
      type: Yup.string()
    }),
    Categories: Yup.array()
      .min(1, 'Please select at least 1 Category')
      .required(),
    Description: Yup.string()
      .min(9)
      .required(),
    Price: Yup.string().matches(
      /^\d+(?:\.\d{2})$/,
      'Price must contain 2 decimal places (cents) e.g. 4.00'
    )
  }),
  isInitialValid: false,
  mapPropsToValues: () => ({
    Name: '',
    Brand: '',
    Description: '',
    Price: '',
    Categories: [],
    GroceryStore: {},
    Image: {}
  }),
  handleSubmit: (values, { props }) => {
    handleSubmit(values, props)
  },
  displayName: 'AddGroceryItemView'
})(AddGroceryItemView)
like image 275
BeniaminoBaggins Avatar asked Sep 12 '19 08:09

BeniaminoBaggins


1 Answers

Provide a label instead.

So for street: Yup.string(), you can write street: Yup.string().label('Street'). This will also help if you have more than one word like in street_number, so you need to write street_number: Yup.string().label('Street Number')

like image 171
Ajit Avatar answered Oct 11 '22 22:10

Ajit