Validators API preview / discussion

Yes indeed. I was thinking of a “yii2 compat” extension for those kind of things, that would make the migration path from 2 to 3 easier for things that changed a lot.

Anyway, main point is: new syntax vs old should not be a blocker.

2 Likes
public function rules()
{
    return [
        'username' => [
            RuleFactory::required(),
            RuleFactory::hasLength(['min' => 3, 'max' => 255]),
            RuleFactory::matchRegularExpression()->regexp('/[a-zA-Z]+/'),
            RuleFactory::unique()->against($this->userModel)->message($this->app->t('ModuleUser', 'This username has already been taken.')),
        ],
        'email' => [
            RuleFactory::required(),
            RuleFactory::email(),
            RuleFactory::unique()->against($this->userModel)->message($this->app->t('ModuleUser', 'This email address has already been taken.')),
        ],
        'password' => [
            RuleFactory::required()->skipOnEmpty($this->module->accountGeneratingPassword),
            RuleFactory::hasLength()->min(6)->max(72),
        ]
    ];
}

Shuold be like this?

2 Likes

Not sure about this. As others have pointed out, the existing syntax is a lot simpler and cleaner. Also, I wanted to try and avoid referring to a certain “rival” framework cough Laravel cough… but take a look at how they implement validation and compare that with what is being proposed here…

I’m agree with you, array syntax always covered all my projects needed.

I was thinking to a helper to support new syntax and keeping compatibility with array syntax, for example:

function rules() 
{
    return RulesHelper::fromArray([
        ['username', 'required'],
        ['username', 'string', 'min' => 3, 'max' => 255],
        ['username', 'matches', 'reg' => $this->userModel::$usernameRegexp],
        ['username', 'unique', 'against' => $this->userModel, 'message' => $this->app->t('ModuleUser', 'This username has already been taken.')],
	]);
}

Anyway, the feeling is that leaving array syntax in more cases, we are loosing code readability.

3 Likes