felipe.zkn
felipe.zkn

Reputation: 2060

Customizing Yii's rule param

I have the following rule:

array('sites_conhecimento_regiao,facilidade_conhecimento_regiao', 'verificarOpcoesDoUsoDeInternetConhecimentoDaRegiao', 'campo'=>'{attribute}'),

And the function it calls to run the validation:

public function verificarOpcoesDoUsoDeInternetConhecimentoDaRegiao($attribute, $params) {
    switch ($params['campo']) {
        case 'sites_conhecimento_regiao':
            $mensagem = 'Informe os sites que utilizou para a sua pesquisa.';
            break;
        case 'facilidade_conhecimento_regiao':
            $mensagem = 'Informe se teve facilidade ao obter as informações.';
            break;
    }

    if (isset($this->conhecimento_regiao)) {
        if (($this->$attribute === '') && ($this->conhecimento_regiao['conhecimento_regiao_internet'] === '1')) {
            $this->addError($attribute, $mensagem);
        }
    }
}

I did it this way to reuse the condition, the core of the validation, for those two fields. Now I wish to verify for which field Yii is calling the function. I tried it using $params, but it returns the string {attribute} instead of replace it by the name of the attribute, just like it does when setting a message in a rule.

How can I achieve my objective?

--

UPDATE: Although I have solved my problem (see answer below), if someone still want to solve the problem of passing metadata through the parameters of a rule, I leave the question opened for this purpose.

Upvotes: 1

Views: 203

Answers (1)

felipe.zkn
felipe.zkn

Reputation: 2060

switch ($attribute) {

An obvious solution...

Upvotes: 1

Related Questions