Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone.js validating model attributes
    primarykey
    data
    text
    <p>I've created a quite simple rails &amp; backbone app. Up to now validation was executed on rails side. Now i thought about implementing backbone validation. I'm doing this so: </p> <pre><code> createCampaign: (e) -&gt; _this= @ e.preventDefault() attributes = info: @getCountries() time_start: @$('#start').val() time_end: @$('#end').val() @collection.create attributes, wait: true success: -&gt; @$('#errors').text('Campaign created!').addClass('correct') @$('#create').removeAttr('disabled'); _this.clearFields('new_campaign') error: -&gt; @handleError </code></pre> <p>With <code>wait: true</code> nothing happens. if i comment it out success actions are taken. although i don't provide needed data on intent.<br> My model &amp; collection </p> <pre><code>class SvitlaTest.Models.Campaign extends Backbone.Model initialize: -&gt; @bind("error", @errorHandling) validate: (attrs) -&gt; return "Please fill start time of the campaign." unless attrs.time_start return "Please fill end time of the campaign." unless attrs.time_end "Please fill Countrz &amp; language fields." unless attrs.info errorHandling: (model, event) -&gt; @$('#create').removeAttr('disabled'); @$('#errors').html(error) class SvitlaTest.Collections.Campaigns extends Backbone.Collection url: '/api/campaigns' model: SvitlaTest.Models.Campaign </code></pre> <p><strong>Update 1</strong><br> my template jst.eco </p> <pre><code>&lt;form method="post" id="new_campaign" class="corners"&gt; &lt;p&gt; &lt;label for="start" &gt;Start time:&lt;/label&gt; &lt;input type="text" id="start" name="start" autofocus="" length='30' maxlength='30' /&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="end" &gt;End time:&lt;/label&gt; &lt;input type="text" id="end" name="end" length='30' maxlength='30' /&gt; &lt;/p&gt; &lt;div id='country_list'&gt; &lt;h4&gt;Info:&lt;/h4&gt; &lt;p class="country_element"&gt; Country &lt;input type="text" class='country' id="country" /&gt; Languages &lt;input type="text" class="languages" id="languages" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" id="create" value="Create" /&gt; &lt;/p&gt; &lt;/form&gt; </code></pre> <p>as of your comment: I'm using gems/backbone-on-rails-1.0.0.0<br> <strong>no information entered</strong><br> 1) with active <code>wait: true</code> when i run<br> I'm using chrome. and if i click submit button (triggers createCampaign) leaving fields empty <strong>nothing</strong> happens! I'm looking at console &amp; network tabs<br> 2) <code>wait: true</code> commented out: callback for success is run, then nothing happens<br> <strong>information entered</strong><br> With or without <code>wait: true</code> new model is created in DB </p> <p><strong>Update</strong><br> added in initialize of view: </p> <pre><code>initialize: -&gt; @collection.on('reset', @render,this) @collection.on('add', @appendCampaign ,this) @collection.on( "invalid", @handleInvalidState) handleInvalidState: (model, error) -&gt; console.log "validation" console.log model console.log error </code></pre> <p>if i comment out <code>wait: true</code> then i get this console output</p> <pre><code>validation Campaign {cid: "c2", attributes: Object, collection: Campaigns, _changing: false, _previousAttributes: Object…} Please fill start time of the campaign. </code></pre> <p>if i don't comment it out nothing happens... have no idea why this happens</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload