Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Class for comparing other fields #146

Closed
rsandimbefore opened this issue Sep 23, 2016 · 1 comment
Closed

Rule Class for comparing other fields #146

rsandimbefore opened this issue Sep 23, 2016 · 1 comment
Labels

Comments

@rsandimbefore
Copy link

What I would create

Hi, I would like to create another rule for reuse logic instead coding same logic in callbacks approach. I made success coding simple logics (using the manual to extending Particle), but when I've tried to create one rule named greaterThanDate($param), where $param is the name of field that I suppose do compare it. It not works.

What is expected

<?php

namespace B4\Validators\Rules;

use Particle\Validator\Rule;

class GreaterThanDate extends Rule
{
    const NOT_GREATER = 'CpfRule::NOT_GREATER';

    protected $messageTemplates = [
        self::NOT_GREATER => 'Data {{ name }} não é maior que data {{ field }}.',
    ];

    private $dateField;

    public function __construct($dateField) {
        $this->dateField = $dateField;
    }

    public function validate($value) {

        if ($value > $this->values[$this->dateField]) {
            return true;
        }
        return $this->error(self::NOT_GREATER);
    }

    protected function getMessageParameters()
    {
        return array_merge(parent::getMessageParameters(), [
            'field' => $this->dateField,
        ]);
    }
}

As you can see $this->values[$this->dateField] should be the file accessed to obtain this information, but $this->values seems to be null. In Rule class, has a protected attribute named $values that should store this information. Am I right?

@rick-nu
Copy link

rick-nu commented Oct 15, 2016

Hello @rsandimbefore, sorry for the late reply, I lost track of this issue.

To answer your question, on the abstract Rule class, there is an attribute $values, but when I check the code, only in the Rule\Callback class that value is actually filled (here). We could change that for all classes by filling the $values attribute here. That explains why your $values attribute was null in your custom rule.

I hope this answered your question, and I hope the answer is not too late. If you have any more questions feel free to reply to this ticket or open a new one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants