How to require a field in a form using mongodb?


#1

Good day I create a form with a field of name and a field of score(number). I want to require the name field and make the score field optional only when i click the submit button. How can i do it ? Im using MongoDB. i tried using if else stement. but i also want to know how to set required field in MongoDB’s way. any advice and opinion to make the code efficient and better will be appreciated

here is the code
body.html

<template name="addPlayerForm">
	<form>
		<input type="text" name="playerName" placeholder="name">
		<input type="nubmer" name="setScore" placeholder="initial score">
		<input type="submit" value="Add Player">
	</form>
</template>

body.js

Template.addPlayerForm.events({
	'submit form': function(event){
		event.preventDefault();
		var playerNameVar = event.target.playerName.value;
		var playerScore = event.target.setScore.value;

		if(playerNameVar!=""){
			PlayersList.insert({
				name: playerNameVar,
				score: playerScore
			});
			event.target.playerName.value = "name";
			event.target.setScore.value = "score";
		}else{
			event.target.playerName.placeholder = "name is required";
		}
		
		
		
	}
});

#2

MongoDB doesn’t enforce a schema. If you want to do that, use the popular Collection2 package: