2

I want to set tax percentage like 100.20. It accept only numeric value , from 1-100 not exceed more than 100 numeric value.So please suggest appropriate solution.

Shital Jachak
  • 449
  • 6
  • 14
  • you can do validation using javascript, instead of doing at server side. – s4suryapal May 10 '14 at 06:46
  • 2
    @s4suryapal That's not a good advice, client side validation cannot, and should not ever be trusted! Ultimately you _must_ validate on the server side too! – ndm May 10 '14 at 13:28

1 Answers1

4

use

'rule'    => 'numeric', 

and

'rule'    => array('range', 1, 100),

http://book.cakephp.org/2.0/en/models/data-validation.html#Validation::range

also check here CakePHP custom validation with a variable in the error message

Community
  • 1
  • 1
Er.KT
  • 2,852
  • 1
  • 36
  • 70
  • This is correct sir but i also want decimal place only 2?? – Shital Jachak May 10 '14 at 09:08
  • so just change 'rule' => 'decimal', – Er.KT May 10 '14 at 09:10
  • please can you share your code here,please post it in question, as you says that it will take negative rang values which is not possible as here we have given range 1 to 100 only – Er.KT May 10 '14 at 09:26
  • array( 'numeric' => array( 'rule' => array('numeric'), 'allowEmpty' => true, 'message' => __("err__numberfield", array(__('lbl_TaxPercent', true))), ), 'numeric' => array( 'rule' => array('range', 0, 101), 'allowEmpty' => true, 'message' => __("err__decimalValues", array(__('lbl_TaxPercent', true))), ), 'decimal' => array( 'rule' => array('decimal', 2), 'allowEmpty' => true, 'message' => __("err__decimal", array(__('lbl_TaxPercent', true))), ) ); – Shital Jachak May 10 '14 at 09:33
  • 1
    @Shamika Update _your question_ with the properly formatted code, it's totally unreadable in a comment, and hard to follow for future readers! – ndm May 10 '14 at 13:31
  • That validation method checks for [greater than and less than the limits](https://github.com/cakephp/cakephp/blob/2.0.0/lib/Cake/Utility/Validation.php#L648) - it won't accept `1` or `100` as valid values. – AD7six May 10 '14 at 13:31