Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Registration Form with Validation

Posted by Praveen Kumar on July 9, 2008

Here i am going to describe how can we make registration form in Plone.

In plone, we have default registration form named join_form. you can find the join_form and may customize it and save it into your custom folder.

after customize the join_form, you will get default fields like username,email,password, and confirm passowrd..etc.

here i added two more fields security question and answer which is required field.

so here are my join_form.cpt

<html xmlns=”http://www.w3.org/1999/xhtml&#8221; xml:lang=”en”
lang=”en”
metal:use-macro=”here/main_template/macros/master”
i18n:domain=”plone”>

<head>
<metal:block fill-slot=”top_slot”
tal:define=”dummy python:request.set(‘disable_border’,1)” />
</head>

<body>

<div metal:fill-slot=”main”
tal:define=”errors options/state/getErrors;”>

<h1 i18n:translate=”heading_registration_form”>Registration Form</h1>

<form action=””
method=”post”
class=”enableUnloadProtection”
tal:define=”allowEnterPassword site_properties/validate_email|nothing;”
tal:attributes=”action template_id” >

<fieldset>

<legend i18n:translate=”legend_personal_details”>
Personal Details
</legend>

<input type=”hidden” name=”came_from” value=””
tal:condition=”request/came_from|nothing”
tal:attributes=”value request/came_from” />
<input type=”hidden” name=”last_visit:date” value=”ZopeTime”
tal:attributes=”value here/ZopeTime” />
<input type=”hidden” name=”prev_visit:date” value=”ZopeTime”
tal:attributes=”value here/ZopeTime” />
<input type=”hidden” name=”came_from_prefs” value=””
tal:attributes=”value request/came_from_prefs|nothing” />

<!–             User name                –>
<div class=”field”
tal:define=”error errors/username| nothing;
username request/username | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

<label for=”username”
i18n:translate=”label_user_name”>User Name</label>

<span class=”fieldRequired” title=”Required”
i18n:attributes=”title title_required;”
i18n:translate=”label_required”>(Required)</span>

<div i18n:translate=”help_user_name_creation_casesensitive”
class=”formHelp”>
Enter a user name, usually something like
‘jsmith’. No spaces or special characters.
Usernames and passwords are case sensitive,
make sure the caps lock key is not enabled.
This is the name used to log in.
</div>

<div tal:content=”error”>Validation error output</div>

<input type=”text”
name=”username”
id=”username”
size=”30″
value=”username”
tabindex=””
tal:attributes=”value username;
tabindex tabindex/next;”
/>
</div>
<!–                Password      –>
<div class=”field”
tal:condition=”not: allowEnterPassword”
tal:define=”error errors/password | nothing;
password request/password | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

<label for=”password”
i18n:translate=”label_password”>Password</label>
<span class=”fieldRequired” title=”Required”
i18n:attributes=”title title_required;”
i18n:translate=”label_required”>(Required)</span>

<div class=”formHelp” i18n:translate=”help_password_creation”>
Minimum 5 characters.
</div>

<div tal:content=”error”>Validation error output</div>

<input type=”password”
name=”password”
id=”password”
size=”10″
value=””
tabindex=””
tal:attributes=”tabindex tabindex/next;”
/>
</div>
<!–             Confirm Password               –>
<div class=”field”
tal:condition=”not: allowEnterPassword”
tal:define=”error errors/password_confirm | nothing;
password_confirm request/password_confirm | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

<label for=”password_confirm”
i18n:translate=”label_confirm_password”>Confirm password</label>

<span class=”fieldRequired” title=”Required”
i18n:attributes=”title title_required;”
i18n:translate=”label_required”>(Required)</span>

<div class=”formHelp” i18n:translate=”help_confirm_password”>
Re-enter the password. Make sure the passwords are identical.
</div>

<div tal:content=”error”>Validation error output</div>

<input type=”password”
name=”password_confirm”
id=”password_confirm”
size=”10″
value=””
tabindex=””
tal:attributes=”tabindex tabindex/next;”
/>
</div>
<!–             Security Question                –>

<div class=”field”
tal:define=”error errors/question| nothing;
question request/ | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

<label for=”question”
i18n:translate=”label_question”>Security Question</label>
<div tal:content=”error”>Validation error output</div>

<select name=”question”>
<option value=”1″>What was the name of your first school?</option>
<option value=”2″>Where did you meet your spouse?</option>
<option value=”3″ selected=”selected”>Who was your childhood hero?</option>
<option value=”4″>What is your favourite past time?</option>
</select>
</div>
<!–             Security answer                –>
<div class=”field”
tal:define=”error errors/answer| nothing;
answer request/answer | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

<label for=”answer”
i18n:translate=”label_answer”>Answer</label>

<span class=”fieldRequired” title=”Required”
i18n:attributes=”title title_required;”
i18n:translate=”label_required”>(Required)</span>

<div i18n:translate=”help_answer_creation_casesensitive”
class=”formHelp”>
Enter your answer, which will help you if you forget your id/password
</div>

<div tal:content=”error”>Validation error output</div>

<input type=”text”
name=”answer”
id=”answer”
size=”30″
value=”answer”
tabindex=””
tal:attributes=”value answer;
tabindex tabindex/next;”
/>
</div>
<!–                 Email         –>

<div class=”field”
tal:define=”error errors/email | nothing;
email request/email | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

<label for=”email”
i18n:translate=”label_email”>E-mail</label>

<span class=”fieldRequired” title=”Required”
i18n:attributes=”title title_required;”
i18n:translate=”label_required”>(Required)</span>

<div class=”formHelp” i18n:translate=”help_email_creation”>
Enter an email address. This is necessary in case the password
is lost. We respect your privacy, and will not give the address
away to any third parties or expose it anywhere.
</div>

<div tal:content=”error”>Validation error output</div>

<input type=”text”
name=”email”
id=”email”
size=”30″
tabindex=””
tal:attributes=”value email;
tabindex tabindex/next;”
/>
</div>
<!–                 Send a mail with password      –>

<div class=”field”
tal:condition=”not: allowEnterPassword”
tal:define=”mail_me request/mail_me|nothing”>

<input type=”checkbox”
class=”noborder”
name=”mail_me”
size=”30″
id=”cb_mailme”
tabindex=””
tal:attributes=”checked python:test(mail_me, ‘checked’, None);
tabindex tabindex/next;”
/>
<label for=”cb_mailme” i18n:translate=”label_mail_password”>Send a mail with the password</label>
</div>
<!–               end                –>

<div class=”field”
tal:condition=”allowEnterPassword”>

<div class=”formHelp” i18n:translate=”label_password_change_mail”>
A URL will be generated and e-mailed to you; follow the link
to reach a page where you can change your password and
complete the registration process.
</div>
</div>

<div class=”formControls”>
<input class=”context”
type=”submit”
tabindex=””
name=”form.button.Register”
value=”Register”
i18n:attributes=”value label_register;”
tal:attributes=”tabindex tabindex/next;” />
</div>

</fieldset>

<input type=”hidden” name=”form.submitted” value=”1″ />

</form>
</div>
</body>
</html>

once you test this page and if you leave the field username, it gives you an error ‘This field is required’ because you have defined

tal:define=”error errors/username| nothing;
username request/username | nothing;”
tal:attributes=”class python:test(error, ‘field error’, ‘field’)”>

which call the python script code in file name join_form_validate

if you leave the field ‘answer’ it does not give you error cause you have not yet written anything for ‘answer’ field in join_form_validate.

Find the join_form_validate and customize it and save it in same folder custom.

now you to pass ‘answer’ filed in Parameter List like answer=”

here are my join_form_validate.py file

from Products.CMFPlone import PloneMessageFactory as _

reg_tool=context.portal_registration
def missing(field):
state.setError(field, _(u’This field is required, please provide some information.’), ‘input_required’)
def nomatch(field):
state.setError(field, _(u’Passwords do not match.’), ‘nomatch_password’)
def minlimit(field):
state.setError(field, _(u’Passwords must contain at least 5 letters.’), ‘min_password’)
def notallowed(field):
state.setError(field, _(u’This username is reserved. Please choose a different name.’), ‘not_allowed’)

if not username:
missing(‘username’)
if not email:
missing(’email’)
if username and username == context.portal_url.getPortalObject().getId():
notallowed(‘username’)

if not answer:

missing(‘answer’)


properties = context.portal_properties.site_properties
if not properties.validate_email:
if password!=password_confirm:
nomatch(‘password’)
nomatch(‘password_confirm’)

if not password:
missing(‘password’)
if not password_confirm:
missing(‘password_confirm’)

if not state.getError(‘password’) and len(password) < 5:
minlimit(‘password’)
minlimit(‘password_confirm’)

if not state.getError(‘username’) and not reg_tool.isMemberIdAllowed(username):
state.setError(‘username’,
_(u’The login name you selected is already in use or is not valid. Please choose another.’),
‘username_invalid’ )

if state.getErrors():
context.plone_utils.addPortalMessage(_(u’Please correct the indicated errors.’))
return state.set(status=’failure’)
else:
context.plone_utils.addPortalMessage(_(u’You have been registered.’))
return state
and now if you test this page, it will give you error for your added field ‘answer’ too.

Here my intention is to give you simple example that how you may customize the cpt and validate.

I hope you will like it.

If you want to leave any message or any suggestion, please do share. i am also ready to hear criticism. Thank you

Advertisements

2 Responses to “Registration Form with Validation”

  1. Hello,
    how to display dynamic content in join_form.cpt?
    For example list of choices that are gathered from DB

  2. Hi, could you please be make me more clear, which database you are using. you may get more idea from given below link
    where i am displaying the dynamic content in personalize form. my request you to read each and every line of below link.

    ZOPE MySQL

    and then let me know if you stuck some where.

    Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: