[ckan-dev] Custom Validator Question

Stefan Oderbolz stefan.oderbolz at liip.ch
Mon Jan 4 16:30:26 UTC 2016


Hi Pedro,

you must implement the IValidators interface in order to use your own
custom validator, so this would work like that in your code:

class ExtrafieldsPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
    p.implements(p.IDatasetForm)
    p.implements(p.IConfigurer)    p.implements(p.IValidators)

    def get_validators(self):
        return { 'cloud_coverage_val': cloud_coverage_val }


Here is the corresponding documentation:
http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html#ckan.plugins.interfaces.IValidators

Hope this helps.

- Stefan


On Mon, Jan 4, 2016 at 5:05 PM, Pedro G Silva <pedro.gracio at deimos.com.pt>
wrote:

> Hi
>
> Im trying to construct my own validator using
> http://docs.ckan.org/en/latest/extensions/adding-custom-fields.html#custom-validators
>  trying to do no. 5. a callable object taking two parameters
> validator(value, context). When trying to use it I get an error:
> UnknownValidator: Validator `cloud_coverage_val` does not exist
>
> Any ideas why? I also tried option no1 wuth no success.
> I have the following code:
>
> import ckan.plugins as p
> import ckan.plugins.toolkit as tk
>
> from ckan.plugins.toolkit import Invalid
> def cloud_coverage_val(value, context):
>     if not value.isdigit():
>         raise Invalid("Invalid cloud coverage value, must be number between 0.0 and 100.0")
>     return float(value)
>
> class ExtrafieldsPlugin(p.SingletonPlugin, tk.DefaultDatasetForm):
>     p.implements(p.IDatasetForm)
>     #########
>     p.implements(p.IConfigurer)
>
>     # IConfigurer
>     def update_config(self, config_):
>         tk.add_template_directory(config_, 'templates')
>         tk.add_public_directory(config_, 'public')
>         tk.add_resource('fanstatic', 'extrafields')
>
>
> *[OMITTED CODE]*
>
>     def show_package_schema(self):
>         schema = super(ExtrafieldsPlugin, self).show_package_schema()
>
>         # Add our custom_text field to the dataset schema.
>         # schema.update({
>         #     'CloudCover': [tk.get_converter('convert_from_extras'),
>         #                     tk.get_validator('ignore_missing')]
>         # })
>
>         schema.update({
>                 'cloud_cover' : [ tk.get_converter('convert_from_extras'),
>                                  tk.get_validator('ignore_missing'),
>                                   tk.get_validator('cloud_coverage_val')]
>             })
>
>         schema['resources'].update({
>                 'cloud_cover_resource' : [ tk.get_validator('ignore_missing') ]
>             })
>         return schema
>
> --
>
>
> [image: DEIMOS Engenharia, SA] <http://www.deimos.pt>
> *Pedro Gracio Silva*
> Aerospace Systems / Production Center
>
> *Av. D. Joao II, Lt 1.17.01, 10 • 1998-023 Lisboa, Portugal*
> Ph: +351 21 893 3020 • Fax: +351 21 896 9099
>
>
> _______________________________________________
> ckan-dev mailing list
> ckan-dev at lists.okfn.org
> https://lists.okfn.org/mailman/listinfo/ckan-dev
> Unsubscribe: https://lists.okfn.org/mailman/options/ckan-dev
>
>


-- 
Liip AG  // Limmatstrasse 183 //  CH-8005 Zürich
Tel +41 43 500 39 80 // GnuPG 0x7B588C67 // www.liip.ch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20160104/ab56b265/attachment-0003.html>


More information about the ckan-dev mailing list