<commit_before><?php
namespace app\models\forms;

use Yii;
use yii\base\Model;
use app\models\Code;

class TokenInputForm extends Model
{
    public $token;
    private $_code = false;

    public function rules()
    {
        return [
            [['token'], 'required'],
            // token is validated by validateToken()
            ['token', 'validateToken'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'token' => Yii::t('app', 'Token'),
        ];
    }

    public function validateToken($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $code = $this->getCode();

            if (!$code) {
                // no code with token found
                $this->addError($attribute, 'Incorrect token or token already used.');
            } else {
                // code with token found now check if is was already used or disabled.
                if ($code->isUsed()) {
                    $this->addError($attribute, 'Token was already used to vote.');
                }
                if (!$code->isValid()) {
                    $this->addError($attribute, 'Incorrect token or token already used.');
                }
            }

        }
    }
<commit_msg>Multilanguage support for TokenInputForm error messages.<commit_after><?php
namespace app\models\forms;

use Yii;
use yii\base\Model;
use app\models\Code;

class TokenInputForm extends Model
{
    public $token;
    private $_code = false;

    public function rules()
    {
        return [
            [['token'], 'required'],
            // token is validated by validateToken()
            ['token', 'validateToken'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'token' => Yii::t('app', 'Token'),
        ];
    }

    public function validateToken($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $code = $this->getCode();

            if (!$code) {
                // no code with token found
                $this->addError($attribute, Yii::t('app', 'Incorrect token or token already used.'));
            } else {
                // code with token found now check if is was already used or disabled.
                if ($code->isUsed()) {
                    $this->addError($attribute, Yii::t('app', 'Token was already used to vote.'));
                }
                if (!$code->isValid()) {
                    $this->addError($attribute, Yii::t('app', 'Incorrect token or token already used.'));
                }
            }

        }
    }

{'fraction_non_alphanumeric': 0.13343677269200932, 'fraction_numerical': 0.0, 'mean_word_length': 1.610323886639676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2990810', 'n_tokens_mistral': 686, 'n_tokens_neox': 668, 'n_words': 192}