Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
Commits on May 14, 2012
@uris77 uris77 Add renderErrorsAsMap to Components
In a Composer, it is now possible to do:
self.renderErrorsAsMap(bean:map)
This does the same as renderErrors, but instead of taking domain class,
it takes a list of maps. The map is structured as follows:
[field: field, error: error].
This is useful whenever one is using an api that returns json , or a
map.
91c6d06
Commits on May 15, 2012
@uris77 uris77 Typo in comment
b6e9193
@uris77 uris77 Renderer Util provides renderMapAsErrors
ComponentErrorRendererUtil dynamically adds renderMapAsErrors. This
allows the input elements of a form to display errors from a map.

Example usage:
I prefer to extract all errors from a domain class into a map. In
Bootstrap I add the following code:

grailsApplication.domainClasses.each{domainClass->
   domainClass.metaClass.retrieveErrorsAsMap = {
      def errors = []
      for(error in delegate?.errors?.fieldErrors){
         def _domain = [
            field: error.field,
            error: messageSource.getMessage(error,null)
            ]
         errors.add(_domain)
      }
   return errors
   }
}

This then allows me retrieve a list of map of errors :

def domainInstance = new Domain()
domainInstance.validate()
def errors = domainInstance.retrieveErrorsAsMap()

And in my composer I can have these errors rendered in their
corresponding input elements as follows:

def create(){
   def domainInstance = new Domain()
   domainInstance.validate()
   def errors = domainInstance.retrieveErrorsAsMap()

   if(errors){
      self.renderMapAsErrors(bean: errors)
   }else{
      flash.message = "Success!"
   }

}

In the example above, you can not see the benefit of something like
this, but if we encapsulate all domain transactions in services, or if
we are working with JSON obtained through some ajax request or a web
service, then this becomes handy.

An example using a service:

class MyService{
   def create(params){
      def instance = [:]
      def _instance = new Domain(params)

      _instance.validate()
      if(_instance.hasErrors()){
         instance.errors = _instance.retrieveErrorsAsMap()
      }else{
         _instance.save()
         instance = _instance.properties
      }

      return instance
   }
}

In the Composer:

void onClick_saveSomething(Event e){
   def instance = myService.create(self.params)
   if(instance.errors){
      self.renderMapAsErrors(bean: instance)
   }else{
      flash.message = 'Success!'
   }
}
a9688ff
Commits on Jun 12, 2012
@xiaochong Merge pull request #63 from uris77/render_map_as_errors
Render map as errors
a3274c1
View
7 ZkuiGrailsPlugin.groovy
@@ -10,6 +10,7 @@ import org.grails.plugins.zkui.artefacts.composer.ComposerArtefactHandler
import org.grails.plugins.zkui.artefacts.vm.ViewModelArtefactHandler
import org.grails.plugins.zkui.metaclass.RedirectDynamicMethod
import org.grails.plugins.zkui.util.UriUtil
+import org.grails.plugins.zkui.util.ComponentErrorRendererUtil
import org.zkoss.zk.ui.Component
import org.zkoss.zk.ui.Executions
import org.zkoss.zk.ui.Page
@@ -235,6 +236,10 @@ The different is it more likely to use the Grails' infrastructures such as gsp,
}
}
+ ComponentErrorRendererUtil errorRendererUtil = new ComponentErrorRendererUtil()
+
+ errorRendererUtil.addRenderMapAsErrors()
+
org.zkoss.zk.ui.Session.metaClass.getAt = { String name ->
delegate.getAttribute(name)
}
@@ -359,4 +364,4 @@ The different is it more likely to use the Grails' infrastructures such as gsp,
// TODO Implement code that is executed when the project configuration changes.
// The event is the same as for 'onChange'.
}
-}
+}
View
2  application.properties
@@ -1,5 +1,5 @@
#Grails Metadata file
-#Thu May 10 09:44:42 CST 2012
+#Tue May 15 08:48:49 CST 2012
app.grails.version=2.0.3
app.name=zkui
plugins.rest-client-builder=1.0.2
View
21 src/groovy/org/grails/plugins/zkui/util/ComponentErrorRendererUtil.groovy
@@ -0,0 +1,21 @@
+package org.grails.plugins.zkui.util
+
+import org.zkoss.zul.impl.InputElement
+import org.zkoss.zk.ui.Component
+
+class ComponentErrorRendererUtil{
+
+ public void addRenderMapAsErrors(){
+ org.zkoss.zk.ui.Component.metaClass.renderMapAsErrors = {Map args->
+ for(_error in args.bean.errors){
+ def name = _error.field
+
+ def selectedComponentList = delegate.select("[name='${name}']")
+ if(selectedComponentList.size() > 0 && selectedComponentList[0] instanceof InputElement){
+ selectedComponentList[0].setErrorMessage(_error.error)
+ }
+ }
+
+ }
+ }
+}
View
38 test/unit/org/grails/plugins/zkui/util/ComponentErrorRendererUtilTests.groovy
@@ -0,0 +1,38 @@
+package org.grails.plugins.zkui.util
+
+import static org.junit.Assert.*
+import org.zkoss.zul.impl.InputElement
+import org.zkoss.zk.ui.Component
+import org.zkoss.zul.Textbox
+import org.zkoss.zul.Window
+import grails.test.*
+
+class ComponentErrorRendererUtilTests extends ComposerUnitTestCase{
+
+ Window component
+ Textbox txt1
+
+ @Before
+ void setUp(){
+ Window.metaClass.select = { [txt1] }
+ component = new Window()
+ txt1 = new Textbox()
+ txt1.setName('name')
+ component.appendChild(txt1)
+ }
+
+ @Test
+ void should_add_renderMapAsErrors_to_InputElement(){
+ ComponentErrorRendererUtil util = new ComponentErrorRendererUtil()
+ util.addRenderMapAsErrors()
+
+ def params = [errors:[[field: 'name', error: 'Name can not be empty']]]
+
+ component.renderMapAsErrors(bean: params)
+
+ assertEquals 'Name can not be empty', txt1.getErrorMessage()
+
+
+ }
+
+}

No commit comments for this range

Something went wrong with that request. Please try again.