Need help with field validation in custom Save button

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Need help with field validation in custom Save button

jasenka
Hi,

I am new to XForms development and need help on following:


I have created custom "Save" button which is supposed to validate all the required fields on the form, and only if values are correct (several fields and attachment, all have constraint),  then set field value (my-field + 1) and save the form. If validation did not pass, then only refresh the error summary, but do not increment the value in the field.


I've tried solution:

  <fr:button id="control-2-control" bind="control-2-bind">
        <xforms:label ref="$form-resources/control-2/label"/>
        <xforms:hint ref="$form-resources/control-2/hint"/>
        <xforms:help ref="$form-resources/control-2/help"/>
        <xforms:alert ref="$fr-resources/detail/labels/alert"/>

        <xforms:action ev:event="DOMActivate">
           <xforms:setvalue ref="./my-field" value=". + 1"/>
           <xforms:dispatch name="fr-save-action" target="fr-persistence-model">
               <xxforms:context name="fr:check-data-valid" select="true()"/>
           </xforms:dispatch>
        </xforms:action>
 </fr:button>

but the field value gets incremented in any case, which I only want to happen if the validation OK.


I've tried something like this, but still didn't get it working:


               <fr:button id="control-2-control" bind="control-2-bind">
                   <xforms:label ref="$form-resources/control-2/label"/>
                   <xforms:hint ref="$form-resources/control-2/hint"/>
                   <xforms:help ref="$form-resources/control-2/help"/>
                   <xforms:alert ref="$fr-resources/detail/labels/alert"/>

                                                                       
                    <xforms:action>
                         <xxforms:variable name="continue-with-save"
                            select="xxforms:instance('fr-form-valid-instance') = 'true'"
                             as="xs:boolean"/>

                        <xforms:action if="not($continue-with-save)">
                               
                        </xforms:action>
               
                       
                        <xforms:action if="($continue-with-save)">                       
                                 
                            <xforms:action ev:event="DOMActivate">
                                <xforms:setvalue ref="./my-field" value=". + 1"/>
                                 <xforms:dispatch name="fr-save-action" target="fr-persistence-model"/>                   
                                 </xforms:action>
                             </xforms:action>
                      </xforms:action>                     
               </fr:button>

Please let me know what would be correct way to use <xforms:if()> structure in the code, and is it possible to put logic inside it without creating variables?
What is the best reference site to learn about this?

Best regards,

Jasenka