cakephp - Cake PHP validation, change incoming $data -


i got problem cake validation. user enters e.g. "20€" in form field , want number "20" stripped out euro-sign. validation methods allow checking if it´s supposed be.

i´m create function in rule-set i´m struggling going further because function returns in cake context either true or false, not modified variable need ...

'rule-3' => array(     'rule'    => 'checkeuro',     'message' => 'don´t need 1 because should conversion :-(' )  public function checkeuro($data) {     $data = str_replace('€','',$data);     return preg_replace('/[^0-9]/','', html_entity_decode($data)); } 

i hope can help. in advance

if need store amount inside database , not euro-sign, should strip euro-sign before validating.

you can inside beforevalidate() callback of model;

public function beforevalidate(array $options = array()) {     if (!empty($this->data[$this->alias]['fieldname']) {         // strip euro-sign         // note: should take plain, non-entity € account here?         $this->data[$this->alias]['fieldname'] = str_replace(             '€', '', $this->data[$this->alias]['fieldname']         );     }      return parent::beforevalidate($options); } 

(of course, replace fieldname actual fieldname)

after stripping currency-symbol, validate value using decimal or numeric validation rule

note if do want store value including currency-symbol, use core money validation rule.


Comments