Skip to content

Instantly share code, notes, and snippets.

@slint
Created March 21, 2019 15:03
Show Gist options
  • Save slint/81b2be2a9fa78b20e6c186f130192348 to your computer and use it in GitHub Desktop.
Save slint/81b2be2a9fa78b20e6c186f130192348 to your computer and use it in GitHub Desktop.
nested record example
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://zenodo.org/schemas/records/record-v1.0.0.json",
"properties": {
"formats": {
"type": "array",
"items": {
"type": "object",
"properties": {
"width": {
"type": "number"
},
"height": {
"type": "number"
},
"thickness": {
"type": "number"
}
},
"required": [
"width", "height"
]
},
"minLength": 1
}
}
}
from invenio_records.api import Record
data = {
"$schema": "http://my-site.org/schemas/records/record-v1.0.0.json",
"formats": [
{'width': 10, 'height': 5},
{'width': 12, 'height': 5},
{'width': 13, 'height': 5},
{'width': 20, 'height': 5},
]
}
rec = Record.create(data)
# if not valid you get ValidationException
@BAA5809
Copy link

BAA5809 commented Apr 8, 2019

this solved it:

         {% for subformfield in form.format %} 
                       {{ subformfield.form.height.label }}:{{ subformfield.form.height(size=6)}} cm,
                      {{ subformfield.form.width.label }}:{{ subformfield.form.width(size=6) }} cm,
                      {{ subformfield.form.thickness.label }}:{{subformfield.form.thickness(size=6) }} cm
          		{{ subformfield.form.hidden_tag() }}
         {% endfor %}

NB: form.hidden_tag() creates automatically a csrf_token! (how am I supposed to know.. :D).
and since the ID of the hidden field is also important, I had to create it inside the loop.

this way:

  • hidden field is created
  • it has the correct ID (when adding another set of fields it will surely increase)
  • and it has a csrf_token value

my output now:

<label for="format-0-height">height</label>:<input id="format-0-height" name="format-0-height" size="6" type="text" value=""> cm,
<label for="format-0-width">width</label>:<input id="format-0-width" name="format-0-width" size="6" type="text" value=""> cm,
<label for="format-0-thickness">thickness</label>:<input id="format-0-thickness" name="format-0-thickness" size="6" type="text" value=""> cm
<input id="format-0-csrf_token" name="format-0-csrf_token" type="hidden"  
value="IjY5M2RmOTkzOGFmNGMyYWIzMDRlYWZhMGRkZjM2NGQwYzZmY2RmNDYi.XKtKXQ.yBspzlO14p4ylp0mooU2pk_IEH4">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment