Deexterity: Validation of multiselection Field with input from Vocabulary

classic Classic list List threaded Threaded
8 messages Options
andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Deexterity: Validation of multiselection Field with input from Vocabulary

Hello,

I try to validate a Dexterity multiselection field that is created like this:

category_choice = schema.List(
    title=_(u"Choose your categories"),
    value_type=schema.Choice(source=vocabCategories),
    required=True,
)


I tried to validate if this field is not empty. I wasn't successful by using @invariant with a function that raises an error message if data.category_choice is None.


@invariant
def noCategoryChoosen(data):
    if data.category_choice is None:
       raise MissingCategory(_(u"You must choose at least one category for your project"))



class MissingCategory(Invalid):
    __doc__ = _(u"You have not chosen a category for the project")


The test seemed to fail because the field has some value / data.

I tried the same with a constraint for the schema.List field with the same result.

Thanks for any hints in advance.

Regards,
Andreas
-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
c.ledermann c.ledermann
Reply | Threaded
Open this post in threaded view
|

Re: Deexterity: Validation of multiselection Field with input from Vocabulary

iirc an empty choice field returns an emty list not None

On 1 March 2015 at 19:00, Andreas Mantke <[hidden email]> wrote:

> Hello,
>
> I try to validate a Dexterity multiselection field that is created like
> this:
>
> category_choice = schema.List(
>     title=_(u"Choose your categories"),
>     value_type=schema.Choice(source=vocabCategories),
>     required=True,
> )
>
>
> I tried to validate if this field is not empty. I wasn't successful by using
> @invariant with a function that raises an error message if
> data.category_choice is None.
>
>
> @invariant
> def noCategoryChoosen(data):
>     if data.category_choice is None:
>        raise MissingCategory(_(u"You must choose at least one category for
> your project"))
>
>
>
> class MissingCategory(Invalid):
>     __doc__ = _(u"You have not chosen a category for the project")
>
>
> The test seemed to fail because the field has some value / data.
>
> I tried the same with a constraint for the schema.List field with the same
> result.
>
> Thanks for any hints in advance.
>
> Regards,
> Andreas
>
> --
> ## Developer LibreOffice
> ## Freie Office-Suite für Linux, Mac, Windows
> ## http://LibreOffice.org
> ## Support the Document Foundation (http://documentfoundation.org)
> ## Meine Seite: http://www.amantke.de
>
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website,
> sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for
> all
> things parallel software development, from weekly thought leadership blogs
> to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Plone-Users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/plone-users
>



--
Best Regards,

Christian Ledermann

London - UK
Mobile : +44 7474997517

<*)))>{

If you save the living environment, the biodiversity that we have left,
you will also automatically save the physical environment, too. But If
you only save the physical environment, you will ultimately lose both.

1) Don’t drive species to extinction

2) Don’t destroy a habitat that species rely on.

3) Don’t change the climate in ways that will result in the above.

}<(((*>

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
espen espen
Reply | Threaded
Open this post in threaded view
|

Re: Deexterity: Validation of multiselection Field with input from Vocabulary

In reply to this post by andreasma
( I dont really know if this is a good idea9, but

Could you
• set the field to be required
• Let one of the options be «Please choose»
• Set «Please choose» as default
• validate the field for not (contains) «please choose»

Espen


1. mars 2015 kl. 20:01 skrev andreasma [via Plone] <[hidden email]>:

Hello,

I try to validate a Dexterity multiselection field that is created like this:

category_choice = schema.List(
    title=_(u"Choose your categories"),
    value_type=schema.Choice(source=vocabCategories),
    required=True,
)


I tried to validate if this field is not empty. I wasn't successful by using @invariant with a function that raises an error message if data.category_choice is None.


@invariant
def noCategoryChoosen(data):
    if data.category_choice is None:
       raise MissingCategory(_(u"You must choose at least one category for your project"))



class MissingCategory(Invalid):
    __doc__ = _(u"You have not chosen a category for the project")


The test seemed to fail because the field has some value / data.

I tried the same with a constraint for the schema.List field with the same result.

Thanks for any hints in advance.

Regards,
Andreas
-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Plone-Users mailing list
<a href="x-msg://18/user/SendEmail.jtp?type=node&amp;node=7573302&amp;i=0" target="_top" rel="nofollow" link="external">[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users



If you reply to this email, your message will be added to the discussion below:
http://plone.293351.n2.nabble.com/Deexterity-Validation-of-multiselection-Field-with-input-from-Vocabulary-tp7573302.html
To start a new topic under General Questions, email [hidden email]
To unsubscribe from Plone, click here.
NAML

andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Re: Deexterity: Validation of multiselection Field with input from Vocabulary

In reply to this post by c.ledermann
Hello Christian,


Am 02.03.2015 um 10:37 schrieb Christian Ledermann:
iirc an empty choice field returns an emty list not None
thanks for your hint. That helped me a lot.
I got the issue in my validation and it works in a default Dexterity form with now:
@invariant
def noCategoryChoosen(data):
     if data.category_choice == []:
          raise MissingCategory(_(u"You had to choose at least one category for your project."))


But if I use a similar validation within a Dexterity, that contains form.fieldsets, it behaves different.

If I add a new Dexterity object I could solve one error message (validation error) after another and could save my Dexterity object at least.

But if I edit a former such object (with added validation feature now) I'm not able to save the changes (choose a value for the List-Choice-Field). The errors message stays in eternity.
Regards,
Andreas

-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
David Glick (Plone) David Glick (Plone)
Reply | Threaded
Open this post in threaded view
|

Re: Deexterity: Validation of multiselection Field with input from Vocabulary

On 3/2/15 11:25 AM, Andreas Mantke wrote:
Hello Christian,


Am 02.03.2015 um 10:37 schrieb Christian Ledermann:
iirc an empty choice field returns an emty list not None
thanks for your hint. That helped me a lot.
I got the issue in my validation and it works in a default Dexterity form with now:
@invariant
def noCategoryChoosen(data):
     if data.category_choice == []:
          raise MissingCategory(_(u"You had to choose at least one category for your project."))


But if I use a similar validation within a Dexterity, that contains form.fieldsets, it behaves different.

If I add a new Dexterity object I could solve one error message (validation error) after another and could save my Dexterity object at least.

But if I edit a former such object (with added validation feature now) I'm not able to save the changes (choose a value for the List-Choice-Field). The errors message stays in eternity.

Some more options to try:

1. set min_length=1 on the List field and zope.schema will validate it (but you won't get a custom validation message)

2. use a constraint on the field instead of an invariant on the schema

from zope.interface import Invalid

def isNotEmpty(value):
    if not value:
        raise Invalid(u'You must choose at least one category.')

category_choice = List(
    // ...,
    constraint=isNotEmpty
)

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Re: Deexterity: Validation of multiselection Field with input from Vocabulary

Hello David,

Am 05.03.2015 um 08:06 schrieb David Glick (Plone):
On 3/2/15 11:25 AM, Andreas Mantke wrote:
Hello Christian,


Am 02.03.2015 um 10:37 schrieb Christian Ledermann:
iirc an empty choice field returns an emty list not None
thanks for your hint. That helped me a lot.
I got the issue in my validation and it works in a default Dexterity form with now:
@invariant
def noCategoryChoosen(data):
     if data.category_choice == []:
          raise MissingCategory(_(u"You had to choose at least one category for your project."))


But if I use a similar validation within a Dexterity, that contains form.fieldsets, it behaves different.

If I add a new Dexterity object I could solve one error message (validation error) after another and could save my Dexterity object at least.

But if I edit a former such object (with added validation feature now) I'm not able to save the changes (choose a value for the List-Choice-Field). The errors message stays in eternity.

Some more options to try:

1. set min_length=1 on the List field and zope.schema will validate it (but you won't get a custom validation message)

2. use a constraint on the field instead of an invariant on the schema

from zope.interface import Invalid

def isNotEmpty(value):
    if not value:
        raise Invalid(u'You must choose at least one category.')

category_choice = List(
    // ...,
    constraint=isNotEmpty
)

thanks for that hint. The option with a constraint is much better. The user get the error message near to the field where his input is missing.

Regards,
Andreas
-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Re: Deexterity: Validation of multiselection Field with input from Vocabulary

Hello,

Am 05.03.2015 um 21:35 schrieb Andreas Mantke:
Hello David,

Am 05.03.2015 um 08:06 schrieb David Glick (Plone):
On 3/2/15 11:25 AM, Andreas Mantke wrote:
Hello Christian,


Am 02.03.2015 um 10:37 schrieb Christian Ledermann:
iirc an empty choice field returns an emty list not None
thanks for your hint. That helped me a lot.
I got the issue in my validation and it works in a default Dexterity form with now:
@invariant
def noCategoryChoosen(data):
     if data.category_choice == []:
          raise MissingCategory(_(u"You had to choose at least one category for your project."))


But if I use a similar validation within a Dexterity, that contains form.fieldsets, it behaves different.

If I add a new Dexterity object I could solve one error message (validation error) after another and could save my Dexterity object at least.

But if I edit a former such object (with added validation feature now) I'm not able to save the changes (choose a value for the List-Choice-Field). The errors message stays in eternity.

Some more options to try:

1. set min_length=1 on the List field and zope.schema will validate it (but you won't get a custom validation message)

2. use a constraint on the field instead of an invariant on the schema
e
from zope.interface import Invalid

def isNotEmpty(value):
    if not value:
        raise Invalid(u'You must choose at least one category.')

category_choice = List(
    // ...,
    constraint=isNotEmpty
)

thanks for that hint. The option with a constraint is much better. The user get the error message near to the field where his input is missing.

although this worked in the past it failed currently. I always get a red error message for the list-choice fields.

I used a buildout with Plone 4.3.4  and Invalid from zope.interface-3.6.7.

I tried with two other field types (schema.Text and schema.TextLine) with the same issue. I can't get Invalid/constraint satisfied. The same with a schema.Int field.

Thanks for any hint in advance.

Regards,
Andreas
-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 

------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Re: [SOLVED] Deexterity: Validation of multiselection Field with input from Vocabulary

Am 06.04.2015 um 17:19 schrieb Andreas Mantke:
Hello,

Am 05.03.2015 um 21:35 schrieb Andreas Mantke:
Hello David,

Am 05.03.2015 um 08:06 schrieb David Glick (Plone):
(...)

Some more options to try:

1. set min_length=1 on the List field and zope.schema will validate it (but you won't get a custom validation message)

2. use a constraint on the field instead of an invariant on the schema
e
from zope.interface import Invalid

def isNotEmpty(value):
    if not value:
        raise Invalid(u'You must choose at least one category.')

category_choice = List(
    // ...,
    constraint=isNotEmpty
)

thanks for that hint. The option with a constraint is much better. The user get the error message near to the field where his input is missing.

although this worked in the past it failed currently. I always get a red error message for the list-choice fields.

I used a buildout with Plone 4.3.4  and Invalid from zope.interface-3.6.7.

I tried with two other field types (schema.Text and schema.TextLine) with the same issue. I can't get Invalid/constraint satisfied. The same with a schema.Int field.

Thanks for any hint in advance.

I fixed the issue. The function needed another line at the end:

'return True'


def isNotEmpty(value):
    if not value:
        raise Invalid(u'You must choose at least one category.')
    return True


Regards,
Andreas
-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 

------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users