question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Single Selection Type Fields Not Persisting

See original GitHub issue

I just started using FormsFX and it’s been great so far. I was able to create a form with string, integer, and boolean type fields and them update a model via properties with no issues. I run into a problem though, when I try to add a single selection type field. The following are the snippets of code showing what I did (I basically followed what’s in the demo application):

    private final ListProperty<Integer> allServings =
            new SimpleListProperty<>(FXCollections.observableArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    private final ObjectProperty<Integer> serving = new SimpleObjectProperty<>(1);

   /**
     * References the <code>Form</code> that is bound to this <code>Recipe</code>.
     *
     * @since 1.0.0
     */
    private Form formInstance;

    /**
     * Retrieves or creates an instance of a FormFX form using the given <code>Recipe</code>.
     *
     * @return An instance of a FormFX form using the given <code>Recipe</code>.
     *
     * @since 1.0.0
     */
    public Form getOrCreateFormInstance() {
        if (Objects.isNull(this.formInstance)) {
            createForm();
        }

        return this.formInstance;
    }

    /**
     * Helper method to initialize the FormFX form field of this <code>Recipe</code>.
     *
     * @since 1.0.0
     */
    private void createForm() {
        this.formInstance = Form.of(Group.of(
                Field.ofStringType(titleProperty())
                     .label("Title")
                     .placeholder("e.g. Chicken Wings")
                     .required("A title is required for all recipes."),
                Field.ofStringType(descriptionProperty())
                     .label("Description")
                     .placeholder("e.g. Delicious Honey BBQ wings")
                     .required("A description is required for all recipes."),
                Field.ofIntegerType(caloriesProperty())
                     .label("Calories")
                     .placeholder("e.g. 500")
                     .span(ColSpan.HALF)
                     .required("The number of calories is required for all recipes."),
                Field.ofIntegerType(cookTimeProperty())
                     .label("Cooking Time")
                     .placeholder("e.g. 60")
                     .span(ColSpan.HALF)
                     .required("The total cooking time (in minutes) is required for all recipes."),
                Field.ofSingleSelectionType(allServingsProperty(), servingProperty())
                     .label("Serving Size")
                     .placeholder("Serving Size")
                     .required("The serving size is required for all recipes."),
                Field.ofStringType(directionsProperty())
                     .label("Cooking Directions")
                     .placeholder("e.g. Bake at 375 degrees in the over for an hour and a half.")
                     .required("Cooking directions are required for all recipes."),
                Field.ofBooleanType(publishedProperty())
                     .label("Publish Recipe")))
                                .title("Recipe Form");
    }

    public Integer getServing() {
        return serving.get();
    }

    public void setServing(Integer serving) {
        this.serving.set(serving);
    }

    public ObjectProperty<Integer> servingProperty() {
        return serving;
    }

    public ObservableList<Integer> getAllServings() {
        return allServings.get();
    }

    public void setAllServings(ObservableList<Integer> allServings) {
        this.allServings.set(allServings);
    }

    public ListProperty<Integer> allServingsProperty() {
        return allServings;
    }

Here is the method that I use to save the form fields to the database:

    @FXML
    public void save(ActionEvent event) {
        this.recipe.getOrCreateFormInstance().persist();
        if (!this.recipe.getOrCreateFormInstance().isValid()) {
            return;
        }

        if (this.recipe.getId() == 0) {
            saveRecipe(this.recipe);
        }
        else {
            updateRecipe(this.recipe);
        }
    }

All other form fields save except the serving field. Am I doing anything incorrectly based on the code I’ve shared? Please let me know if you need anymore context.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dlemmermanncommented, Dec 6, 2017

It will be available on Maven once I deployed a new release 😃 Hope to find some time for that today.

1reaction
wemglcommented, Dec 2, 2017

@martinfrancois Thanks for sharing your fix. It’s working great. I did get a failing test when I built the latest master though. Here’s the stacktrace I got:

itemsTest(com.dlsc.formsfx.view.controls.SimpleControlTest)  Time elapsed: 0.028 sec  <<< FAILURE!
java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:86)
	at org.junit.Assert.assertTrue(Assert.java:41)
	at org.junit.Assert.assertTrue(Assert.java:52)
	at com.dlsc.formsfx.view.controls.SimpleControlTest.itemsTest(SimpleControlTest.java:83)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

It was only after I skipped the tests that I was able to build the jar and add it to my project’s class path and saw that your fix was working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Single Selection Type Fields Not Persisting · Issue #2 - GitHub
I was able to create a form with string, integer, and boolean type fields and them update a model via properties with no...
Read more >
Entries in Jotform Table not saving - single selection field type
I have a Table set up for one of my forms, essentially acting as my database. I have several columns in my table...
Read more >
"SELECT SINGLE" is not working. - SAP Community
Hi all, here i have one report. And in this i want just one row when my input material number match with my...
Read more >
Form not posting one field - php - Stack Overflow
I ran into a problem in my php/html code here, and I can't seem to locate the bug. The form seems to be...
Read more >
Conditional Formula on Choice Fields (single select) Not ...
Greetings, I have a Modern SharePoint list, using conditional formulas on several text fields that evaluate properly, however, when I use conditional.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found