From 6c926094c3c0ee5ea1b1b5d0811fe65e613003d9 Mon Sep 17 00:00:00 2001 From: Michael Randers-Pehrson Date: Thu, 21 Aug 2014 23:00:05 -0400 Subject: [PATCH 1/2] Including missing validation check The validation check was removed from the update to the CommentForm handleSubmit function. --- docs/docs/tutorial.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/docs/tutorial.md b/docs/docs/tutorial.md index 6b771aebab..882d2ad412 100644 --- a/docs/docs/tutorial.md +++ b/docs/docs/tutorial.md @@ -565,6 +565,9 @@ var CommentForm = React.createClass({ handleSubmit: function() { var author = this.refs.author.getDOMNode().value.trim(); var text = this.refs.text.getDOMNode().value.trim(); + if (!text || !author) { + return false; + } this.props.onCommentSubmit({author: author, text: text}); this.refs.author.getDOMNode().value = ''; this.refs.text.getDOMNode().value = ''; From c999785fa0721af549e3d00036714d27fff94158 Mon Sep 17 00:00:00 2001 From: Michael Randers-Pehrson Date: Fri, 22 Aug 2014 08:27:23 -0400 Subject: [PATCH 2/2] Adding `e.preventDefault()` to `handleSubmit`, Also added plain `return`, updated text. --- docs/docs/tutorial.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/docs/tutorial.md b/docs/docs/tutorial.md index 882d2ad412..922c2a2a9a 100644 --- a/docs/docs/tutorial.md +++ b/docs/docs/tutorial.md @@ -481,16 +481,17 @@ Let's make the form interactive. When the user submits the form, we should clear ```javascript{3-13,16-18} // tutorial16.js var CommentForm = React.createClass({ - handleSubmit: function() { + handleSubmit: function(e) { + e.preventDefault(); var author = this.refs.author.getDOMNode().value.trim(); var text = this.refs.text.getDOMNode().value.trim(); if (!text || !author) { - return false; + return; } // TODO: send request to the server this.refs.author.getDOMNode().value = ''; this.refs.text.getDOMNode().value = ''; - return false; + return; }, render: function() { return ( @@ -508,7 +509,7 @@ var CommentForm = React.createClass({ React attaches event handlers to components using a camelCase naming convention. We attach an `onSubmit` handler to the form that clears the form fields when the form is submitted with valid input. -We always return `false` from the event handler to prevent the browser's default action of submitting the form. (If you prefer, you can instead take the event as an argument and call `preventDefault()` on it.) +Call `preventDefault()` on the event to prevent the browser's default action of submitting the form. ##### Refs @@ -562,16 +563,17 @@ Let's call the callback from the `CommentForm` when the user submits the form: ```javascript{6} // tutorial18.js var CommentForm = React.createClass({ - handleSubmit: function() { + handleSubmit: function(e) { + e.preventDefault(); var author = this.refs.author.getDOMNode().value.trim(); var text = this.refs.text.getDOMNode().value.trim(); if (!text || !author) { - return false; + return; } this.props.onCommentSubmit({author: author, text: text}); this.refs.author.getDOMNode().value = ''; this.refs.text.getDOMNode().value = ''; - return false; + return; }, render: function() { return (