These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
How to apply filters in KO3.1 (Validation)
  • Hi ! I've seen that in KO3.1.x the Validation class doesn't support filters any more.
    Therefore, What is the way yo go for using filters in KO3.1 now?
    In ORM we have a custom run_filter method, but is protected, do you recommend to try to copy that function or there is a better and simpler solution?

    Thanks!!

  • Filters should be run before Validation. Filter, then validate. You can create your own filter methods depending on what your filtering needs are.

  • Yes I know, but I don't know why filters was removed from Validation, they were really useful, now we must create our own Filter class.
    Maybe someone of the team can explain better why they have decided to remove the FIlter capability.

  • Maybe someone of the team can explain better why they have decided to remove the FIlter capability.

    http://forum.kohanaframework.org/discussion/comment/56552/#Comment_56552

  • Like I said, they were removed because filters have nothing to do with Validation. They should be run separately.

  • Thanks @Eclipse and @xenakis. I agree with removing Filters from Validation, but I don't agree with removing Filters from Kohana!
    I think we can make our custom Filter class, but with that idea we can make our own Validation, our own ORM, our own framework.
    Kohana is great and I trust in the team's code more than mine, that's why I don't like the idea of upgrading Kohana removing functionalities that we must rewrite.

  • You don't need a class to do filters. If you want to apply a callback to all members of an array, you can use array_walk(), a built in php function. I made this very simple method that i put in an extended arr class to selectively apply a callback to specific array key members: https://gist.github.com/849312

  • Now, you should do like this if ($_POST) { /** * Filtering */ array_walk($_POST, function(&$value, $index) { $value = trim($value); $value = strip_tags($value); $value = HTML::chars($value); });

    then validation and so on.
    

  • Or

    $array = array_walk($array, 'trim');
    $array = array_walk($array, 'strip_tags');
    $array = array_walk($array, 'HTML::chars');
    

    BTW I don't think you should run HTML::chars as a filter. Just use it in your views.

  • @Wouter it useful when you check's data, like authentification, to prevent XSS.

  • @Wouter is right, you shouldn't be using HTML::chars as an input filter, it is an output filter.

  • array_walk returns bool value. You need to pass an array by reference.

    bool array_walk(&$array, 'trim_function');

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion