[Django] #29205: MultiValueField ignores a required value of a sub field

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

[Django] #29205: MultiValueField ignores a required value of a sub field

Django
#29205: MultiValueField ignores a required value of a sub field
------------------------------------------+------------------------
               Reporter:  Takayuki Hirai  |          Owner:  nobody
                   Type:  Bug             |         Status:  new
              Component:  Forms           |        Version:  1.11
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 A field and a form definition:

 {{{
 #!python
 from django.forms import (
     Form,
     CharField,
     MultiValueField,
     MultiWidget,
 )


 class MF(MultiValueField):
     widget = MultiWidget

     def __init__(self):
         fields = [
             CharField(required=False),
             CharField(required=True),
         ]
         widget = self.widget(widgets=[
             f.widget
             for f in fields
         ], attrs={})
         super(MF, self).__init__(
             fields=fields,
             widget=widget,
             require_all_fields=False,
             required=False,
         )

     def compress(self, value):
         return []


 class F(Form):
     mf = MF()
 }}}

 When the form is passed empty values for both sub fields,
 {{{form.is_valid() == True}}}.
 But I expected {{{is_valid()}}} returns False, because one of the sub
 fields is set as required.

 {{{
 #!python

 f = F({
     'mf_0': '',
     'mf_1': '',
 })
 assert f.is_valid() == True  # I expect this should return False
 }}}

 On the other hand, When one of its sub field is passed a non-empty value,
 {{{form.is_valid() == False}}}

 {{{
 #!python

 f = F({
     'mf_0': ''xxx,
     'mf_1': '',
 })
 assert f.is_valid() == Flase
 }}}

 If above behavior is not expected, please fix this problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/29205>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.32fc11e2458bb9457bfcf6c976cb07d8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29205: MultiValueField ignores a required value of a sub field

Django
#29205: MultiValueField ignores a required value of a sub field
--------------------------------+--------------------------------------
     Reporter:  Takayuki Hirai  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------
Description changed by Takayuki Hirai:

Old description:

> A field and a form definition:
>
> {{{
> #!python
> from django.forms import (
>     Form,
>     CharField,
>     MultiValueField,
>     MultiWidget,
> )
>

> class MF(MultiValueField):
>     widget = MultiWidget
>
>     def __init__(self):
>         fields = [
>             CharField(required=False),
>             CharField(required=True),
>         ]
>         widget = self.widget(widgets=[
>             f.widget
>             for f in fields
>         ], attrs={})
>         super(MF, self).__init__(
>             fields=fields,
>             widget=widget,
>             require_all_fields=False,
>             required=False,
>         )
>
>     def compress(self, value):
>         return []
>

> class F(Form):
>     mf = MF()
> }}}
>
> When the form is passed empty values for both sub fields,
> {{{form.is_valid() == True}}}.
> But I expected {{{is_valid()}}} returns False, because one of the sub
> fields is set as required.
>
> {{{
> #!python
>
> f = F({
>     'mf_0': '',
>     'mf_1': '',
> })
> assert f.is_valid() == True  # I expect this should return False
> }}}
>
> On the other hand, When one of its sub field is passed a non-empty value,
> {{{form.is_valid() == False}}}
>
> {{{
> #!python
>
> f = F({
>     'mf_0': ''xxx,
>     'mf_1': '',
> })
> assert f.is_valid() == Flase
> }}}
>
> If above behavior is not expected, please fix this problem.
New description:

 A field and a form definition:

 {{{
 #!python
 from django.forms import (
     Form,
     CharField,
     MultiValueField,
     MultiWidget,
 )


 class MF(MultiValueField):
     widget = MultiWidget

     def __init__(self):
         fields = [
             CharField(required=False),
             CharField(required=True),
         ]
         widget = self.widget(widgets=[
             f.widget
             for f in fields
         ], attrs={})
         super(MF, self).__init__(
             fields=fields,
             widget=widget,
             require_all_fields=False,
             required=False,
         )

     def compress(self, value):
         return []


 class F(Form):
     mf = MF()
 }}}

 When the form is passed empty values for both sub fields,
 {{{form.is_valid() == True}}}.
 But I expected {{{is_valid()}}} returns False, because one of the sub
 fields is set as required.

 {{{
 #!python

 f = F({
     'mf_0': '',
     'mf_1': '',
 })
 assert f.is_valid() == True  # I expect this should return False
 }}}

 On the other hand, When one of its sub field is passed a non-empty value,
 {{{form.is_valid() == False}}}

 {{{
 #!python

 f = F({
     'mf_0': 'xxx',
     'mf_1': '',
 })
 assert f.is_valid() == Flase
 }}}

 If above behavior is not expected, please fix this problem.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.abc88dc69e906182f7686b318510bd4b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29205: MultiValueField ignores a required value of a sub field

Django
In reply to this post by Django
#29205: MultiValueField ignores a required value of a sub field
--------------------------------+--------------------------------------
     Reporter:  Takayuki Hirai  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------

Comment (by Tim Graham):

 Why do you pass `required=False` in `super(MF, self).__init__()`? Removing
 that seems to resolve the issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.89525992141c2a6aba04ace9288363b4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29205: MultiValueField ignores a required value of a sub field

Django
In reply to this post by Django
#29205: MultiValueField ignores a required value of a sub field
--------------------------------+--------------------------------------
     Reporter:  Takayuki Hirai  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------

Comment (by Takayuki Hirai):

 I tried to remove {{{required=False}}}, then both INPUT elements in HTML
 became {{{required}}}.

 {{{
 <tr><th><label for="id_mf_0">Mf:</label></th><td><input type="text"
 name="mf_0" required id="id_mf_0" />

 <input type="text" name="mf_1" required id="id_mf_1" /></td></tr>
 }}}

 Code:

 {{{
 #!python
 class MW(MultiWidget):
     def decompress(self, value):
         return []


 class MF(MultiValueField):
     widget = MW

     def __init__(self):
         fields = [
             CharField(required=False),
             CharField(required=True),
         ]
         widget = self.widget(widgets=[
             f.widget
             for f in fields
         ], attrs={})
         super(MF, self).__init__(
             fields=fields,
             widget=widget,
             require_all_fields=False,
             required=True,
         )

     def compress(self, value):
         return []
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.ffc7a44bd2e2e5cc1be90b7040c7bbf7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29205: MultiValueField ignores a required value of a sub field

Django
In reply to this post by Django
#29205: MultiValueField ignores a required value of a sub field
--------------------------------+------------------------------------
     Reporter:  Takayuki Hirai  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Accepted
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+------------------------------------
Changes (by Tim Graham):

 * stage:  Unreviewed => Accepted


Comment:

 If this is indeed an incorrect behavior (I'm not certain that there isn't
 some other way to achieve the use case), it might be that
 `Bound.build_widget_attrs()` incorrectly adds the required attribute for
 this case. I'm not sure what a fix would look like.

--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.c694d00d23f1c7d592cd41a3f6e42de3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29205: MultiValueField ignores a required value of a sub field

Django
In reply to this post by Django
#29205: MultiValueField ignores a required value of a sub field
--------------------------------+------------------------------------
     Reporter:  Takayuki Hirai  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Accepted
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+------------------------------------
Changes (by Herbert Fortes):

 * cc: Herbert Fortes (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.304775bbfe915d485ba81a6b9eb1dbd4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #29205: MultiValueField ignores a required value of a sub field

Django
In reply to this post by Django
#29205: MultiValueField ignores a required value of a sub field
--------------------------------+------------------------------------
     Reporter:  Takayuki Hirai  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Accepted
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+------------------------------------
Changes (by Frank Sachsenheim):

 * cc: Frank Sachsenheim (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29205#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.29c60a7728d1fb56fd18a4631ea0aa7a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.