Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Prototype and injected javascript


Go to End


4 Posts   2113 Views

Avatar
Double-A-Ron

Community Member, 607 Posts

14 January 2010 at 11:09am

Hi all,

I'm working on a very stripped down version of BlackCandy on 2.3.3 to be used as a starting point for new projects. I have noticed a few things I don't think I have noticed before, and was wondering if someone could clear them up:

1. I see Prototype is included by default in any page. I thought this was stopped in favour of jQuery in 2.3.0? Yes I know how to block it, but I was curious as to the future of Prototype with SS.

2. When I include $SearchForm in my template, the following code is injected at the bottom of the source:

Behaviour.register({
	'#SearchForm_SearchForm': {
		validate : function(fromAnOnBlur) {
			initialiseForm(this, fromAnOnBlur);
			
			
			var error = hasHadFormError();
			if(!error && fromAnOnBlur) clearErrorMessage(fromAnOnBlur);
			
			return !error;
		},
		onsubmit : function() {
			if(typeof this.bypassValidation == 'undefined' || !this.bypassValidation) return this.validate();
		}
	},
	'#SearchForm_SearchForm input' : {
		initialise: function() {
			if(!this.old_onblur) this.old_onblur = function() { return true; } 
			if(!this.old_onfocus) this.old_onfocus = function() { return true; } 
		},
		onblur : function() {
			if(this.old_onblur()) {
				// Don't perform instant validation for CalendarDateField fields; it creates usability wierdness.
				if(this.parentNode.className.indexOf('calendardate') == -1 || this.value) {
					return $('SearchForm_SearchForm').validate(this);
				} else {
					return true;
				}
			}
		}
	},
	'#SearchForm_SearchForm select' : {
		initialise: function() {
			if(!this.old_onblur) this.old_onblur = function() { return true; } 
		},
		onblur : function() {
			if(this.old_onblur()) {
				return $('SearchForm_SearchForm').validate(this); 
			}
		}
	}
});

//]]>

Why is this included? The SearchForm method on Page.php is as follows:

function SearchForm() {
		$searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
		$fields = new FieldSet(
	  	new TextField("Search", "", $searchText)
	  );
		$actions = new FieldSet(
	  	new FormAction('results', 'Search')
	  );

	  return new SearchForm($this, "SearchForm", $fields, $actions);
	}

3. A quick look at the source for Silverstripe.org shows that neither prototype is included, nor is the above JS block injected, despite the search form seemingly being exactly the same.

Ta
Aaron

Avatar
Willr

Forum Moderator, 5523 Posts

14 January 2010 at 12:03pm

1: No. 2.3 still includes prototype. 2.4 will even still include prototype as its default / main library. The target for jquery rewrite is 2.5

2/3: Its not shown on the ss.org site. Because we blocked the default libraries - Requirements::block('jsparty/prototype.js'); etc on load. And to get rid of the form behaviour you have to disable JS validation on forms - Validator::set_javascript_validation_handler('none');

Avatar
Double-A-Ron

Community Member, 607 Posts

14 January 2010 at 12:48pm

Thanks for the clarification mate.

Is there any way to disable JS validation for one form? I assume disabling it globally also affects UDF which is not ideal.

Perhaps I am better off just hard coding the HTML for the search form into the template?

Avatar
Willr

Forum Moderator, 5523 Posts

14 January 2010 at 1:16pm

Actually it won't affect UDF at all since that actually turns off validation to do its own thing anyway (as it uses jquery and no validation handler currently exists for jquery it sets it to none)