[xwiki-devs] How to validate forms in XWiki editor
Hi again, We are trying to figure out how to validate the form field values in XWiki. For example, if we have a form like this: <html> <form name="feedback" action="" method="post" onSubmit="return checkform()"> <input type="text" name="name"> <input type="text" name="email" value="xyz"> <textarea name="feed"></textarea> <input type="submit" value="Submit" /> </form> how do we check that none of the fields are left blank, valid characters etc... 1) Can we use Javascript? If yes, is there a special macro to embed javascript in xwiki editor? 2)I guess this question goes back to the $msg construct. Is there a msg that I can display if any fields have wrong values? Also, when I tried: <html> <form name="feedback" action="" method="post" onSubmit="return checkform()"> <input type="text" name="name"> <input type="text" name="email" value="xyz"> <textarea name="feed"></textarea> <input type="submit" value="Submit" /> # set($name = $request.getParameter("name")) # set($email= $request.getParameter("email")) # set($feed= $request.getParameter("feed")) </form> The name is $name the velocity statements just printed as is on the page..the # being interpreted as 1, 2, 3. Why did this happen? Thanks
Hi Kamna, We are trying to figure out how to validate the form field values in XWiki.
For example, if we have a form like this:
<html> <form name="feedback" action="" method="post" onSubmit="return checkform()"> <input type="text" name="name"> <input type="text" name="email" value="xyz"> <textarea name="feed"></textarea> <input type="submit" value="Submit" /> </form>
how do we check that none of the fields are left blank, valid characters etc...
1) Can we use Javascript? If yes, is there a special macro to embed javascript in xwiki editor?
2)I guess this question goes back to the $msg construct. Is there a msg that I can display if any fields have wrong values?
You can use stuff such as #if ($request.name !="" && $request.email !="" ) do action #else $msg.error #end
Also, when I tried: <html> <form name="feedback" action="" method="post" onSubmit="return checkform()"> <input type="text" name="name"> <input type="text" name="email" value="xyz"> <textarea name="feed"></textarea> <input type="submit" value="Submit" /> # set($name = $request.getParameter("name")) # set($email= $request.getParameter("email")) # set($feed= $request.getParameter("feed")) </form> The name is $name
the velocity statements just printed as is on the page..the # being interpreted as 1, 2, 3. Why did this happen?
That's because XWiki recognizes # hello # world as a numbered list and it will render 1. hello 2. world You need to write #set($a = $b) instead of # set($a = $b) -> there's one space too many on your line ;-) Guillaume
participants (2)
-
Guillaume Lerouge -
Kamna Jain