Ah that's even better, thank you grrigri, you've made my life a little easier :-)
Tim On Mar 28, 10:40 pm, grigri <[EMAIL PROTECTED]> wrote: > All validation entries have an `allowEmpty` parameter that says > whether the field must be filled in or not. An empty field will *not* > go through the validation rule itself and will be validated or failed > depending on the `allowEmpty` parameter (not to be confused with the > `required` parameter, which controls whether the field must be > *present* in the data). > > To validate a decimal number you can use a callback, like you have, or > a regular expression. For example: > > var $validate = array( > 'field' => array( > 'allowEmpty' => true, > 'rule' => '/^[0-9]*\\.?[0-9]+$/', > 'message' => 'This must be blank or a positive decimal number' > ) > ); > > You could use a different regular expression to allow negative > numbers, enforce limits on the fraction digits, or allow scientific > notation as well. > > For example, if you wanted a positive number with maximum 2 digits > after the decimal point then you'd use something like: > > '/^[0-9]+(\\.[0-9]{,2})?$/' > > On Mar 28, 8:12 am, Tim W <[EMAIL PROTECTED]> wrote: > > > In case anyone else wants to know how i've done it, it's as simple as > > this > > > Model > > > var $validate = array( > > 'fieldName' => array('integer_optional' => array('rule' => > > array('optionalInteger', 'fieldName'))) > > ); > > // NB: integer_optional is the key to look up the error message > > > function optionalInteger($data, $fieldName) { > > if (strlen($data[$fieldName]) > 0) { > > return is_numeric($data); > > } > > > return true; > > > } > > > I couldn't work out how to call an existing validation method (given > > the 2 minutes I spent on it) so I just wrote my own since it was > > trivial and quicker. I'm still interested to hear a better way to do > > this, if one exists :) > > > Tim > > > On Mar 28, 8:56 pm, Tim W <[EMAIL PROTECTED]> wrote: > > > > Hi all, > > > > I have some data that's optional, but if it's filled in it must be a > > > decimal. Is there an easy way to do this with 1.2 validation? My > > > current though is that I might create a custom validation rule, and > > > from their if the field isn't blank then send it through to the built > > > in validation code. > > > > Thanks > > > > Tim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---