Search code examples
embedded-javascript

Node.js - Boolean (Checkbox) Field in Embedded Javascript


I've a boolean field in the model just similar to the below one. And I want to create a view using embedded javascript.   
**settings.js**

module.exports = {

   attributes: {
       myField:{
          type:'boolean',
          defaultsTo:'false'
       }
   }
};

This is the code that I'm using

 <form action="/settings/create" method="POST" id="sign-up-form" class="form-inline">

     <div class="form-group">
     <label for="field1">my field label</label>
      <input type="checkbox" class="form-control" id="field1" name="myField">
    </div>

    <input type="submit" class="btn btn-primary" value="Create"/>
    <input type="hidden" name="_csrf" value="<%= _csrf %>" />
</form>

So the problems I'm facing are

  1. When I create the record, I can see the boolean is set to ON and OFF in the json and why this is not true or false?
  2. Also how to render the true or false in the view using embedded javascript

Solution

  •   <div class="form-group">
        <label for="confirmationBox">Field Label</label>
        <% if(object.fieldName==true || object.fieldName=="true"){ %>
        <input type="checkbox" id="confirmationBox" name="confirmationBox" checked>
        <% } else { %>
        <input type="checkbox" id="confirmationBox" name="confirmationBox">
        <% } %>
      </div>