getkirby / ideas

This is the backlog of ideas and feature requests from the last two years. Use our new feedback platform to post your new ideas or vote on existing ideas.
https://feedback.getkirby.com
20 stars 0 forks source link

Field plugin: support own validators #276

Open hdodov opened 5 years ago

hdodov commented 5 years ago

As far as I've seen from native fields, they can specify validations and use any of the native validators (but not custom ones). However, it's likely that a custom field would require its own validation.

My proposal is to turn the validations option in an associative array and allow developers to specify anonymous functions for validation. For example:

Kirby::plugin('me/plugin', [
  'fields' => [
    'myfield' => [
      'validations' => [
        // Allow custom validators...
        'myvalidator' => function ($value) {
          return $value === 'foobar';
        },

        // But also native ones.
        'max' => true,
        'min' => true
      ]
    ]
  ]
]);

Or perhaps add a new extension type, fieldValidators where you specify your validation functions, while the validations option still accepts only the names.

distantnative commented 5 years ago

Could you try if it works like that for you (without array keys)?

Kirby::plugin('me/plugin', [
  'fields' => [
    'myfield' => [
      'validations' => [
        // Allow custom validators...
        function ($value) {
          return $value === 'foobar';
        },

        // But also native ones.
        'max',
        'min'
      ]
    ]
  ]
]);
hdodov commented 5 years ago

Nope, it throws an error:

Function name must be a string

It's why I added them as key-value pairs in my proposal.