Jump to:

23491 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Prototype and injected javascript

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

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

Page: 1
Go to End
Author Topic: 1680 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Prototype and injected javascript Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Prototype and injected javascript Link to this post

    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');
          

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Prototype and injected javascript Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Prototype and injected javascript Link to this post

    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)

    1680 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.