Skip to content

Commit bbd1b33

Browse files
committed
Correct formatting
1 parent 5a6d0a4 commit bbd1b33

File tree

1 file changed

+46
-58
lines changed

1 file changed

+46
-58
lines changed

grails-plugin-gsp/src/main/groovy/org/grails/plugins/web/taglib/ValidationTagLib.groovy

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,18 @@ class ValidationTagLib implements TagLibrary {
9292

9393
def tagSyntaxCall = (attrs instanceof GroovyPageAttributes) ? attrs.isGspTagSyntaxCall() : false
9494

95-
def rejectedValue = null
95+
def rejectedValue = null
9696
if (bean.metaClass.hasProperty(bean, 'errors')) {
9797
Errors errors = bean.errors
9898
rejectedValue = errors?.getFieldError(field)?.rejectedValue
9999
if (rejectedValue == null) {
100100
rejectedValue = parseForRejectedValue(bean, field)
101101
}
102-
103-
}
104-
else {
102+
103+
} else {
105104
rejectedValue = parseForRejectedValue(bean, field)
106105
}
107-
if (rejectedValue != null) {
106+
if (rejectedValue != null) {
108107
out << formatValue(rejectedValue, field, tagSyntaxCall)
109108
}
110109
}
@@ -124,21 +123,18 @@ class ValidationTagLib implements TagLibrary {
124123
if (attrs.bean) {
125124
checkList << attrs.bean
126125
}
127-
}
128-
else if (attrs.containsKey('model')) {
126+
} else if (attrs.containsKey('model')) {
129127
if (model) {
130-
checkList = model.findAll {it.value?.errors instanceof Errors}.collect {it.value}
128+
checkList = model.findAll { it.value?.errors instanceof Errors }.collect { it.value }
131129
}
132-
}
133-
else {
130+
} else {
134131
for (attributeName in request.attributeNames) {
135132
def ra = request[attributeName]
136133
if (ra) {
137134
def mc = GroovySystem.metaClassRegistry.getMetaClass(ra.getClass())
138135
if (ra instanceof Errors && !checkList.contains(ra)) {
139136
checkList << ra
140-
}
141-
else if (mc.hasProperty(ra, 'errors') && ra.errors instanceof Errors && !checkList.contains(ra.errors)) {
137+
} else if (mc.hasProperty(ra, 'errors') && ra.errors instanceof Errors && !checkList.contains(ra.errors)) {
142138
checkList << ra.errors
143139
}
144140
}
@@ -151,8 +147,7 @@ class ValidationTagLib implements TagLibrary {
151147
def errors = null
152148
if (i instanceof Errors) {
153149
errors = i
154-
}
155-
else {
150+
} else {
156151
def mc = GroovySystem.metaClassRegistry.getMetaClass(i.getClass())
157152
if (mc.hasProperty(i, 'errors')) {
158153
errors = i.errors
@@ -211,18 +206,16 @@ class ValidationTagLib implements TagLibrary {
211206
if (errors.hasFieldErrors(field)) {
212207
errorList += errors.getFieldErrors(field)
213208
}
214-
}
215-
else {
209+
} else {
216210
errorList += errors.allErrors
217211
}
218212
}
219213

220214
for (error in errorList) {
221215
def result
222216
if (var) {
223-
result = body([(var):error])
224-
}
225-
else {
217+
result = body([(var): error])
218+
} else {
226219
result = body(error)
227220
}
228221
if (outputResult) {
@@ -253,18 +246,17 @@ class ValidationTagLib implements TagLibrary {
253246
if (errorsList) {
254247
out << "<ul>"
255248
out << eachErrorInternalForList(attrs, errorsList, {
256-
out << "<li>${message(error:it, encodeAs:codec)}</li>"
249+
out << "<li>${message(error: it, encodeAs: codec)}</li>"
257250
})
258251
out << "</ul>"
259252
}
260-
}
261-
else if (renderAs.equalsIgnoreCase("xml")) {
253+
} else if (renderAs.equalsIgnoreCase("xml")) {
262254
def mkp = new MarkupBuilder(out)
263255
mkp.errors() {
264256
eachErrorInternal(attrs, {
265257
error(object: it.objectName,
266-
field: it.field,
267-
message: message(error:it)?.toString(),
258+
field: it.field,
259+
message: message(error: it)?.toString(),
268260
'rejected-value': StringEscapeUtils.escapeXml(it.rejectedValue))
269261
})
270262
}
@@ -297,7 +289,7 @@ class ValidationTagLib implements TagLibrary {
297289
Object error = attrs.error ?: attrs.message
298290
if (error) {
299291
if (!attrs.encodeAs && error instanceof MessageSourceResolvable) {
300-
MessageSourceResolvable errorResolvable = (MessageSourceResolvable)error
292+
MessageSourceResolvable errorResolvable = (MessageSourceResolvable) error
301293
if (errorResolvable.arguments) {
302294
error = new DefaultMessageSourceResolvable(errorResolvable.codes, encodeArgsIfRequired(errorResolvable.arguments) as Object[], errorResolvable.defaultMessage)
303295
}
@@ -311,14 +303,12 @@ class ValidationTagLib implements TagLibrary {
311303
}
312304
catch (NoSuchMessageException e) {
313305
if (error instanceof MessageSourceResolvable) {
314-
text = ((MessageSourceResolvable)error).codes[0]
315-
}
316-
else {
306+
text = ((MessageSourceResolvable) error).codes[0]
307+
} else {
317308
text = error?.toString()
318309
}
319310
}
320-
}
321-
else if (attrs.code) {
311+
} else if (attrs.code) {
322312
String code = attrs.code?.toString()
323313
List args = []
324314
if (attrs.args) {
@@ -332,17 +322,16 @@ class ValidationTagLib implements TagLibrary {
332322
}
333323

334324
def message = messageSource.getMessage(code, args == null ? null : args.toArray(),
335-
defaultMessage, locale)
325+
defaultMessage, locale)
336326
if (message != null) {
337327
text = message
338-
}
339-
else {
328+
} else {
340329
text = defaultMessage
341330
}
342331
}
343332
if (text) {
344333
Encoder encoder = codecLookup.lookupEncoder(attrs.encodeAs?.toString() ?: 'raw')
345-
return encoder ? encoder.encode(text) : text
334+
return encoder ? encoder.encode(text) : text
346335
}
347336
''
348337
}
@@ -360,16 +349,16 @@ class ValidationTagLib implements TagLibrary {
360349
}
361350

362351
// Maps out how Grails contraints map to Apache commons validators
363-
static CONSTRAINT_TYPE_MAP = [email : 'email',
364-
creditCard : 'creditCard',
365-
matches : 'mask',
366-
blank: 'required',
367-
nullable: 'required',
368-
maxSize: 'maxLength',
369-
minSize: 'minLength',
370-
range: 'intRange',
371-
size: 'intRange',
372-
length: 'maxLength,minLength']
352+
static CONSTRAINT_TYPE_MAP = [email : 'email',
353+
creditCard: 'creditCard',
354+
matches : 'mask',
355+
blank : 'required',
356+
nullable : 'required',
357+
maxSize : 'maxLength',
358+
minSize : 'minLength',
359+
range : 'intRange',
360+
size : 'intRange',
361+
length : 'maxLength,minLength']
373362

374363
/**
375364
* Validates a form using Apache commons validator javascript against constraints defined in a Grails
@@ -386,15 +375,15 @@ class ValidationTagLib implements TagLibrary {
386375
throwTagError("Tag [validate] is missing required attribute [form]")
387376
}
388377

389-
def againstClass = attrs.against ?: form.substring(0,1).toUpperCase() + form.substring(1)
378+
def againstClass = attrs.against ?: form.substring(0, 1).toUpperCase() + form.substring(1)
390379
def dc = grailsAttributes.grailsApplication.getDomainClass(againstClass)
391380
if (!dc) {
392381
throwTagError("Tag [validate] could not find a domain class to validate against for name [${againstClass}]")
393382
}
394383

395384
def appliedConstraints = []
396385
dc.constrainedProperties.values().each {
397-
appliedConstraints += it.collect{ it.appliedConstraints }
386+
appliedConstraints += it.collect { it.appliedConstraints }
398387
}
399388

400389
appliedConstraints = appliedConstraints.flatten()
@@ -404,15 +393,14 @@ class ValidationTagLib implements TagLibrary {
404393
if (validateType) {
405394
if (fieldValidations[validateType]) {
406395
fieldValidations[validateType] << it
407-
}
408-
else {
409-
fieldValidations[validateType] = [it]
396+
} else {
397+
fieldValidations[validateType] = [it]
410398
}
411399
}
412400
}
413401

414402
out << '<script type="text/javascript">\n'
415-
fieldValidations.each { k,v ->
403+
fieldValidations.each { k, v ->
416404
def validateType = k
417405
if (validateType) {
418406
def validateTypes = [validateType]
@@ -422,7 +410,7 @@ class ValidationTagLib implements TagLibrary {
422410

423411
for (vt in validateTypes) {
424412
// import required script
425-
def scriptName = "org/apache/commons/validator/javascript/validate" + vt.substring(0,1).toUpperCase() + vt.substring(1) + ".js"
413+
def scriptName = "org/apache/commons/validator/javascript/validate" + vt.substring(0, 1).toUpperCase() + vt.substring(1) + ".js"
426414
def inStream = getClass().classLoader.getResourceAsStream(scriptName)
427415
if (inStream) {
428416
out << inStream.getText('UTF-8')
@@ -434,11 +422,11 @@ class ValidationTagLib implements TagLibrary {
434422
out << "document.forms['${form}'].elements['${constraint.propertyName}']," // the field
435423
out << '"Test message"' // TODO: Resolve the actual message
436424
switch (vt) {
437-
case 'mask': out << ",function() { return '${constraint.regex}'; }";break
438-
case 'intRange': out << ",function() { if (arguments[0]=='min') return ${constraint.range.from}; else return ${constraint.range.to} }";break
439-
case 'floatRange': out << ",function() { if (arguments[0]=='min') return ${constraint.range.from}; else return ${constraint.range.to} }";break
440-
case 'maxLength': out << ",function() { return ${constraint.maxSize}; }";break
441-
case 'minLength': out << ",function() { return ${constraint.minSize}; }";break
425+
case 'mask': out << ",function() { return '${constraint.regex}'; }"; break
426+
case 'intRange': out << ",function() { if (arguments[0]=='min') return ${constraint.range.from}; else return ${constraint.range.to} }"; break
427+
case 'floatRange': out << ",function() { if (arguments[0]=='min') return ${constraint.range.from}; else return ${constraint.range.to} }"; break
428+
case 'maxLength': out << ",function() { return ${constraint.maxSize}; }"; break
429+
case 'minLength': out << ",function() { return ${constraint.minSize}; }"; break
442430
}
443431
out << ');\n'
444432
}
@@ -447,8 +435,8 @@ class ValidationTagLib implements TagLibrary {
447435
}
448436
}
449437
out << 'function validateForm(form) {\n'
450-
fieldValidations.each { k,v ->
451-
def validateType = k.substring(0,1).toUpperCase() + k.substring(1)
438+
fieldValidations.each { k, v ->
439+
def validateType = k.substring(0, 1).toUpperCase() + k.substring(1)
452440
out << "if (!validate${validateType}(form)) return false;\n"
453441
}
454442
out << 'return true;\n'

0 commit comments

Comments
 (0)