0

I read Yii tutorial about data validation, but have the question: how can I to automatically filter user input (for example, I know, that identifier must be trimmed, or identifiers must be integers).

Are there any best practices?

Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39
  • Do you want to filter before validation? or do you want to validate that id's are integers, etc. – Skatox Jan 05 '13 at 14:56
  • 2
    If you want to apply specific transformations to the input, you can specify a [`filter`](http://www.yiiframework.com/doc/api/1.1/CFilterValidator) validation rule, or use `beforeValidate`/`afterValidate`. – DCoder Jan 05 '13 at 14:58
  • I want to filter before validation, you are right. Also, I can want to not validate data, only filter them for my purposes. – Guy Fawkes Jan 05 '13 at 14:58
  • DCoder, but I must to specify all transformations in pure code? Aren't there analogs of Zend_Filter subclasses? – Guy Fawkes Jan 05 '13 at 15:00
  • Personally, I disagree with the idea that `Zend_Filter` should be run *before* validation. If the user entered `1,2e3` in a numeric field and your system filtered that to `123`, `1200` or `1` instead of rejecting it, you really should expect user complaints. *First*, use the validator rules (most of these *are* pre-made) to ensure the input is well formatted, *then*, if you must, apply some filtering in `afterValidate` to coerce the attributes into the right data type/format, *then* do all the data processing. There are no prepared filters like Zend_Filter, you'll have to craft your own. – DCoder Jan 05 '13 at 15:15
  • It depends. In my project I have two main types of logic, of course: filtering after validation and validation after filtering (but can be validation only or filtering only cases) Thank you. As I can see, it's not good practice to reinvent a bicycle and I'm wondered why Yii developers didn't realize filters in framework. – Guy Fawkes Jan 05 '13 at 15:31

0 Answers0