What is the main purpose of a filter in YII?

How does the title clarify what is the main purpose of filters in Yii? I'm new to Yii and a little confused about filters and validators? Can anyone explain this to me?

+3


source to share


3 answers


The validator will check that the attribute in the model is what it should be: integer, date, less than the specified size, ...

Example:

public function rules()
{
    return array(
        //username and password are required
        array('username, password', 'required'),
        //myInt is a number between 0 and 255
        array('myInt', 'numerical', 'min'=>0, 'max'=> 255),
    );
}

      

Validation rules will be checked when calling $model->validate()

or $model->save()

. If one of the validators fails, then an error will be sent to the user.

You can find out the errors caused by calling $model->getErrors()

Source: Checking Model Rules

Filter definition:

The filter can be applied before and after the action. It can change the context that the action should perform or beautify the result that the action generates.



So basically it will do some work before the controller method is called (so before doing anything on the screen) or after the controller is executed (so this can happen after the data is validated and added to the db) ... As an example we can say:

  • Check user authorization
  • Implements HTTP caching
  • ...

To apply filters to actions, we need to override the CController :: filters () method. The method should return an array of filter configurations. For example,

public function filters()
{
    return array(
       'postOnly + edit, create',
        array(
            'application.filters.PerformanceFilter - edit, create',
            'unit'=>'second',
        ),
    );
}

      

Using the plus and minus commands, we can specify which actions the filter should and should not apply to. In the example above, the filter postOnly

will be applied to the actions edit

and create

, and the filter PerformanceFilter

will be applied to all actions EXCEPT edit

and create

. If there is no plus or minus in the filter configuration, the filter will be applied to all actions.

Source: Yii API about CFilter and Yii Guide

So, if you want to validate some data, use validators, and if what you want to do does not depend on the model (i.e. checking user login ...), then you must implement a filter. Overall, the difference between filter and validator is pretty obvious.

+6


source


Validators are used to prevent inserting or updating invalid data into db. Filters can be used to prepare for preparation before or after inspection



class LoginForm extends CFormModel
{
    public $mail;
    public $password;
    public $rememberMe;

    public function rules()
    {
        return array(
            array('mail, password', 'filter'=>'trim'),
            array('mail', 'filter'=>'mb_strtolower'),
            array('mail, password', 'required'),
            array('mail', 'email'),
            array('rememberMe', 'boolean'),
            array('password', 'authenticate'),
        );
    }
}

      

+2


source


Filters are mainly used to filter your URL. AccessControl is included here as well. This means that when you set the url to access any action. This filter then checks if the action is allowed for this Account or not. Also, if you establish that this action to delete a controller can only be accessed by the POST method, you cannot delete with the GET method. Those are all kinds of access .... Managed by FILTERS.

On the other hand, the Validator is used to confirm your input indicated in your wishes. As with Minimum, Maximum, Integer or not, whether it will be unique or not, whether this field is required or not. this field will be email type or not ...... and many other types of INPUT field validation ......

In one word, it can be said that " Validator is used to validate input, and filter is used to validate output "

+2


source







All Articles