Maybe someone should copy this thread to the wiki :D It's more documentation on
ERXValidation than you'll find there.
Unrelated note: If you are working with dollar values, use a BigDecimal.
Doubles and floats are a bad choice for financial math in Java.
Short answer:
@@escapedValue@@
Long answer:
ERXValidation uses the ERXSimpleTemplateParser for its string templates. The
ERXSimpleTemplateParser uses context objects to provide those @@ values. The
parser can accept two context objects, the primary, and a fall back.
In the case of ERXValidation, the primary context object is the
ERXValidationException itself. The secondary context, if one exists, is the
ERXValidationException.context(). In ERDirectToWeb, this is the d2wContext. If
you aren't using direct to web, you can provide your own context by providing
an ERXValidationFactory defaultDelegate at startup and implementing the
ExceptionDelegateInterface on it.
You will notice, ERXValidationException already has an escapedValue() method.
So, assuming your validation exception is the type of exception that would have
a value, then you can use this method to display the value back to the user. If
you just use @@value@@ instead, you could open yourself up to XSS exploits.
Regarding your validateQuoteAmount() method, I would not swallow that
exception. I would generate an ERXValidationException from it.
If you look at the constants defined in ERXValidationException you will see a
number of predefined validation exception types. In your case, that is probably
InvalidNumberException if the parser decides it is not a number, and
InvalidValueException if the number is out of some predefined acceptable range.
So, you could replace your method with something like
public Object validateQuoteAmount(Object aValue) throws
ValidationException {
DecimalFormat df = new DecimalFormat("#.##");
Double returnValue;
try {
returnValue = Double.valueOf(df.format(aValue));
} catch (NumberFormatException e) {
ERXValidationFactory factory =
ERXValidationFactory.defaultFactory();
ERXValidationException ex =
factory.createCustomException(
this, "quoteAmount", aValue,
ERXValidationException.InvalidNumberException);
throw ex;
}
return returnValue;
}
Then you would provide a validation key like
"Quote.quoteAmount.InvalidNumberException" = "You must enter a dollar amount in
the format 123.00. You entered <b>@@escapedValue@@</b>.";
in each language that you support.
Ramsey
On Aug 22, 2013, at 7:15 AM, Theodore Petrosky wrote:
> So I can create ValidationTemplate.strings
>
> "Quote.quoteAmount"="You must enter a dollar amount in the format 123.00 (you
> entered weird characters)!";
>
> is there a key that I can access what was typed? @@enteredQuoteAmountValue@@
>
> so I can add it to the error message?
>
>
>
> also,
> if I input 234.66666666666 in the field my app crashes with:
>
> Error: com.webobjects.eoaccess.EOGeneralAdaptorException: The
> attribute quoteAmount has a scale of 2, but the value 123.666666666 has a
> scale of 9: Rounding necessary
> Reason: The attribute quoteAmount has a scale of 2, but the value
> 123.666666666 has a scale of 9: Rounding necessary
>
> so I created a validation method:
>
> public Object validateQuoteAmount(Object aValue) throws
> ValidationException {
>
> Double returnValue = 0.0;
> try {
> DecimalFormat df = new DecimalFormat("#.##");
> returnValue = Double.valueOf(df.format(aValue));
>
> } catch (Exception e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> return returnValue;
> }
>
> is there a better way?
>
> Ted
>
>
> On Aug 21, 2013, at 1:42 PM, Ramsey Gurley <[email protected]> wrote:
>
>> http://thecodinglove.com/post/49933241299/when-i-start-using-a-3rd-party-lib-and-notice-there-is
>>
>> ERXValidationFactory.templateForEntityPropertyType determines what keys are
>> considered for most validation messages. In order, they are
>>
>> entity.property.type
>> entity.property
>> property.type
>> property
>> type
>>
>> So let's say you have an EO named Document with a mandatory title attribute.
>> Leaving that blank would result in a NullPropertyException type validation
>> error. So, your keys would be from most specific to least:
>>
>> Document.title.NullPropertyException
>> Document.title
>> title.NullPropertyException
>> title
>> NullPropertyException
>>
>> Typically, the least specific key is already defined in Wonder's
>> ValidationTemplate.strings in the frameworks.
>>
>> "NullPropertyException" = "Please provide @@indefiniteArticleForProperty@@
>> <b>@@displayNameForProperty@@</b>.";
>>
>> You can override these with the more specific keys.
>>
>> In this specific case, the validation template string is actually defined
>> elsewhere. It's the result of an EOGeneralAdaptorException being converted
>> into a standard validation exception in the ERXSQLHelper. You can find it in
>> the PostgresqlSQLHelper.handleDatabaseException method.
>>
>> On Aug 21, 2013, at 10:22 AM, Christoph Wick wrote:
>>
>>> Is there a documentation about the possible keys for that? What kinds of
>>> keys can I put into a ValidationTemplates.string?
>>>
>>> Thx, C.U.CW
>>> --
>>> What are the three enemies of a programmer? Sunlight, oxygen, and the
>>> appalling roar of the birds.
>>>
>>> On 14.08.2013, at 04:07, Theodore Petrosky <[email protected]> wrote:
>>>
>>>> wow, that's beautiful
>>>>
>>>>
>>>>
>>>> On Aug 13, 2013, at 8:45 PM, Paul Hoadley <[email protected]> wrote:
>>>>
>>>>> Hi Ted,
>>>>>
>>>>> On 14/08/2013, at 10:03 AM, Theodore Petrosky <[email protected]> wrote:
>>>>>
>>>>>> I added an index and unique constraint to my User. loginName column.
>>>>>>
>>>>>> I see in the logs that when I try to violate the uniqueness my d2w does
>>>>>> the correct thing and complain however this is the error:
>>>>>>
>>>>>> Could not save your changes: CustomMethodException
>>>>>>
>>>>>> I do see in the logs:
>>>>>>
>>>>>> ERROR: duplicate key value violates unique constraint "loginname_idx"
>>>>>>
>>>>>> did I miss something in a rule to pass the better worded error message
>>>>>> to the user?
>>>>>
>>>>> Create Resources/English.lproj/ValidationTemplate.strings and add:
>>>>>
>>>>> {
>>>>> "UniqueConstraintException.loginname_idx" = "Please choose a different
>>>>> username.";
>>>>> }
>>>>>
>>>>> Does that work?
>>>>>
>>>>>
>>>>> --
>>>>> Paul Hoadley
>>>>> http://logicsquad.net/
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Do not post admin requests to the list. They will be ignored.
>>>> Webobjects-dev mailing list ([email protected])
>>>> Help/Unsubscribe/Update your Subscription:
>>>> https://lists.apple.com/mailman/options/webobjects-dev/wicki%40me.com
>>>>
>>>> This email sent to [email protected]
>>>
>>>
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Webobjects-dev mailing list ([email protected])
>>> Help/Unsubscribe/Update your Subscription:
>>> https://lists.apple.com/mailman/options/webobjects-dev/rgurley%40smarthealth.com
>>>
>>> This email sent to [email protected]
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Webobjects-dev mailing list ([email protected])
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/webobjects-dev/tedpet5%40yahoo.com
>>
>> This email sent to [email protected]
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]