views:

256

answers:

1

How can I validate (a form element in this case) to ensure that the value is a currency?

Have looked at Zend_Validate_Float.

  1. Needs to check that value is between 0 and 2dp.
  2. Ideally locale-aware (as ZVF is) to allow for locale specific formatting (thousands, decimal as comma/dot)
  3. Would also want to extend to allow/disallow negative values
  4. And provide optional upper/lower limits.

  5. Is the key, as I can do 3. and 4. with a chain.

Do I need regex?

A: 

AFAIK there is no validator for currency in ZF yet.

You need to write a custom one. See docs for writing custom validators.

Basically, the simplest thing you can do is to normalize input to floating point number (+ currency symbol if you need locale). But correcting user input is not a good solution.

For locale specific formatting you will probably need locale data stored in Zend_Locale_Data. But for comparing input values you will have to write custom currency converter.

Detecting used locale is not so simple, so I'd suggest creating additional select field, for choosing pre-defined format (e.g. locale) and using this value for your custom validator attached to the currency field.

takeshin