Skip to content

Commit d20bebd

Browse files
authored
Merge pull request #49 from ahdbk/master
Update Fields elements with supported custom attributes
2 parents a37f1a2 + 708c693 commit d20bebd

File tree

9 files changed

+126
-11
lines changed

9 files changed

+126
-11
lines changed

demo/src/main/java/io/asfjava/ui/demo/screen/DemoForm.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public class DemoForm implements Serializable {
2020
@TextField(title = "Your Github Mail",fieldAddonRight="@github.com", description = "This is TextField with fieldAddonRight")
2121
private String gitHub;
2222

23+
2324
// @Tab(title = "Contact", index = 2)
24-
@Password(title = "Password", placeHolder = "Please set you password", description = "This is password")
25+
@Password(title = "Password", placeHolder = "Please set you password",minLenght=6,description = "This is password", validationMessage = "The password must contain a minimum of 6 characters ")
2526
private String password;
2627

2728
@Tab(title = "Info", index = 1)
28-
@TextField(title = "First Name", placeHolder = "Your first name", description = "This is a description for your first name field")
29+
@TextField(title = "First Name", placeHolder = "Your first name",minLenght=3,maxLenght=10, validationMessage = "The First Name must contain a minimum of 3 and a max of 10 characters ", description = "This is a description for your first name field with minLenght and maxLenght")
2930
private String firstName;
3031

3132
// @Tab(title = "Info", index = 1)
@@ -54,7 +55,7 @@ public class DemoForm implements Serializable {
5455
private String civilState;
5556

5657
// @Tab(title = "Contact", index = 2)
57-
@TextArea(title = "Address", placeHolder = "Fill your address please", description = "This is textarea")
58+
@TextArea(title = "Address", placeHolder = "Fill your address please",maxLenght=30, description = "This is textarea" , validationMessage="Max 30 charactres")
5859
private String address;
5960

6061
@Tab(title = "Additional Info", index = 3)

src/main/java/io/asfjava/ui/core/form/Password.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
String description() default "";
1717

18+
int minLenght() default 0;
19+
20+
int maxLenght() default Integer.MAX_VALUE;
21+
22+
String pattern() default "";
23+
1824
String fieldAddonLeft() default"";
1925

2026
String fieldAddonRight() default"";

src/main/java/io/asfjava/ui/core/form/TextArea.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
String placeHolder() default "";
1515

1616
String description() default "";
17+
18+
int minLenght() default 0;
19+
20+
int maxLenght() default Integer.MAX_VALUE;
21+
22+
String fieldAddonLeft() default"";
23+
24+
String fieldAddonRight() default"";
1725

1826
boolean noTitle() default false;
1927

src/main/java/io/asfjava/ui/core/form/TextField.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
String description() default "";
1717

18+
int minLenght() default 0;
19+
20+
int maxLenght() default Integer.MAX_VALUE;
21+
1822
String fieldAddonLeft() default"";
1923

2024
String fieldAddonRight() default"";

src/main/java/io/asfjava/ui/core/generators/TextAreaGenerator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public void generate(ObjectNode fieldFormDefinition, Field field) {
1414
fieldFormDefinition.put("key", field.getName());
1515
fieldFormDefinition.put("type", "textarea");
1616

17+
String fieldAddonLeft = annotation.fieldAddonLeft();
18+
if (!fieldAddonLeft.isEmpty()) {
19+
fieldFormDefinition.put("fieldAddonLeft", fieldAddonLeft);
20+
}
21+
22+
String fieldAddonRight = annotation.fieldAddonRight();
23+
if (!fieldAddonRight.isEmpty()) {
24+
fieldFormDefinition.put("fieldAddonRight", fieldAddonRight);
25+
}
26+
1727
String description = annotation.description();
1828
if (!description.isEmpty()) {
1929
fieldFormDefinition.put("description", description);

src/main/java/io/asfjava/ui/core/schema/decorators/PasswordSchemaDecorator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public void customizeSchema(BeanProperty property, JsonSchema jsonschema) {
1414
if (annotation != null && annotation.title() != null) {
1515
((StringSchema) jsonschema).setTitle(annotation.title());
1616
}
17+
if (annotation.pattern() != null) {
18+
((StringSchema) jsonschema).setPattern(annotation.pattern());
19+
}
20+
if (annotation.minLenght() != 0) {
21+
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
22+
}
23+
if (annotation.maxLenght() != Integer.MAX_VALUE) {
24+
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
25+
}
1726
}
1827

1928
@Override

src/main/java/io/asfjava/ui/core/schema/decorators/TextAreaSchemaDecorator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ public void customizeSchema(BeanProperty property, JsonSchema jsonschema) {
1414
if (annotation != null && annotation.title() != null) {
1515
((StringSchema) jsonschema).setTitle(annotation.title());
1616
}
17+
18+
if (annotation.minLenght() != 0) {
19+
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
20+
}
21+
if (annotation.maxLenght() != Integer.MAX_VALUE) {
22+
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
23+
}
1724
}
1825

1926
@Override

src/main/java/io/asfjava/ui/core/schema/decorators/TextFieldSchemaDecorator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public void customizeSchema(BeanProperty property, JsonSchema jsonschema) {
1818
if (annotation.pattern() != null) {
1919
((StringSchema) jsonschema).setPattern(annotation.pattern());
2020
}
21+
if (annotation.minLenght() != 0) {
22+
((StringSchema) jsonschema).setMinLength(annotation.minLenght());
23+
}
24+
if (annotation.maxLenght() != Integer.MAX_VALUE) {
25+
((StringSchema) jsonschema).setMaxLength(annotation.maxLenght());
26+
}
2127
}
2228
}
2329

src/test/java/io/asfjava/ui/core/schema/UiFormSchemaGeneratorTest.java

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.junit.Test;
1414

1515
import com.fasterxml.jackson.core.JsonProcessingException;
16-
import com.fasterxml.jackson.databind.JsonMappingException;
1716
import com.fasterxml.jackson.databind.ObjectMapper;
1817

1918
import io.asfjava.ui.core.GeneratorFactoryInitializer;
@@ -186,6 +185,7 @@ public void testGenerate_Password() throws JsonProcessingException {
186185

187186
String json = new ObjectMapper().writeValueAsString(ui);
188187
Assert.assertThat(json, hasJsonPath("$.schema.properties.password.title", equalTo("Password")));
188+
Assert.assertThat(json, hasJsonPath("$.schema.properties.password.pattern", equalTo("[a-z]")));
189189
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')]", hasSize(1)));
190190
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].description", hasItem("This is password")));
191191
Assert.assertThat(json,
@@ -204,6 +204,7 @@ public void testGenerate_Password_WithFieldAddonLeft() throws JsonProcessingExce
204204
String json = new ObjectMapper().writeValueAsString(ui);
205205
Assert.assertThat(json, hasJsonPath("$.schema.properties.password.title",equalTo("Password")));
206206
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')]",hasSize(1)));
207+
Assert.assertThat(json, hasJsonPath("$.schema.properties.password.pattern", equalTo("[a-z]")));
207208
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].description",hasItem("This is password")));
208209
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].placeholder",hasItem("Please set you password")));
209210
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].validationMessage",hasItem("this is a validation msg")));
@@ -221,6 +222,7 @@ public void testGenerate_Password_WithFieldAddonRight() throws JsonProcessingExc
221222
String json = new ObjectMapper().writeValueAsString(ui);
222223
Assert.assertThat(json, hasJsonPath("$.schema.properties.password.title",equalTo("Password")));
223224
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')]",hasSize(1)));
225+
Assert.assertThat(json, hasJsonPath("$.schema.properties.password.pattern", equalTo("[a-z]")));
224226
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].description",hasItem("This is password")));
225227
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].placeholder",hasItem("Please set you password")));
226228
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='password')].validationMessage",hasItem("this is a validation msg")));
@@ -248,6 +250,48 @@ public void testGenerate_TextArea() throws JsonProcessingException {
248250
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].notitle", hasItem(true)));
249251
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].readonly", hasItem(true)));
250252

253+
}
254+
255+
public void testGenerate_TextArea_WithFieldAddOnLeft() throws JsonProcessingException {
256+
UiForm ui = UiFormSchemaGenerator.get().generate(TextAreaForm2.class);
257+
258+
String json = new ObjectMapper().writeValueAsString(ui);
259+
Assert.assertThat(json, hasJsonPath("$.schema.properties.address.title", equalTo("Address")));
260+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')]", hasSize(1)));
261+
Assert.assertThat(json, hasJsonPath("$.schema.properties.address.minLenght", equalTo(6)));
262+
Assert.assertThat(json, hasJsonPath("$.schema.properties.address.maxLenght", equalTo(10)));
263+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].description", hasItem("This is textarea")));
264+
Assert.assertThat(json,
265+
hasJsonPath("$.form[?(@.key=='address')].placeholder", hasItem("Fill your address please")));
266+
Assert.assertThat(json,
267+
hasJsonPath("$.form[?(@.key=='address')].validationMessage", hasItem("this is a validation msg")));
268+
// Assert.assertThat(json,
269+
// hasJsonPath("$.form[?(@.key=='password')].type",hasItem("textArea")));
270+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].notitle", hasItem(true)));
271+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].readonly", hasItem(true)));
272+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].fieldAddonLeft", hasItem("@")));
273+
274+
275+
}
276+
277+
public void testGenerate_TextArea_WithFieldAddOnRight() throws JsonProcessingException {
278+
UiForm ui = UiFormSchemaGenerator.get().generate(TextAreaForm3.class);
279+
280+
String json = new ObjectMapper().writeValueAsString(ui);
281+
Assert.assertThat(json, hasJsonPath("$.schema.properties.address.title", equalTo("Address")));
282+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')]", hasSize(1)));
283+
Assert.assertThat(json, hasJsonPath("$.schema.properties.address.minLenght", equalTo(6)));
284+
Assert.assertThat(json, hasJsonPath("$.schema.properties.address.maxLenght", equalTo(10)));
285+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].description", hasItem("This is textarea")));
286+
Assert.assertThat(json,
287+
hasJsonPath("$.form[?(@.key=='address')].placeholder", hasItem("Fill your address please")));
288+
Assert.assertThat(json,
289+
hasJsonPath("$.form[?(@.key=='address')].validationMessage", hasItem("this is a validation msg")));
290+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].notitle", hasItem(true)));
291+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].readonly", hasItem(true)));
292+
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='address')].fieldAddonRight", hasItem("@")));
293+
294+
251295
}
252296

253297
@Test
@@ -347,12 +391,12 @@ public void testGenerate_TabbedFormed() throws JsonProcessingException{
347391
Assert.assertThat(json, hasJsonPath("$.form[?(@.tabs)].tabs[?(@.title=='Contact')].items[*]",hasSize(1)));
348392
Assert.assertThat(json, hasJsonPath("$.form[?(@.key=='webSite')]"));
349393
}
350-
351394
}
395+
352396

353397
class TextFieldForm implements Serializable {
354398

355-
@TextField(title = "First Name", placeHolder = "Your first name", pattern = "[a-z]", noTitle = true, validationMessage = "this is a validation msg", description = "This is a description for your first name field", readOnly = true)
399+
@TextField(title = "First Name", placeHolder = "Your first name", pattern = "[a-z]",minLenght=6,maxLenght=10, noTitle = true, validationMessage = "this is a validation msg", description = "This is a description for your first name field", readOnly = true)
356400
private String firstName;
357401

358402
public String getFirstName() {
@@ -363,7 +407,7 @@ public String getFirstName() {
363407

364408
class TextFieldFormRight implements Serializable {
365409

366-
@TextField(title = "First Name", placeHolder = "Your first name", fieldAddonRight = "@", pattern = "[a-z]", noTitle = true, validationMessage = "this is a validation msg", description = "This is a description for your first name field")
410+
@TextField(title = "First Name", placeHolder = "Your first name", fieldAddonRight = "@", pattern = "[a-z]",minLenght=6,maxLenght=10, noTitle = true, validationMessage = "this is a validation msg", description = "This is a description for your first name field")
367411
private String firstName;
368412

369413
public String getFirstName() {
@@ -434,7 +478,7 @@ public Double getNumber() {
434478

435479
class PasswordForm implements Serializable {
436480

437-
@Password(title = "Password", placeHolder = "Please set you password", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
481+
@Password(title = "Password", placeHolder = "Please set you password", pattern = "[a-z]", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
438482
private String password;
439483

440484
public String getPassword() {
@@ -444,7 +488,7 @@ public String getPassword() {
444488

445489
class PasswordForm2 implements Serializable {
446490

447-
@Password(title = "Password", placeHolder = "Please set you password", fieldAddonRight = "@", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
491+
@Password(title = "Password", placeHolder = "Please set you password", pattern = "[a-z]",minLenght=6,maxLenght=10, fieldAddonRight = "@", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
448492
private String password;
449493

450494
public String getPassword() {
@@ -454,17 +498,37 @@ public String getPassword() {
454498

455499
class PasswordForm3 implements Serializable {
456500

457-
@Password(title = "Password", placeHolder = "Please set you password", fieldAddonLeft = "@", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
501+
@Password(title = "Password", placeHolder = "Please set you password", pattern = "[a-z]",minLenght=6,maxLenght=10, fieldAddonLeft = "@", description = "This is password", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
458502
private String password;
459503

460504
public String getPassword() {
461505
return password;
462506
}
463507
}
464508

509+
class TextAreaForm2 implements Serializable {
510+
511+
@TextArea(title = "Address", placeHolder = "Fill your address please", fieldAddonLeft = "@",minLenght=6,maxLenght=10, description = "This is textarea", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
512+
private String address;
513+
514+
public String getAddress() {
515+
return address;
516+
}
517+
}
518+
519+
class TextAreaForm3 implements Serializable {
520+
521+
@TextArea(title = "Address", placeHolder = "Fill your address please",fieldAddonRight = "@",minLenght=6,maxLenght=10, description = "This is textarea", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
522+
private String address;
523+
524+
public String getAddress() {
525+
return address;
526+
}
527+
}
528+
465529
class TextAreaForm implements Serializable {
466530

467-
@TextArea(title = "Address", placeHolder = "Fill your address please", description = "This is textarea", noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
531+
@TextArea(title = "Address", placeHolder = "Fill your address please", description = "This is textarea",minLenght=6,maxLenght=10, noTitle = true, validationMessage = "this is a validation msg", readOnly = true)
468532
private String address;
469533

470534
public String getAddress() {

0 commit comments

Comments
 (0)