jonjamz / blaze-forms

Dead easy reactive forms with validation (Meteor).
https://atmospherejs.com/templates/forms
MIT License
113 stars 11 forks source link

Too much reactivity when passing in invalidCount to form content #17

Closed steph643 closed 9 years ago

steph643 commented 9 years ago

In the following code, {{log "refreshing"}} is called once, as expected:

<template name="myFormBlock">
  <form>
    {{> UI.contentBlock
        data=data
        schema=schema
        schemaContext=__schemaContext__
        setValidatedValue=__setValidatedValue__
        submit=__submit__
        submitted=__submitted__
        loading=__loading__
        failed=__failed__
        success=__success__
    }}
    <p>
      <button type="submit">Submit</button>
    </p>    
  </form>
</template>

<template name="myForm">

    {{#myFormBlock schema=schema action=action data=data}}      

      {{log "refreshing"}}

      {{> myInput field='testField1'}}
      {{> myInput field='testField2'}}
      {{> myInput field='testField3'}}

    {{/myFormBlock}}

</template>

But suppose you want to pass in invalidCount as an argument to UI.contentBlock (see the use case in discussion https://github.com/meteortemplates/forms/issues/15), like this:

<template name="myFormBlock">
  <form>
    {{> UI.contentBlock
        data=data
        schema=schema
        schemaContext=__schemaContext__
        setValidatedValue=__setValidatedValue__
        submit=__submit__
        submitted=__submitted__
        loading=__loading__
        failed=__failed__
        success=__success__
        invalidCount=invalidCount
    }}
    <p>
      <button type="submit">Submit</button>
    </p>    
  </form>
</template>

Then {{log "refreshing"}} is called 4 times (once + once per input).

jonjamz commented 9 years ago

This works properly with 1.8.0 but you must use:

{{#with context}}
  {{#if submitted}} ... {{/if}}
  {{#if invalidCount}} ... {{/if}}
{{/with}}

I could conceivably make all these individual helpers (so context doesn't have to be used). But this was the simplest option for now.

jonjamz commented 9 years ago

Code is here:

https://github.com/meteortemplates/forms/blob/master/lib/helpers.coffee