N
N
Ninja Mate2016-01-24 10:07:30
JavaScript
Ninja Mate, 2016-01-24 10:07:30

How to validate checkbox in ReactJS form?

The task is to make the submit button inactive if the agree checkbox is not pressed.
But I don't understand how to get the value from the checkbox...

var MyContactForm = React.createClass({

    getInitialState() {
        return {
            disabled: true
        };
    },

    resetValidation() {
        this.setState({
            disabled: true
        });
    },

    validationState() {
        var checked = this.refs.input.checked(); //Пытаюсь проверить таким образом, но безуспешно, документации не обнаружил
    },

    handleChange() {
        this.setState( this.validationState() );
    },

   render: function() {
        return (
            <form>
                <Input type="text" label="Name" placeholder="Your Name" bsSize="large" />
                <Input type="email" label="Email" placeholder="Your Email" bsSize="large"/>
                <Input type="textarea" label="Massage" placeholder="Please tell me more" bsSize="large"/>

                <RulesAndConditions>Rules and Conditions</RulesAndConditions>
                <Input type="checkbox" label="Confirm" readOnly onChange={this.handleChange()} bsSize="large"/>

                <ButtonGroup vertical block>
                    <Button onClick={this.resetValidation} type="reset" bsStyle="warning" bsSize="large">Reset</Button>
                    <p/>
                    <Button type="submit" bsStyle="success" bsSize="large" disabled={this.state.disabled}>Submit</Button>
                    <p/>
                    <Button onClick={this.props.onHide} bsStyle="danger" bsSize="large">Close</Button>
                </ButtonGroup>
            </form>
        )
    }
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2016-01-24
@victorzadorozhnyy

I tried to check in this way, but to no avail, I did not find any documentation

Where did you look for her? Refs to Components
Do you want to hang the result of the method call on onChange?
What state do you want to put here?
If you want through refs, then first make a binding and then get the value from the DOM property of the element
, for example like this
<input ref="myCheckbox" ...

this.refs.myCheckbox.checked

Example 1
Example 2
Or use event in the handleChange method
handleChange(event) {
  var checked = event.target.checked;
}

Example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question