-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added javascript (AJAX) validation of forms in preparation for regist…
…ration process
- Loading branch information
Showing
12 changed files
with
539 additions
and
9 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/annotation/org/springframework/http/annotations.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
#%L | ||
fixture | ||
%% | ||
Copyright (C) 2013 Martin Lau | ||
%% | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
#L% | ||
--> | ||
<root> | ||
<item name='org.springframework.http.HttpEntity org.springframework.http.HttpHeaders getHeaders()'> | ||
<annotation name='org.jetbrains.annotations.NotNull'/> | ||
</item> | ||
</root> |
38 changes: 38 additions & 0 deletions
38
src/main/annotation/org/springframework/validation/annotations.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
#%L | ||
fixture | ||
%% | ||
Copyright (C) 2013 Martin Lau | ||
%% | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
#L% | ||
--> | ||
<root> | ||
<item name="org.springframework.validation.Errors java.util.List<org.springframework.validation.FieldError> getFieldErrors()"> | ||
<annotation name="org.jetbrains.annotations.NotNull"/> | ||
</item> | ||
<item name="org.springframework.validation.Errors java.util.List<org.springframework.validation.ObjectError> getAllErrors()"> | ||
<annotation name="org.jetbrains.annotations.NotNull"/> | ||
<annotation name="jet.runtime.typeinfo.KotlinSignature"> | ||
<val name="value" val=""fun getAllErrors(): List<ObjectError>""/> | ||
</annotation> | ||
</item> | ||
<item name="org.springframework.validation.FieldError java.lang.String getField()"> | ||
<annotation name="org.jetbrains.annotations.NotNull"/> | ||
</item> | ||
<item name="org.springframework.validation.ObjectError java.lang.String getObjectName()"> | ||
<annotation name="org.jetbrains.annotations.NotNull"/> | ||
</item> | ||
</root> | ||
|
25 changes: 25 additions & 0 deletions
25
src/main/annotation/org/springframework/web/bind/annotations.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
#%L | ||
fixture | ||
%% | ||
Copyright (C) 2013 Martin Lau | ||
%% | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
#L% | ||
--> | ||
<root> | ||
<item name='org.springframework.web.bind.MethodArgumentNotValidException org.springframework.validation.BindingResult getBindingResult()'> | ||
<annotation name='org.jetbrains.annotations.NotNull'/> | ||
</item> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/kotlin/io/fixture/controller/ValidationController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* #%L | ||
* fixture | ||
* %% | ||
* Copyright (C) 2013 Martin Lau | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
package io.fixture.controller | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.MethodArgumentNotValidException | ||
import java.util.HashMap | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import javax.validation.Valid | ||
import org.springframework.web.bind.annotation.ResponseBody | ||
import io.fixture.controller.form.RegistrationForm | ||
import org.springframework.web.bind.annotation.RequestMethod | ||
import org.springframework.http.MediaType | ||
import org.springframework.validation.FieldError | ||
import java.util.LinkedList | ||
import java.util.Collections | ||
|
||
[Controller] | ||
[RequestMapping(value = array("/validate"))] | ||
public class ValidationController { | ||
|
||
val valid = Collections.emptyMap<String, List<String?>>() | ||
|
||
// To test this: | ||
// $.ajax({ | ||
// type: 'POST', | ||
// url: '/fixture/validate/join.json', | ||
// contentType: 'application/json', | ||
// data: JSON.stringify({ | ||
// "username":"blah", | ||
// "password":"blah", | ||
// "confirm":"blah", | ||
// "familyName":"blah", | ||
// "givenName":"blah", | ||
// "email":"email@blah.com", | ||
// "accept":false | ||
// }), | ||
// success: function(data) { | ||
// console.log(data); | ||
// } | ||
// }); | ||
|
||
[RequestMapping( | ||
method = array(RequestMethod.POST), | ||
value = array("/register") | ||
)] | ||
[ResponseBody] | ||
fun validateRegistrationForm([Valid] [RequestBody] form: RegistrationForm) = valid | ||
|
||
[ExceptionHandler] | ||
[ResponseBody] | ||
fun handleValidationError(exception: MethodArgumentNotValidException): Map<String, List<String?>> { | ||
|
||
val errors = HashMap<String, MutableList<String?>>() | ||
|
||
exception.getBindingResult().getAllErrors().forEach { | ||
|
||
val message = it.getDefaultMessage() | ||
val name = if (it is FieldError) it.getField() else "*" | ||
|
||
errors.getOrPut(name) { LinkedList() }.add(message) | ||
} | ||
|
||
return errors | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/io/fixture/controller/form/RegistrationForm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* #%L | ||
* fixture | ||
* %% | ||
* Copyright (C) 2013 Martin Lau | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
package io.fixture.controller.form | ||
|
||
import org.hibernate.validator.constraints.ScriptAssert | ||
import org.hibernate.validator.constraints.NotEmpty | ||
import org.hibernate.validator.constraints.Email | ||
import org.hibernate.validator.constraints.Length | ||
import javax.validation.constraints.NotNull | ||
import javax.validation.constraints.AssertTrue | ||
|
||
[ScriptAssert( | ||
lang = "javascript", | ||
script = "_this.password == _this.confirm" | ||
)] | ||
class RegistrationForm( | ||
|
||
[NotEmpty] | ||
var givenName: String? = null, | ||
|
||
[NotEmpty] | ||
var familyName: String? = null, | ||
|
||
[Email] | ||
[NotNull] | ||
var email: String? = null, | ||
|
||
[NotEmpty] | ||
var username: String? = null, | ||
|
||
[NotEmpty] | ||
var password: String? = null, | ||
|
||
[NotEmpty] | ||
var confirm: String? = null, | ||
|
||
[AssertTrue] | ||
[NotNull] | ||
var accept: Boolean? = null | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/kotlin/io/fixture/controller/ValidationControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* #%L | ||
* fixture | ||
* %% | ||
* Copyright (C) 2013 Martin Lau | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
package io.fixture.controller | ||
|
||
import kotlin.test.assertEquals | ||
import org.junit.Test | ||
import org.springframework.web.bind.MethodArgumentNotValidException | ||
import org.springframework.validation.MapBindingResult | ||
import java.util.HashMap | ||
import org.springframework.validation.ObjectError | ||
import org.springframework.validation.FieldError | ||
|
||
class ValidationControllerTest { | ||
|
||
val subject = ValidationController() | ||
|
||
[Test] | ||
fun testHandleValidationError() { | ||
val bindingResult = MapBindingResult(HashMap<Any?, Any?>(), "test") | ||
bindingResult.addError(ObjectError("objectName", "must be set")) | ||
bindingResult.addError(ObjectError("objectName", "must be valid")) | ||
bindingResult.addError(FieldError("objectName", "fieldName", "must not be set")) | ||
|
||
val result = subject.handleValidationError(MethodArgumentNotValidException(null, bindingResult)) | ||
|
||
assertEquals(2, result.size) | ||
assertEquals(result.get("*"), listOf("must be set", "must be valid")) | ||
assertEquals(result.get("fieldName"), listOf("must not be set")) | ||
} | ||
|
||
} |
Oops, something went wrong.