How can I have a numeric field allow and default to a Null / unspecified value?
When I add the control for this field to the screen it always defaults to "0" on new entities...
[PXDBInt]
[PXDefault(TypeCode.DBNull, "", PersistingCheck=PXPersistingCheck.Nothing)]
[PXUIField(DisplayName="Nullable Int")]
The issue here appears to be whether the field is in a Form area or Grid table. In a Grid a null-value is rendered as an empty cell and persists as null if left empty. However, when in a Form area a null value is rendered with "0". If the value is changed attempting to then change it back to empty/null results in a zero value persisting to the database.
The solution I found was when creating the control instead of using a PXNumberEdit type field, changing it to a PXTextEdit field and setting the TextMode property to "Number" gave the desired effect. Null values would render as empty and are distinguished from actual zero values.
There is AllowNull attribute in px:PXNumberEdit control.
In this implementation you do not even need to add the PXDefault attribute to your field.
I added MinValue = 1, MaxValue = 999999
in c# code and DisplayFormat="n0" MaxLength="6"
in .aspx because of my task, it should work without this.
<px:PXNumberEdit runat="server" ID="edUsrCapacity" DataField="UsrCapacity"
DisplayFormat="n0" MaxLength="6" AllowNull="True" />
[PXDBInt(MinValue = 1, MaxValue = 999999)]
[PXUIField(DisplayName = Messages.FsEquipment.Capacity)]
public virtual int? UsrCapacity
{
get;
set;
}
public abstract class usrCapacity : BqlInt.Field<usrCapacity>
{
}
The default value for all fields is null unless you force a value with PXDefault.
If you remove the [PXDefault] attribute it will automatically default to null.
Make sure your value is defined as a int? and not int as well. the "?" denotes it as "nullable"
For example:
#region CurrentConfigRevision
public abstract class currentConfigRevision : PX.Data.IBqlField
{
}
protected int? _CurrentConfigRevision;
[PXDBInt]
[PXUIField(DisplayName="Current Revision")]
public virtual int? CurrentConfigRevision
{
get
{
return this._CurrentConfigRevision;
}
set
{
this._CurrentConfigRevision = value;
}
}
#endregion
This will always have a null value unless the user specifies in the interface (or it's set via code)