XWiki registration
- Open XWiki.Registration class in edit mode, e.g. http://localhost:8080/xwiki/bin/edit/XWiki/Registration
- Uncomment sample code for sample code, which asks for favorite color (and accepts green as valid answer):
...
## Uncomment this code to see an example of how you can easily add a field to the registration page
## NOTE: In order to save the favorite color in the "doAfterRegistration" hook, this page must be
## saved by an administrator and can not self sandboxing.
#set($sandbox = false)
#set($field =
{'name' : 'favorite_color',
'label' : 'What is your favorite color',
'params' : {
'type' : 'text',
'size' : '60'
},
'validate' : {
'mandatory' : {
'failureMessage' : $services.localization.render('core.validation.required.message')
},
'regex' : {
'pattern' : '/^green$/',
'failureMessage' : 'You are not cool enough to register here.'
},
'fieldOkayMessage' : 'You are awesome.'
},
'doAfterRegistration' : '#saveFavoriteColor()'
})
#set($discard = $fields.add($field))
## Save the user's favorite color on their user page.
#macro(saveFavoriteColor)
#set($xwikiname = $request.get('xwikiname'))
#set($userDoc = $xwiki.getDocument("$userSpace$xwikiname"))
$userDoc.setContent("$userDoc.getContent() ${xwikiname}'s favorite color is $request.get('favorite_color')!")
## The user (who is not yet logged in) can't save documents so saveWithProgrammingRights
## will save the document as long as the user who last saved this registration page has programming rights.
$userDoc.saveWithProgrammingRights("Saved favorite color from registration form.")
#end
...
## Uncomment this code to see an example of how you can easily add a field to the registration page
## NOTE: In order to save the favorite color in the "doAfterRegistration" hook, this page must be
## saved by an administrator and can not self sandboxing.
#set($sandbox = false)
#set($field =
{'name' : 'favorite_color',
'label' : 'What is your favorite color',
'params' : {
'type' : 'text',
'size' : '60'
},
'validate' : {
'mandatory' : {
'failureMessage' : $services.localization.render('core.validation.required.message')
},
'regex' : {
'pattern' : '/^green$/',
'failureMessage' : 'You are not cool enough to register here.'
},
'fieldOkayMessage' : 'You are awesome.'
},
'doAfterRegistration' : '#saveFavoriteColor()'
})
#set($discard = $fields.add($field))
## Save the user's favorite color on their user page.
#macro(saveFavoriteColor)
#set($xwikiname = $request.get('xwikiname'))
#set($userDoc = $xwiki.getDocument("$userSpace$xwikiname"))
$userDoc.setContent("$userDoc.getContent() ${xwikiname}'s favorite color is $request.get('favorite_color')!")
## The user (who is not yet logged in) can't save documents so saveWithProgrammingRights
## will save the document as long as the user who last saved this registration page has programming rights.
$userDoc.saveWithProgrammingRights("Saved favorite color from registration form.")
#end
...
- If necessary, customize this code according to your needs and preferences.
- If multi lingual messages are needed, look at Writing Internationalized XWiki Applications for more info.
Tags
XWiki
English
Created by Valdis Vītoliņš on 2013-11-09 13:24
Last modified by Valdis Vītoliņš on 2021-04-13 14:30
Created by Valdis Vītoliņš on 2013-11-09 13:24
Last modified by Valdis Vītoliņš on 2021-04-13 14:30