getkirby-v2 / toolkit

This is the deprecated toolkit for Kirby v2.
http://getkirby.com
81 stars 50 forks source link

Improved invalid helper function #205

Closed mzur closed 7 years ago

mzur commented 7 years ago

I'm currently working on v3 of the Uniform plugin. This version will be based on the kirby-form plugin which in turn makes use of the invalid helper function to validate the form fields. To this end I'd like to propose some improvements to the invalid function. This PR consists of two idependent commits:

  1. Improved 'required' validation. Now only null, '', [] or undefined variables are rejected by the required validation. Previously '0' was rejected, too.

  2. Improved error messages. Now an array of error messages can be defined for each validation rule, too. Each item of the array corresponds to one validation method so the error messages can be more helpful. Example:

    $rules = ['username' => ['required', 'alpha', 'min' => 4]];
    $messages = ['username' => [
      'The username is required.',
      'The username may only contain letters.',
      'The username must be at least 4 characters long.',
    ]];

I've also added a few tests to ensure backwards compatibility. Only the array_unique had to be removed which might result in duplicate error messages in old code.

bastianallgeier commented 7 years ago

I really like the improvements!! 👍 sorry that it took so long to merge them.