by
KK » Sat Jul 25, 2015 7:41 pm
@Tim,
P.S. When I was playing around with testing this form, it seems like there is a hierarchy to validation. Not all errors are necessarily reported. When certain kinds of validation fail, the form stops processing and never gets to the other errors. Maybe KK can explain more clearly about the different levels of validation. I could be completely wrong about this.

In this particular case where we use databound-form to create extended-user, there is indeed an hierarchy -
1. The usual validation that is done by the form. Any errors found are reported and execution ends here.
2. If validation done in step 1 above succeeds, the k_success block executes <cms:db_persist_form /> that tries to create a native 'User' account. This 'User' object does its own validations on the name, email, password and repeat-password fields (the same as are done while creating a user from the admin-panel). Any errors found at this stage are reported.
In @GoingMarryAsap code, only the 'human' field was being validated in the point 1 above. The rest were done in point 2.
If this dichotomy is bothersome, we can do the following -
Instead of directly exposing the extended-user fields as 'bound' fields, we use plain cms:inputs in the form e.g. as follows -
- Code: Select all
DisplayName:<br />
<cms:input name='title' label='User-name' type='text' required='1' validator='title_ready|min_len=4|max_len=255' /><br />
Email Address:<br />
<cms:input name='email' label='E-Mail' type='text' required='1' validator='email' /><br />
Password:<br />
<cms:input name='password' label='Password' type='text' required='1' validator='min_len=5|max_len=64' /><br />
Repeat Password:<br />
<cms:input name='repeat_password' label='Repeat Password' type='text' required='1' validator='matches_field=password' /><br />
Captcha:<br />
<cms:input name='human' label='Captcha' required='1' type='text' validator='regex=/^Rival$/i' /><br />
<input type="submit" name="submit" value="Create account"/>
In the code above we are using only normal form fields (none is 'bound'). The important point to note here is that for the name, email, password and repeat-password fields (i.e. the fields that were originally bound)
we have attached the same validations as would be done by the 'User' object internally. This way we do all the validations via the form itself ('Point 1').
Now upon successful form submission, we pass these values explicitly to cms:db_persist_form as follows -
- Code: Select all
<cms:if k_success >
<cms:db_persist_form
...
k_page_title = frm_title
extended_user_email = frm_email
extended_user_password = frm_password
extended_user_password_repeat = frm_repeat_password
/>
...
...
This way we can have all validations, validation messages, labels etc. at one place (in the form itself). The 'point 2' validation will still occur but since we have already validated all values. it will never throw any error of its own.
For someone comparing the original sample code that accompanies extended-users, here is this version of the code -
- Code: Select all
<cms:form
masterpage=k_user_template
mode='create'
enctype='multipart/form-data'
method='post'
anchor='0'
>
<cms:if k_success >
<cms:check_spam email=frm_email />
<cms:db_persist_form
_invalidate_cache='0'
k_page_name = "<cms:random_name />"
k_publish_date = '0000-00-00 00:00:00'
k_page_title = frm_title
extended_user_email = frm_email
extended_user_password = frm_password
extended_user_password_repeat = frm_repeat_password
/>
<cms:if k_success >
<cms:send_mail from="<cms:php>echo K_EMAIL_FROM;</cms:php>" to=frm_email subject='New Account Confirmation' debug='1'>
Please click the following link to activate your account:
<cms:activation_link frm_email />
Thanks,
Website Name
</cms:send_mail>
<cms:set_flash name='success_msg' value='1' />
<cms:redirect k_page_link />
</cms:if>
</cms:if>
<cms:if k_error >
<font color='red'><cms:each k_error ><cms:show item /><br /></cms:each></font>
</cms:if>
DisplayName:<br />
<cms:input name='title' label='User-name' type='text' required='1' validator='title_ready|min_len=4|max_len=255' /><br />
Email Address:<br />
<cms:input name='email' label='E-Mail' type='text' required='1' validator='email' /><br />
Password:<br />
<cms:input name='password' label='Password' type='text' required='1' validator='min_len=5|max_len=64' /><br />
Repeat Password:<br />
<cms:input name='repeat_password' label='Repeat Password' type='text' required='1' validator='matches_field=password' /><br />
<input type="submit" name="submit" value="Create account"/>
</cms:form>
Hope it helps.