Can someone help me how to make a form for adding object from another page? I know how to add the object in groovy or velocity but I don’t know how to generate the form with all fields from class with minimum effort. I believe there is some simple way how to generate form like is on template page when creating new object. I would like to avoid reading all options for select from static list etc.
I think something like this could do the job but it doesn’t work:
{{velocity}}
#set($class = $xwiki.getClass("Construction sites.PrinterClass"))
#set($obj = $doc.getObject('Construction sites.PrinterTemplate'))
#foreach($prop in $class.properties)
$prop.prettyName
$doc.display($prop.name, 'edit', $obj)
#end
{{/velocity}}
Hi,
You could check https://www.xwiki.org/xwiki/bin/view/FAQ/How%20can%20I%20create%20a%20new%20page%20based%20on%20a%20form
The idea is to create a template page containing the xobject and then to redirect to the edit mode, passing the template as parameter. This will display a form view.
Hope it helps
Unfortunately I don’t have a clue how to do it.
After a few hours I ended with code below that renders the form but I don’t have an idea:
- how to use it to save the data because I don’t see the form elements in DOM
- how to get it working without collapsible div
- how to display all inputs of the form in one line
Maybe it’s really easier to create whole the form manually without some nice features like suggestion.
{{velocity}}
{{html clean="false"}}
#set($discard = $xwiki.jsfx.use('js/xwiki/editors/dataeditors.js', true))
#set($discard = $xwiki.ssfx.use('js/xwiki/editors/dataeditors.css', true))
#set($class = 'Construction sites.ManpowerClass')
#set($mydoc = $xwiki.getDocument("Construction sites.ManpowerTemplate"))
<div class="xform">
<div id="xclass_Construction_sites.PrinterClass" class="xclass collapsable">
<div id="xclass_${escapetool.xml($class)}_content" class="xclass-content">
<div>
#set($xredirect = $escapetool.url(${doc.getURL('edit', "template=$!{request.template}&title=$!{request.title}&parent=$!{request.parent}")}))
<div id="add_xobject_${escapetool.xml($class)}" class="add_xobject">
<div id="add_xobject_${escapetool.xml($class)}" class="add_xobject" style="display: block;">
<div id="add_xobject_${escapetool.xml($class)}_title" class="add_xobject-title" style="display: block;">
<a href="$doc.getURL('edit', "xpage=editobject&xaction=addObject&form_token=$!{services.csrf.token}&classname=$escapetool.url(${class})&xredirect=${xredirect}")" class="xobject-add-control" title="$services.localization.render('core.editors.object.add.label')">$services.localization.render('core.editors.object.add.label')</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{/html}}
{{/velocity}}
I finally found how to do the job. Below is an example how to generate all form fields with minium effort.
#set ($xdoc=$xwiki.getDocument('Construction sites.ManpowerTemplate'))
#set ($xobject = $xdoc.getObject('Construction sites.ManpowerClass'))
#set ($xclass = $xobject.xWikiClass)
#set ($discard = $doc.use($xobject))
#foreach ($property in $xclass.properties)
$property.name
$doc.display($property.name, 'edit', $xobject)
#end