0

I'm creating a form using Sails.js. The form has multiple pages, and each time the user navigates to the next page, the data they've inputted to the form for the current page is submitted to a MySQL database.

On one page of the form, there are two radio buttons, one with a value 'Yes' and one with a value of 'No'. The value of whichever button is checked is submitted to the database, to the 'author1Corresponding' column. From my understanding of radio buttons, if one is checked, the specified value, i.e. 'Yes' or 'No' should be submitted to the database. However, when either one is checked, author1Corresponding gets the value 'false'. If neither button is checked, author1Corresponding gets the value 'null'.

Here is my code in the html form for the two buttons:

<label for="articleSubmission[author1Corresponding]">Is this Author the Corresponding Author?</label><br>

  <input type="radio" class="radio" name="articleSubmission[author1Corresponding]" value="Yes"> Yes<br><br>

  <input type="radio" class="radio" name="articleSubmission[author1Corresponding]" value="No"> No

The class radio is just being used to style both buttons as having display: inline.

Why isn't the defined value being submitted to the database, and how can I fix this?

tedleahy
  • 69
  • 5
  • How exactly is the data sent, with a regular form, if so did you check the GET or POST data to see what is actually sent to the server ? – adeneo Nov 27 '14 at 11:49
  • agree with adeneo, console out your params console.log(req.allParams()) and check the value going to your db – Meeker Dec 01 '14 at 22:06
  • @Meeker and adeneo, The value logged out when I console.log(req.allParams()) is "Yes" if the first button is clicked, and "No" if the second button is clicked. Viewing the database, the value shows as '0' no matter which is clicked. – tedleahy Dec 17 '14 at 11:58
  • I would include in the question your model definition. – Meeker Dec 17 '14 at 15:13
  • Why would you be using a radio input for boolean values? That's what checkboxes are for. – idbehold Jan 30 '15 at 20:00

1 Answers1

1

I found the solution! So author1Corresponding had a type of boolean (defined in the ArticleSubmission model), as the value could only ever be true or false.

Rather than the values being set to "yes" and "no", they had to be set to "true" and "false" instead. Once this was done, they were passed to the database as a 1 if "Yes" had been clicked, and a 0 if "No" had been clicked.

I think it's to do with how Waterline handles booleans.

tedleahy
  • 69
  • 5
  • Don't know why this answer was marked down. It would be nice if people would leave comments. – Meeker Dec 17 '14 at 16:13