Question

I'm trying to make a nullable integer, that is an integer which can also have a value set as null.

To do this, I was hoping to have an integer input with a check box to signify it is null (which could then have JavaScript hook up to disable/enable the integer input).

I see three ways of doing this:

  1. Have a custom Type which has two fields.
  2. Have a custom form widget which handles the two.
  3. Some combination of one and two.

I'm not having any trouble with that, it's pretty straight forward.

What I can't seem to find any documentation about is how to get those two fields to funnel in to a single variable.

Any ideas?

Was it helpful?

Solution

The best way I can see is applying a form transformer to your form.

If the checkbox is checked you set null to your property, if it isn't you assign the value of your integer widget.

You can have a look here and apply this to your particular case.

Side note: I wouldn't do this in the controller, it's not his job.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top