-4

I think, that values for Hidden fields are completly ignored by Django during POST.

  1. no cleaned_data for that field
  2. No way to change it and most important:
  3. If required causes error "Enter a list of values." even if you can see that values in code: <input id="id_names" name="names" type="hidden" value="[35]" />

It could be the good solution (no man can hack hidden fields if they are ignored), but should be documented, and shouldn't be required.

Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44
  • 3
    Hidden fields hide the interface for the user. In all other ways, they behave exactly like a normal field - this includes that they may be required. Also note that you can edit the value of a hidden field with any modern browser that includes some developer tools. With that said, I'm not exactly sure what your question is. – knbk Aug 12 '16 at 13:52
  • 2
    Your problem is that `ModelMultipleChoiceField` does not work with `HiddenInput`. The field expects a list of values, e.g `[35]`, but the widget is returning a *string*, e.g `'[35]'`. The fact that the `HiddenInput` is hidden is not important, you would get the same problem with `TextInput`. You could try writing a hidden widget that works with `ModelMultipleChoiceField` (I couldn't find an existing one when I searched). Most of the time, you don't need a hidden field at all, you can handle that field in the view. However you have shown so little code in your three questions that I can't tell. – Alasdair Aug 12 '16 at 14:09
  • @knbk All you wrote is a tautology. My question is a hypothesis, that hidden field is ignored by POST, because it doesn't behave as it should be in normal HTML - I mean if the POST request would be processed by PHP it would work correctly. But I THINK that DJANGO ignores hidden value from the POST. – Tomasz Brzezina Aug 15 '16 at 18:52
  • @Alasdair In this question is nothing about ModelMultipleChoiceField. I found, that hidden field which is Required produces error 'Enter a list of values' If I remove required everything works. So I'm trying to confirm or deny this behaviour. – Tomasz Brzezina Aug 15 '16 at 19:00
  • True, you didn't explicitly mention `ModelMultipleChoiceF‌ield` in this question, but you did in the [other](http://stackoverflow.com/questions/38856775/how-to-debug-modelmultiplechoicefield) [two](http://stackoverflow.com/questions/38918343/manytomany-hidden-field-and-clean-method) questions you opened recently. As knbk says, hidden widgets behave like normal fields, Django will not process them differently. If you don't trust us, then test with `TextInput` instead of `HiddenInput` and you'll see the same behaviour. – Alasdair Aug 15 '16 at 19:29
  • If your question is "Are hidden inputs ignored?", then the answer is a firm **no**. It wouldn't make sense to ignore something you explicitly mark as form input by using an `` tag. There are plenty of ways to include data or HTML that are hidden from users and ignored by forms, this isn't one of them. – knbk Aug 15 '16 at 20:35
  • @Alasdair I think you maybe right someway -> the model is ManyToManyField not ModelMutlipleChoiceField. So, you suggest, that problem with result of hiddenInput is wrong type - how to fix it? – Tomasz Brzezina Aug 16 '16 at 09:19
  • `ManyToManyField` is the model field, `ModelMultipleChoiceField` is the form field. As I said before the solution is to write a hidden widget that works with `ModelMultipleChoice‌` (I can't help you do this). Or, rethink your approach, and stop trying to include the hidden field. – Alasdair Aug 16 '16 at 09:38
  • @Alasdair Please don't mix this question with other one. They are connected, but if I remove `ModelMultipleChoice‌​Field` the problem with hidden input persist. My HiddenInput is a widget to `ManyToManyField` And it works half way - in HTML there is value of instance of `ManyToManyField`, but POSTing returns error "Enter a list of values." connected to HiddenInput. So there's two possibilites - no value is returned or wrong type (String). This is strange behaviour for me. – Tomasz Brzezina Aug 16 '16 at 15:18
  • 1
    If you have a many-to-many field on your model, Django will automatically use `ModelMultipleChoice‌Field` for the model form. Therefore using `HiddenInput` with a `ManyToManyField` model field will not work, in the same way that it won't work with a `ModelMultipleChoice‌Field` form field. It isn't because `HiddenInput` is hidden. You will get the same error with `TextInput`. – Alasdair Aug 16 '16 at 15:29
  • @Alasdair Ok, now I understand the problem. Thank you very much – Tomasz Brzezina Aug 17 '16 at 09:14

1 Answers1

0

No, it isn't. The problem is that value of HiddenField is always string, and if the Field is other type, than result is wrong type. "Enter a list of values" means the result is other type instead of no result at all.

Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44