Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2010, 11:45 PM
razec razec is offline
 
Join Date: Nov 2009
Posts: 28
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default LiveValidation JavaScript in templates

I've created some new vBulletin powered pages on my forum and these make use of the templates. Prior to doing it this way, with vB 3.8 I was using vBAdvanced and had these new pages integrated as PHP pages. These pages include forms that users must fill out, and to help make it easier I had used LiveValidation (www.livevalidation.com) to do error checking on the fields.

I can't seem to include the JavaScript in the templates. When I do, it throws an error message when I try to save:

Quote:
The following error occurred when attempting to evaluate this template:

Parse error: syntax error, unexpected ';' in /home/user/html/includes/adminfunctions_template.php(4667) : eval()'d code on line 142

This is likely caused by a malformed conditional statement. It is highly recommended that you fix this error before continuing, but you may continue as-is if you wish.
So I put the JavaScript in a separate JS file and included it in the template. When I do it this way, the JavaScript simply doesn't work properly. I'm including it using:

Code:
<script type="text/javascript" src="form_validation.js"></script>
Which I know is valid. The problem is that when the pages load, the browser shows a JavaScript error that says the first field I reference ("CompanyName") doesn't exist ("Message: LiveValidation::initialize - No element with reference or id of 'CompanyName' exists!"). It does, though. This all works perfectly on my vB3 site. I don't know why the JavaScript doesn't seem to see the fields I'm using.

I know this is difficult without showing the templates, but can anyone think of what the problem is? I don't understand how this can work perfectly with vB3 but not work with vB4, because it certainly doesn't seem to be dependent on the vBulletin software. I suspect it's because of the way vBulletin is generating the page from the templates. I didn't use vB templates for my forms in the past, so that's the only thing that makes sense to me.

I've already included the livevalidation class script in the header template, which is the same way I did it in vB3. But for each form that I want to validate, I need to include a script like the one below. I had previously added this block just after I put the </form> tag.

Code:
	<script type="text/javascript">
		var v_CompanyName = new LiveValidation( 'CompanyName', { validMessage: vMsg, onlyOnSubmit: true } );
		v_CompanyName.add( Validate.Presence, { failureMessage: vFMsg } );

		var v_StreetAddress = new LiveValidation( 'StreetAddress', { validMessage: vMsg, onlyOnSubmit: true } );
		v_StreetAddress.add( Validate.Presence, { failureMessage: vFMsg } );

		// truncated
		
		var automaticOnSubmit = v_CompanyName.form.onsubmit;

		v_CompanyName.form.onsubmit = function()
		{
			var valid = automaticOnSubmit();
			
			if (!valid)
				alert('One or more required fields were omitted or submitted with invalid values. Please review the form and ensure that you have filled out all of the required fields. Missing fields are highlighted in red.');

			return valid;
		}
	</script>
The point of this code is that when the user clicks the Submit button, LV validates the fields then (instead of on-the-fly). If a field is missing or doesn't meet the criteria I set, the form doesn't get posted.
Reply With Quote
  #2  
Old 01-19-2010, 03:56 PM
derfelix derfelix is offline
 
Join Date: Nov 2001
Posts: 204
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

i had a similar errof for a javascript..
the problem is putting var inside a function...

to solve it put the content of the javascript
inside

HTML Code:
	<script type="text/javascript">
<vb:litteral>
		var v_CompanyName = new LiveValidation( 'CompanyName', { validMessage: vMsg, onlyOnSubmit: true } );
		v_CompanyName.add( Validate.Presence, { failureMessage: vFMsg } );

		var v_StreetAddress = new LiveValidation( 'StreetAddress', { validMessage: vMsg, onlyOnSubmit: true } );
		v_StreetAddress.add( Validate.Presence, { failureMessage: vFMsg } );

		// truncated
		
		var automaticOnSubmit = v_CompanyName.form.onsubmit;

		v_CompanyName.form.onsubmit = function()
		{
			var valid = automaticOnSubmit();
			
			if (!valid)
				alert('One or more required fields were omitted or submitted with invalid values. Please review the form and ensure that you have filled out all of the required fields. Missing fields are highlighted in red.');

			return valid;
		}
</vb:litteral>
	</script>


or other solution
declare the var valid;
outside of the function...
at the beginning of the script.. that should work also...
inside the function you then only have:
valid = automaticOnSubmit();

F-

The template parser just doesnt like the word var inside curly brackets.....
Reply With Quote
  #3  
Old 01-19-2010, 04:38 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It's <vb:literal>, not <vb:litteral>
Reply With Quote
  #4  
Old 01-19-2010, 08:53 PM
razec razec is offline
 
Join Date: Nov 2009
Posts: 28
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmm, it's still giving me the same error. This is what my template looks like now (different one, shorter than the one I was referring to earlier so I'll use this one as my example):

Code:
{vb:stylevar htmldoctype}
<html xmlns="http://www.w3.org/1999/xhtml" dir="{vb:stylevar textdirection}" lang="{vb:stylevar languagecode}" id="vbulletin_html">
	<head>
		<title>{vb:raw vboptions.bbtitle}</title>
		{vb:raw headinclude}
		<script type="text/javascript">
			<vb:literal>
				// Default valid message
				var vMsg = ' Ok.';
				var vFMsg = ' This field is required.';
				
				var v_BusinessName = new LiveValidation( 'BusinessName', { validMessage: vMsg, onlyOnSubmit: true } );
				v_BusinessName.add( Validate.Presence, { failureMessage: vFMsg } );

				var v_ContactName = new LiveValidation( 'ContactName', { validMessage: vMsg, onlyOnSubmit: true } );
				v_ContactName.add( Validate.Presence, { failureMessage: vFMsg } );

				var v_ContactPhone = new LiveValidation( 'ContactPhone', { validMessage: vMsg, onlyOnSubmit: true } );
				v_ContactPhone.add( Validate.Presence, { failureMessage: vFMsg } );
				
				var automaticOnSubmit = v_BusinessName.form.onsubmit;
				var valid;

				v_BusinessName.form.onsubmit = function()
				{
					valid = automaticOnSubmit();
					
					if (!valid)
						alert('One or more required fields were omitted or submitted with invalid values. Please review the form and ensure that you have filled out all of the required fields. Missing fields are highlighted in red.');

					return valid;
				}
			</vb:literal>
		</script>
	</head>
	<body>
		{vb:raw header}

		{vb:raw navbar}

		<br/><h2 class="blockhead">{vb:raw pagetitle}</h2>
		<div class="blockbody">
			<div class="blockrow">
				<vb:if condition="$show['member']">
					<vb:if condition="is_member_of($bbuserinfo, 12)">
						<p>Please provide your business' contact information below.</p><br/>

						<form action="companyinfo_submit.php" method="POST" name="companyinfo">
							<input type="hidden" name="securitytoken" value="{vb:raw bbuserinfo.securitytoken}"/>
							
							<table border="0" align="center" width="500px">
								<tr>
									<td align="right" valign="center"><b>Business Name:</b></td>
									<td valign="center">&nbsp;<input type="text" name="BusinessName" size="30"/></td>
								</tr>
								<tr>
									<td align="right" valign="center"><b>Website Address:</b></td>
									<td valign="center">&nbsp;<input type="text" name="Website" size="30"/></td>
								</tr>
								<tr>
									<td align="right" valign="center"><b>Contact Name:</b></td>
									<td valign="center">&nbsp;<input type="text" name="ContactName" size="30"/></td>
								</tr>
								<tr>
									<td align="right" valign="center"><b>Contact Phone:</b></td>
									<td valign="center">&nbsp;<input type="text" name="ContactPhone" size="30"/></td>
								</tr>
								<tr>
									<td align="center" valign="center" colspan="2"><input type="submit" name="cmdSubmit" value="Send"/></td>
								</tr>
							</table>
						</form>
					<vb:else />
						{vb:rawphrase not_authorized_form_err}
					</vb:if>
				<vb:else />
					{vb:rawphrase not_logged_in_form_err}
				</vb:if>
			</div>
		</div>

		{vb:raw footer}
	</body>
</html>
It keeps telling me:

Quote:
Message: LiveValidation::initialize - No element with reference or id of 'BusinessName' exists!
Line: 80
Char: 24
Code: 0
As you can see, though, BusinessName is definitely there. In case you're wondering, I've included the LiveValidation class in the headinclude template.
Reply With Quote
  #5  
Old 01-20-2010, 01:30 AM
derfelix derfelix is offline
 
Join Date: Nov 2001
Posts: 204
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by razec View Post

It keeps telling me:



As you can see, though, BusinessName is definitely there. In case you're wondering, I've included the LiveValidation class in the headinclude template.
Nope, I think BusinessName NOT there, at least NOT at the time the javascript is called...
You have it at the top..., when the html below is not rendered yet..
I personnally would do it differently..

in the form put a onsubmit="checkthisform()"

then embed all the js in a function called checkthisform()

(you would have to put the v_BusinessName.form.onsubmit = function() in a second function outside the checkthisform

F.
Reply With Quote
  #6  
Old 01-20-2010, 04:08 AM
razec razec is offline
 
Join Date: Nov 2009
Posts: 28
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I've tried putting the script immediately after the </form> tag (before the <vb:else/>) and it gives me the same error. I really can't think of what I'm missing here.
Reply With Quote
  #7  
Old 01-20-2010, 05:48 AM
derfelix derfelix is offline
 
Join Date: Nov 2001
Posts: 204
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

add it in footer after the vbulletin init...
because maybe you are using stuff that is in the vbulletin.js
and that is only available after the init in footer..
(just a thought)

i have a checkform script...
again.. it is only called AFTER the onsubmit.. (even the vars are only defined AFTER the onsubmit)
AND the onsubmit is in the form tag.. that works!!!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:04 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04086 seconds
  • Memory Usage 2,236KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (1)bbcode_html
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete