Browse Source

Add comma decimal support for input forms

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
86f6f69df1
4 changed files with 41 additions and 6 deletions
  1. +1
    -5
      bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java
  2. +28
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/web/BigDecimalPropertyEditor.java
  3. +11
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java
  4. +1
    -1
      bookstore/src/main/resources/messages.properties

+ 1
- 5
bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java View File

@ -24,9 +24,6 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
import com.fjordtek.bookstore.annotation.CurrentYear;
@Entity
@ -143,11 +140,10 @@ public class Book {
private String isbn;
//////////
@NumberFormat(style = Style.NUMBER, pattern = "#,###.###")
@Column(nullable = false)
@Digits(
integer = 3, fraction = 2,
message = "Invalid price, possibly too many decimals"
message = "Invalid price value"
)
@DecimalMin(
value = minPrice, message = "Too low price value. Minimum allowed: " + minPrice


+ 28
- 0
bookstore/src/main/java/com/fjordtek/bookstore/web/BigDecimalPropertyEditor.java View File

@ -0,0 +1,28 @@
// Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.web;
import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;
public class BigDecimalPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String input) {
String[] decimalSeparators = {",", "."};
for (int i = 0; i < decimalSeparators.length; i++) {
// TODO count of decimal separators must be exactly 1
if (input.contains(decimalSeparators[i])) {
input = input.replace(decimalSeparators[i], ".");
break;
}
}
Number number = Double.parseDouble(input);
BigDecimal bigDecimal = BigDecimal.valueOf(number.doubleValue());
setValue(bigDecimal);
}
}

+ 11
- 0
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -2,6 +2,7 @@
package com.fjordtek.bookstore.web;
import java.math.BigDecimal;
import java.time.Year;
import java.util.HashMap;
import java.util.Map;
@ -15,6 +16,8 @@ import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@ -29,6 +32,14 @@ import com.fjordtek.bookstore.model.CategoryRepository;
@Controller
public class BookController {
/* We allow both comma and dot decimal separators
* for BigDecimal data types used in Book class.
*/
@InitBinder("book")
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(BigDecimal.class, new BigDecimalPropertyEditor());
}
@Autowired
private BookRepository bookRepository;


+ 1
- 1
bookstore/src/main/resources/messages.properties View File

@ -25,7 +25,7 @@ book.desc.example.author = Andrzej Sapkowski
book.desc.example.title = The Witcher\:\ The Lady of the Lake
book.desc.example.isbn = 1212121-3 or 4545454-67 or 6767676-890
book.desc.example.year = 1854 or 2005
book.desc.example.price = 17 or 123.5 or 45.67
book.desc.example.price = 17 or 123.5 or 45.67 or 78,43
book.error.author = Invalid author name
book.error.title = Invalid title


Loading…
Cancel
Save