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

DataExtension + JavaScript custom validation


Go to End


3 Posts   981 Views

Avatar
eightythree

Community Member, 12 Posts

24 April 2016 at 9:51pm

Edited: 25/04/2016 2:59am

Hello,
I'd like to add an custom validation in my forms using JavaScript and DataExtension. With 'normal' fields everything works fine, but when I try to do the same thing with DataExtension, JavaScript can't see variable.

My code looks like this:

SiteConfigExtension.php:

class SiteConfigExtension extends DataExtension
{
    private static $db = array(   
      'MainQuoteModal__Message' => 'Text',
    );
}

HomePage.php Controller

  public function init() {
    parent::init();

    Requirements::javascriptTemplate($this->ThemeDir().'/js/script.js', array(
      'MainQuoteModal__Message' => $this->MainQuoteModal__Message
    ));
  }

Script.js

    var validationObj = {
        firstName: {
            identifier: 'first-name',
            rules: [{
                type: 'empty',
                prompt: '(my variable)'
            }]
        }
    };

Since I need to add 'SiteConfig' before any field name in my template I don't how to use this in my script.js

I've tried:
prompt: '$MainQuoteModal__Message'
prompt: '$SiteConfig.MainQuoteModal__Message'
prompt: '$SiteConfig+MainQuoteModal__Message'

but none of the works here. I think JS need to 'somehow' recognize "SiteConfig" but I don't actually know how to write that.

UPDATE:
I've met another problem, this custom validation works only in my HomePage and supposed to work on every page.

Any help will be appreciated.

Cheers,
Darek.

Avatar
Sphere

Community Member, 46 Posts

25 April 2016 at 1:28pm

It seems you're applying the message to be set in SiteConfig, yet in your HomePage class, you address $this.

To have it on every page, add the array to the Page init. And have it read the message from the appropriate class. In this case, SiteConfig::current_siteconfig()->FieldName

Avatar
eightythree

Community Member, 12 Posts

26 April 2016 at 4:58am

Hi Spehere,
thanks for your reply. I moved my init form HomePage.php to Page.php and I'm trying fo figure out where to put your snippet. I've tried something like this:

Page.php

class Page_Controller extends ContentController
{
    public function init()
    {
        parent::init();        
        Requirements::javascriptTemplate($this->ThemeDir().'/js/script.js', array(
            'ValidationMsg__Empty' => SiteConfig::current_site_config()->ValidationMsg__Empty
        ));
    }
}

Code is executing properly, but stll no luck with working validation on all pages. Still only form at HomePage giving proper validation message. On the other pages after submit, form just sending message, even with empty fields. Any additional help would be appreciated.

Cheers,
Darek.