Yii Framework Forum: Cfiltervalidator - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cfiltervalidator Rate Topic: -----

#1 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 27-October 10

Posted 25 April 2014 - 07:27 AM

I don't understand why there's a validator "CFilterValidator". For me it's not a validator and it should not be mixed with them.
It's a filter and should be used as so.
0

#2 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 809
  • Joined: 02-July 10
  • Location:Central Poland

Posted 25 April 2014 - 08:52 AM

yes, but you can say very same about safe, unsafe, default, etc. they are not real validators.
They allow you to use validation stream and configuration for better data hadling (in pipeline maneer):

1. set default NULL value for fields with empty string (common problem when posting from HTTP form)
2. if fieldX is empty -> copy there value from another field
3. make other field value lowercase
4. check if all those fields have proper value (real validation)

now - you could do same other way (like separate model methods, beforeValidation handler, etc) but that would be less readable and you culd not set mixed order (like: validate fieldX, then filter fieldY based on validated fieldX, then validate fieldY).
red
0

#3 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 27-October 10

Posted 25 April 2014 - 09:04 AM

It confuses me because for me filters (especially sanitize filters) come before processing data and validation comes before interacting with DB.
If I use a filter which is a validator, it implies that I must call validate() before I do something with the data, and validate will be called again through save() when I save my data to db.
0

#4 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 809
  • Joined: 02-July 10
  • Location:Central Poland

Posted 25 April 2014 - 10:44 AM

View Postjpj, on 25 April 2014 - 09:04 AM, said:

It confuses me because for me filters (especially sanitize filters) come before processing data and validation comes before interacting with DB.
If I use a filter which is a validator, it implies that I must call validate() before I do something with the data, and validate will be called again through save() when I save my data to db.

you can think about filters as something that enforces given data format but without throwing errors but rather converting data to required format. The difference is like this:

validator: if fieldX is not all lowercase -> report error and stop processing
filter: if fieldX is not all lowercase -> make it lowercase and go on
red
0

#5 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 27-October 10

Posted 26 April 2014 - 05:48 AM

I understand this but when you want to sanitize data before processing it,how do you filter it?you call validate?for me the flow is often like this:I receive data in the controller,I want to sanitize-filter it,I do anything with the data,I update the db with data.Thats right when I update db that I call validate,not before.
0

#6 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 809
  • Joined: 02-July 10
  • Location:Central Poland

Posted 26 April 2014 - 06:00 AM

You are right, but it is up to developer to remember to sanitize data everywhere it is assigned to model. With filters you may safely assign any data (from HTTP form, or other sources like outer services) and just save the model. Everytime you save it - filters will sanitize data. It is safer than assuming developer sinitized data before assigning it, especialy when working with services (not plain http form data) because developers tend to assume such data is already sanitized...

anyway - if you don't like just don't use filters. In my oppinion - you can create safer applications when using filters properly together with validators.
red
0

#7 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 27-October 10

Posted 26 April 2014 - 06:55 AM

It s just that for me the concept is different between a filter and a validator so they should not be mixed.For me if data has to be filtered,it should be in a process different from the validation process.
0

#8 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 26 April 2014 - 08:44 PM

View Postredguy, on 25 April 2014 - 08:52 AM, said:

1. set default NULL value for fields with empty string (common problem when posting from HTTP form)

Could you please give an example of how to do this one. I have need of it but am not sure how :)
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#9 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 809
  • Joined: 02-July 10
  • Location:Central Poland

Posted 27 April 2014 - 06:29 AM

View Postjkofsky, on 26 April 2014 - 08:44 PM, said:

Could you please give an example of how to do this one. I have need of it but am not sure how :)


array( 'column_which_are_default_NULL', 'default', 'setOnEmpty'=>true, 'value'=>NULL )

red
1

#10 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 27 April 2014 - 06:32 PM

View Postredguy, on 27 April 2014 - 06:29 AM, said:

array( 'column_which_are_default_NULL', 'default', 'setOnEmpty'=>true, 'value'=>NULL )


thanks
Do, or do not. There is no 'try.' Jedi Master Yoda
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users