Skip to content
Gravity Logo / Docs Gravity home
Browse documentation
Documentation/Gravity Native

Form

Forms are simple and require almost no work to set up. Simply create an object with the form fields you need and pass it to the <Form> component. You can render any input type you need – from text boxes to pickers and sliders.

Code

 <Form 
  data={ profileFields }
  url='/api/user'
  method='PATCH'
  buttonText='Save' 
  callback={ this.onSuccess }
/>

Props

PropDescriptionRequiredValue
buttonTextsubmit button labeloptionalstring
callbackexecuted after a successful submitoptionalfunction
dataform object (see below)requiredobject
methodHTTP request typeoptionalstring
stylecustom styleoptionalstyle object
submitOnChangesubmit the form on each input changeoptionalboolean
urlurl to send the form tooptionalstring

Form Object

When constructing a form object, the outer key is the input name which is assigned an object with any combination of input props.

formData = {
  name: {
    label: 'Your name',
    type: 'text',
    required: true,
    placeholder: 'Jon Smith',
    errorMessage: 'Please enter your name',
  },
  email: {
    label: 'Email address',
    type: 'email',
    required: true,
  },
 age: {
    label: 'Age',
    type: 'number',
    min: 18,
    max: 65,
  },
  dob: {
    label: 'Date of birth',
    type: 'date',
  },
  hungry: {
    label: 'Feeling hungry?',
    type: 'switch',
   },
  happiness: {
    label: 'Happiness level',
    type: 'slider',
  },
  phone: {
    label: 'Phone Number',
    type: 'phone',
    required: false
  },
  website: {
    label: 'Website',
    type: 'url',
    required: false,
  },
  gender: {
    label: 'Gender',
    type: 'picker',
    options: ['male', 'female'],
    required: true,
  }
}

Input Props

PropDescriptionRequiredValue
defaultdefault valueoptionalstring
errorMessageoverrides the default error messageoptionalstring
labelinput labeloptionalstring
maxmaximum value on number inputoptionalinteger
minminimum value on number inputoptionalinteger
nameinput namerequiredstring
optionsarray of strings for a pickerrequiredarray
placeholderplaceholder textoptionalstring
requireddetermines if a value is requiredoptionalboolean
stepincrement step amount for slideroptionalnumber
typeinput typerequiredtext, email, password, number, url, phone, hidden, picker, date, time, switch, slider
valueinitial valueoptionalstring

Form Submission

When your form is submitted to the server, Gravity will optimise the request and only send the name/value pairs, so on the server you can access a value using:

req.body.email

Form Errors

To show an error on a specific form input, throw an error on the server with an inputError key and the input name.

throw ({ inputError: 'email', message: 'You\'re already registered' });