Checkboxes are slightly special as the user cannot define a value, but only check and uncheck the box. There are special props, trueValue
and falseValue
which allow you to determine what the value returned will be when the box is check or not checked respectfully. trueValue
will default to true
and falseValue
will default to false
.
import React from 'react';
import { AvForm, AvGroup, AvInput } from 'availity-reactstrap-validation';
import { Button, Label, FormGroup } from 'reactstrap';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {};
}
handleSubmit(event, errors, values) {
this.setState({errors, values});
}
render() {
return (
<div>
<AvForm onSubmit={this.handleSubmit}>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="checkbox" /> Check it Out
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="inOrOut" trueValue="Yes, I'm in!" falseValue="NOPE!" /> Are You In?
</Label>
</AvGroup>
<FormGroup>
<Button>Submit</Button>
</FormGroup>
</AvForm>
{this.state.values && <div>
<h5>Submission values</h5>
Invalid: {this.state.errors.join(', ')}<br />
Values: <pre>{JSON.stringify(this.state.values, null, 2)}</pre>
</div>}
</div>
);
}
}
Checking the boxes and submitting the form, you will see the value passed is the trueValue
for the checkbox; true
by default.
import React from 'react';
import { AvForm, AvGroup, AvInput } from 'availity-reactstrap-validation';
import { Button, Label, FormGroup } from 'reactstrap';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {};
}
handleSubmit(event, errors, values) {
this.setState({errors, values});
}
render() {
return (
<div>
<AvForm onSubmit={this.handleSubmit}>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="agree" trueValue="User Agreed" required /> Agree to this!
</Label>
</AvGroup>
<FormGroup>
<Button>Submit</Button>
</FormGroup>
</AvForm>
{this.state.values && <div>
<h5>Submission values</h5>
Invalid: {this.state.errors.join(', ')}<br />
Values: <pre>{JSON.stringify(this.state.values, null, 2)}</pre>
</div>}
</div>
);
}
}
Leaving the boxes unchecked and submitting the form, you will see the value passed is thefalseValue
for the checkbox; false
by default.
import React from 'react';
import { AvForm, AvGroup, AvInput } from 'availity-reactstrap-validation';
import { Button, Label, FormGroup } from 'reactstrap';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {};
}
handleSubmit(event, errors, values) {
this.setState({errors, values});
}
render() {
return (
<div>
<AvForm onSubmit={this.handleSubmit}>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="doNotAgree" falseValue="User Does Not Agreed" /> Agree to this!
</Label>
</AvGroup>
<FormGroup>
<Button>Submit</Button>
</FormGroup>
</AvForm>
{this.state.values && <div>
<h5>Submission values</h5>
Invalid: {this.state.errors.join(', ')}<br />
Values: <pre>{JSON.stringify(this.state.values, null, 2)}</pre>
</div>}
</div>
);
}
}
Using the model prop on the form, you can indicate if the checkboxes should be checked or unchecked when initialized by providing the trueValue or falseValue in the model prop on the form.
import React from 'react';
import { AvForm, AvGroup, AvInput } from 'availity-reactstrap-validation';
import { Button, Label, FormGroup } from 'reactstrap';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {};
}
handleSubmit(event, errors, values) {
this.setState({errors, values});
}
render() {
const model = {
checkbox1: true,
checkbox2: false,
checkbox3: false,
checkbox4: true,
checkbox5: 'yes',
checkbox6: 'yes',
checkbox7: 'no',
checkbox8: 'no',
checkbox9: 'other?',
};
return (
<div>
<AvForm onSubmit={this.handleSubmit} model={model}>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="checkbox1" /> true is "checked" (default)
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="checkbox2" /> false is "unchecked" (default)
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" trueValue={false} falseValue name="checkbox3" /> false can be "checked"
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" trueValue={false} falseValue name="checkbox4" /> true can be "unchecked"
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" trueValue="yes" name="checkbox5" /> make "yes" checked
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" falseValue="yes" name="checkbox6" /> make "yes" unchecked
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" trueValue="no" falseValue="yes" name="checkbox7" /> make "no" checked
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" trueValue="yes" falseValue="no" name="checkbox8" /> make "no" unchecked
</Label>
</AvGroup>
<AvGroup check>
<Label check>
<AvInput type="checkbox" name="checkbox9" /> any values that are not the trueValue is unchecked
</Label>
</AvGroup>
<FormGroup>
<Button>Submit</Button>
</FormGroup>
</AvForm>
{this.state.values && <div>
<h5>Submission values</h5>
Invalid: {this.state.errors.join(', ')}<br />
Values: <pre>{JSON.stringify(this.state.values, null, 2)}</pre>
</div>}
</div>
);
}
}