Basic form example
==================
This example demonstrates basic usage of React Forms library.
.. raw:: html
Implementation
--------------
We start with a schema for our form:
.. jsx::
:hidesource:
var React = require('react')
var ReactForms = require('react-forms')
var Schema = ReactForms.schema.Schema
var Property = ReactForms.schema.Property
var Form = ReactForms.Form
.. jsx::
var schema = (
}
/>
}
validate={function(v) { return /.+\@.+\..+/.test(v) }}
/>
)
Note how we use ``required`` property on schema nodes to tell React Forms we
want the form to be valid only if values for those schema nodes are present.
We also provide a custom validator to ``email`` field.
Now we define a form component which wraps React Forms ``Form`` component and
provides a button to submit a form:
.. jsx::
var MyForm = React.createClass({
render: function() {
// render Form as and transfer all props to it
var form = this.transferPropsTo(
)
// return component with rendered form and a submit button
return (
)
},
onSubmit: function(e) {
e.preventDefault()
// check if form is valid
var validation = this.refs.form.value().validation
if (ReactForms.validation.isFailure(validation)) {
console.log('invalid form')
return
}
alert('form submitted!')
}
})
And finally we render ``MyForm`` into DOM:
.. jsx::
React.renderComponent(
,
document.getElementById('example'))