Validators API preview / discussion

(Mehdi Achour) #21

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.

(Vasadi-Balogh Tamás) #22
public function rules()
    return [
        'username' => [
            RuleFactory::hasLength(['min' => 3, 'max' => 255]),
            RuleFactory::unique()->against($this->userModel)->message($this->app->t('ModuleUser', 'This username has already been taken.')),
        'email' => [
            RuleFactory::unique()->against($this->userModel)->message($this->app->t('ModuleUser', 'This email address has already been taken.')),
        'password' => [

Shuold be like this?


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…

(Fabrizio Caldarelli) #24

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.