home contents changes options help subscribe edit (external edit)

Here is an example of a plone/cmf member preference checkbox field with error handling, preservation of state and intelligent defaults. It should show the value found in request, if any (eg while correcting validation errors), otherwise it should show the value found in the member's properties, or a default if not found. This was hard to get right so I thought I'd write down a solution.:

          <div class="field"
               tal:define="error errors/no_email | nothing;"
               tal:attributes="class python:test(error, 'field error', 'field')"
               >
            <label for="no_email" i18n:translate="">
              I do not want to receive emails
            </label>
            <div class="formHelp" i18n:translate=""> 
              Check this box if you don't wish to receive occasional emails.
            </div>
            <div tal:content="error"></div>
            <input type="hidden" value="" name="no_email:default:boolean" />
            <input type="checkbox" value="1"
                   tal:define="prop string:no_email"
                   tal:attributes="name prop;
                                   checked python:
                                     ((prop in request.form and [request.form[prop],1]) 
                                     or [member.getProperty(prop,''),1])[0];
                                   tabindex tabindex/next;"
                   />
          </div>



subject:
  ( 34 subscribers )