Custom formatting & validation causes wrong data to be saved
Custom formatting & validation causes wrong data to be saved
- Subject: Custom formatting & validation causes wrong data to be saved
- From: Nathan Hampton <email@hidden>
- Date: Tue, 15 Feb 2005 19:27:21 -0800
Hello! I have a form which includes fields for home phone and cell
phone. I have created methods to format the phone numbers for storage
and display, and have overridden the validation methods for these
attributes. Unfortunately, EOF is putting the cell phone data in the
home phone attribute and saving a null value in the cell phone
attribute when a cell phone value is provided. If you specify only the
home phone, it works fine, and it properly displays both values when
they're both in the database. I think this has something to do with
how methods are called, but I'm not quite sure what to do.
I've already verified that the bindings on the text fields (for home
phone and cell phone, in that order) are correct. Below is the Java
code for setting, formatting, validating, and displaying the input.
I'm using EOGenerator, so the calls to super.* just invoke the standard
wrapper methods around storedValueForKey() and takeStoredValueForKey().
I left out the cell phone methods because they're exactly the same as
the home phone methods. Further below is the output from all those
System.out.println() calls.
Any guidance anyone can offer would be most appreciated.
--NCH
public String homePhone()
{
return formatPhoneForDisplay(super.homePhone());
}
public void setHomePhone(String value)
{
System.out.println("HSWorker.setHomePhone(String) called with argument
" + value);
super.setHomePhone(formatPhoneForSave(value));
System.out.println("HSWorker.setHomePhone(String) complete");
}
public String validateHomePhone(String valueFromRequest) throws
NSValidation.ValidationException
{
System.out.println("HSWorker.validateHomePhone(String) called with
value " + valueFromRequest);
if (valueFromRequest != null)
{
String value = formatPhoneForSave(valueFromRequest);
if (value.length() > 10)
{
throw new NSValidation.ValidationException("Home phone is too
long.");
}
else if (value.length() < 10)
{
throw new NSValidation.ValidationException("Home phone should
include both the three-digit area code and the seven-digit number.");
}
else
{
return valueFromRequest;
}
}
else
{
return null;
}
}
private String formatPhoneForDisplay(String value)
{
try
{
return (value.substring(0,3) + "-" + value.substring(3,6) + "-" +
value.substring(6));
}
catch (java.lang.NullPointerException e)
{
return null;
}
catch (java.lang.IndexOutOfBoundsException e1)
{
return null;
}
}
private String formatPhoneForSave(String value)
{
System.out.println("HSWorker.formatPhoneForSave(String) called with
argument " + value);
String result = null;
try
{
result = value.replaceAll("\\D", "");
}
catch (java.lang.NullPointerException e)
{
result = null;
}
catch (java.lang.IndexOutOfBoundsException e1)
{
result = null;
}
System.out.println("HSWorker.formatPhoneForSave(String) returning " +
result);
return result;
}
When only the home phone is entered:
HSWorker.validateHomePhone() called with value 503-555-1212
HSWorker.formatPhoneForSave() called with argument 503-555-1212
HSWorker.formatPhoneForSave() returning 5035551212
HSWorker.validateCellPhone() called with value null
HSWorker.validateCellPhone() called with value null
HSWorker.validateHomePhone() called with value 503-555-1212
HSWorker.formatPhoneForSave() called with argument 503-555-1212
HSWorker.formatPhoneForSave() returning 5035551212
When both home and cell phone are entered:
HSWorker.validateHomePhone() called with value 503-555-1212
HSWorker.formatPhoneForSave() called with argument 503-555-1212
HSWorker.formatPhoneForSave() returning 5035551212
HSWorker.validateCellPhone() called with value 971-555-1212
HSWorker.formatPhoneForSave() called with argument 971-555-1212
HSWorker.formatPhoneForSave() returning 9715551212
HSWorker.setCellPhone() called with argument 971-555-1212
HSWorker.formatPhoneForSave() called with argument 971-555-1212
HSWorker.formatPhoneForSave() returning 9715551212
HSWorker.setCellPhone() complete
HSWorker.validateCellPhone() called with value null
HSWorker.validateHomePhone() called with value 971-555-1212
HSWorker.formatPhoneForSave() called with argument 971-555-1212
HSWorker.formatPhoneForSave() returning 9715551212
When only the cell phone is entered:
HSWorker.validateHomePhone() called with value null
HSWorker.setHomePhone() called with argument null
HSWorker.formatPhoneForSave() called with argument null
HSWorker.formatPhoneForSave() returning null
HSWorker.setHomePhone() complete
HSWorker.validateCellPhone() called with value 971-555-1212
HSWorker.formatPhoneForSave() called with argument 971-555-1212
HSWorker.formatPhoneForSave() returning 9715551212
HSWorker.setCellPhone() called with argument 971-555-1212
HSWorker.formatPhoneForSave() called with argument 971-555-1212
HSWorker.formatPhoneForSave() returning 9715551212
HSWorker.setCellPhone() complete
HSWorker.validateCellPhone() called with value null
HSWorker.validateHomePhone() called with value 971-555-1212
HSWorker.formatPhoneForSave() called with argument 971-555-1212
HSWorker.formatPhoneForSave() returning 9715551212
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden