You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by pureza <pu...@gmail.com> on 2013/03/05 12:17:44 UTC

Persisting optional fields (null vs empty object)

Hi,

I have a form with a few inputs to fill-in Address information. When
creating the form I pass it an IModel.of(new Address()) and then I use a
CompoundPropertyModel to attach each input with the corresponding Address
field.

However, the address is optional. If the user doesn't fill it, I'd like to
save NULL to the database, instead of the empty Address instance.

Any ideas on how to achieve this?

Thanks,

Luís Pureza




--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by Sven Meier <sv...@meiers.net>.
Hi Luis,

 >a custom PropertyModel ... would you recommend against this?

models are one of Wicket's key selling points. If it works for you, go 
for it :).

Sven


On 03/05/2013 06:40 PM, pureza wrote:
> Sven,
>
> I was somehow able to get the behavior I want by implementing a custom
> PropertyModel that creates the object if needed or sets it to null if it is
> empty. This way I hope to be able to reuse this PropertyModel in all my
> optional fields without any special custom code.
>
> Would you recommend against this?
>
> Your previous suggestion also works fine, it's just that it requires
> changing many panels...
>
> Thanks,
>
> Luis
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4657000.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by pureza <pu...@gmail.com>.
Sven,

I was somehow able to get the behavior I want by implementing a custom
PropertyModel that creates the object if needed or sets it to null if it is
empty. This way I hope to be able to reuse this PropertyModel in all my
optional fields without any special custom code.

Would you recommend against this?

Your previous suggestion also works fine, it's just that it requires
changing many panels...

Thanks,

Luis



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4657000.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by Sven Meier <sv...@meiers.net>.
Wicket's form handling with let the textfields write their values to 
their models first. FormComponentPanel#updateModel() is called after that.

Sven

On 03/05/2013 04:12 PM, pureza wrote:
>> Just let the textFields work on their models as usual.
> I can't do that, because if I set the Address to null the textfields will
> try to update fields of a null object.
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4656995.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by pureza <pu...@gmail.com>.
> Just let the textFields work on their models as usual. 

I can't do that, because if I set the Address to null the textfields will
try to update fields of a null object.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4656995.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by Sven Meier <sv...@meiers.net>.
Hi,

 >The way I applied your solution ...

Just let the textFields work on their models as usual.

Your FormComponentPanel's #convertInput() can be a noop, override 
#updateModel() to null the address if all its fields are empty.

But doing this stuff outside from the UI layer (as Marc has suggested) 
would a better solution.

Sven


On 03/05/2013 03:46 PM, pureza wrote:
> Hi Sven,
>
> Thanks for your reply. Your solution works, but unfortunately I have too
> many optional fields and the resulting code feels kind of unnatural.
>
> The way I applied your solution was by creating the TextFields without any
> model (or with an empty model, to be precise). Then, convertInput() gets the
> input from all the TextFields and creates the Address instance manually (or
> sets it to null if they're all empty).
>
> As I said, I believe this results in a lot of manual work and goes against
> the Wicket way of doing things.
>
> It would seem to me that this is a common problem, no? How do people deal
> with this usually?
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4656992.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by Marc Nuri San Félix <ma...@marcnuri.com>.
Hi Pureza

If you are using an MVC architecture, this problem can be addressed in the
controller. You should check the address field on the save event and if its
values are empty, just set the address null.

On the other hand, if there is an option to set the address as a whole as a
null value, in the user interface you should give this option too. The
panel to edit the address fields should only be visible if the address
field is not null. And the user should have an option to toggle the panel.

Cheers
--
Marc Nuri
www.marcnuri.com


On Tue, Mar 5, 2013 at 3:46 PM, pureza <pu...@gmail.com> wrote:

> Hi Sven,
>
> Thanks for your reply. Your solution works, but unfortunately I have too
> many optional fields and the resulting code feels kind of unnatural.
>
> The way I applied your solution was by creating the TextFields without any
> model (or with an empty model, to be precise). Then, convertInput() gets
> the
> input from all the TextFields and creates the Address instance manually (or
> sets it to null if they're all empty).
>
> As I said, I believe this results in a lot of manual work and goes against
> the Wicket way of doing things.
>
> It would seem to me that this is a common problem, no? How do people deal
> with this usually?
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4656992.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Persisting optional fields (null vs empty object)

Posted by pureza <pu...@gmail.com>.
Hi Sven,

Thanks for your reply. Your solution works, but unfortunately I have too
many optional fields and the resulting code feels kind of unnatural.

The way I applied your solution was by creating the TextFields without any
model (or with an empty model, to be precise). Then, convertInput() gets the
input from all the TextFields and creates the Address instance manually (or
sets it to null if they're all empty).

As I said, I believe this results in a lot of manual work and goes against
the Wicket way of doing things.

It would seem to me that this is a common problem, no? How do people deal
with this usually?



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979p4656992.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Persisting optional fields (null vs empty object)

Posted by Sven Meier <sv...@meiers.net>.
A FormComponentPanel might help, implement #convertInput() to set the 
model object to null.

Sven

On 03/05/2013 12:17 PM, pureza wrote:
> Hi,
>
> I have a form with a few inputs to fill-in Address information. When
> creating the form I pass it an IModel.of(new Address()) and then I use a
> CompoundPropertyModel to attach each input with the corresponding Address
> field.
>
> However, the address is optional. If the user doesn't fill it, I'd like to
> save NULL to the database, instead of the empty Address instance.
>
> Any ideas on how to achieve this?
>
> Thanks,
>
> Luís Pureza
>
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Persisting-optional-fields-null-vs-empty-object-tp4656979.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org