Server side validation redirects to preview of page instead of back to edit when validation returns False

Hello Xwiki users,

I’m calling some server side validation from a template file, the validation seems to be working but it redirects to the preview of the page instead of back to edit when validation returns False

Do I somehow need to tell xvalidate to redirect to the correct editor? The code works fine when placed in an AWM sheet but doesn’t seem to like being called from a template file.

This is how I include the validation in the template:

#set($mydoc = $xwiki.getDocument("Content Manager.Code.Form and Validation")) 
$mydoc.getRenderedContent()

Where “Content Manager.Code.Form and Validation” contains:

       <div class="xform">
            ## Iterate over the properties of the Content Manager Reduced Class
			#set($discard = $doc.use('Content Manager.Code.Content Manager Reduced Class'))
            #set($class = $doc.getObject('Content Manager.Code.Content Manager Reduced Class').xWikiClass)
            #foreach($prop in $class.properties)
                <b>$prop.prettyName</b>
                $doc.display($prop.getName())
            #end 
	    </div> 

    ## Invoke validation
    <input type="hidden" name="xvalidate" value="1" />
    ## Set the Groovy script which will be used for validation
    <input type="hidden" name="xvalidation" value="Content Manager.Code.Content Manager GroovyValidation" />

    #if(($xcontext.validationStatus.errors&&$xcontext.validationStatus.errors.size()>0)||($xcontext.validationStatus.exceptions&&$xcontext.validationStatus.exceptions.size()>0))
    <div class="validation-errors" style="border: 1px solid grey; padding: 10px;">
    This is a recap of all errors in this page (change the form to show errors only at the top or only next to the fields):

    #foreach($error in $xcontext.validationStatus.errors)
    <font color="red">$xwiki.parseMessage($error)</font><br />
    #end

    #foreach($exp in $xcontext.validationStatus.exceptions)
    <font color="red">$exp</font><br />
    #end
    </div>
    #end

Can anyone tell me what I’m doing wrong?

Thanks,
Ben

After some more investigating, it seems this is happening because I’m trying to call validation from WYSIWYG edit mode rather than Inline edit mode.

I assume I’m missing a call to something in the editWYSIWYG.vm

So I know what the fix needs to be, but I’m not sure how to implement it. Essentially xvalidate causes the editor to jump from WYSIWYG to INLINE.

If I hard code a #set($editor = 'wysiwyg') before the below bit of code in “edit.vm” this fixes the problem:

  #if("$!editor" == '')
    ## Normally no special characters should exist in the editor parameter, so this is just a protection measure.
    #set($editor = "$!{escapetool.xml($request.getParameter('editor'))}")
    #if($editor == '')
      ## Determine the sheets using the default document translation because objects are accessible only from the default translation.
      #if(!$services.sheet.getSheets($doc, $xcontext.action).isEmpty())
        #set($editor = 'inline')
      #elseif ($xwiki.editorPreference == 'wysiwyg' && $services.wysiwyg.isSyntaxSupported($tdoc.syntax.toIdString()))
        #set($editor = 'wysiwyg')
      #else
        #set($editor = 'wiki')
      #end
    #end
  #end
  #if($editor == 'inline')
    ## The inline editor uses the view layout, unlike the rest of the editors.
    #template('startpage.vm')
    #template('editinline.vm')
    #template('endpage.vm')
  #else

What is the correct way to set the editor back to “WYSIWYG” after it gets changed by xvalidate without hardcoding it?