Rich Ajax Application Framework
Code Snippets

Code Snippets


This page will be updated from time to time with new code snippets, so stay tuned for further updates. In order to use the example shown below you will need to download the Raxan PDI Framework.

Using Custom Validators

How to create custom validators



    <?php

    require_once("raxan/pdi/gateway.php");

    // add custom phone and money validators
    RichDataSanitizer::addDataValidator('Phone', '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/'); // using regex
    RichDataSanitizer::addDataValidator('ZipCode','validate_ZipCode'); // using callback
    function validate_ZipCode($value){    // validate zip code
        return preg_match('/^(\d{5}-\d{4})|(\d{5})$/',$value);
    }

    class MyPage extends RichWebPage {

        protected $frm;
        protected $updateFormOnPostback = true; // redisplay form values

        protected function _load() {
            // load the html page
            $this->source('views/update-form.html');

            // bind to form submit event
            $this->frm = $this['form']->bind('submit', '.form_submit');
        }

        protected function form_submit($e) {    // event callback
            $msg = array();
            $request = $this->clientRequest();

            // validate user input
            if (!$request->isPhone('phone')) $msg[] ='Please enter a valid phone number (format: 123-123-1234).';
            if (!$request->isZipCode('zipcode')) $msg[] ='Please enter a valid US Zip code.';

            if (count($msg)>0) {
                $msg = '<strong>'.implode('<br />',$msg).'</strong>';
                $this['body']->prepend($msg);
            }
        }

    }

    RichWebPage::Init('MyPage');
    ?>

    
Top of page

Validating Form inputs

How to validate data submited via a web form



    <?php

    require_once("raxan/pdi/gateway.php");

    class MyPage extends RichWebPage {

        protected function _load() {
            // load the html page
            $this->source('views/signup-form.html');

            // bind to the form submit event
            $this->frm = $this['form']->bind('submit', '.form_submit');
        }

        protected function form_submit($e) {    // event callback
            $msg = array();
            $data = $this->clientRequest();
            $pwd = $data->value('password'); $cpwd = $data->value('cpassword');

            // validate user input
            if (!$data->value('name')) $msg[] ='Please enter your user name.';
            if (!$data->isEmail('email')) $msg[] ='Please enter a valid email adress.';
            if (!$pwd) $msg[] ='Please enter a valid password.';
            else if ($pwd!=$cpwd) $msg[] ='Password typed mismatched.';
            if (!$data->isUrl('website')) $msg[] ='Please enter a valid website.';

            if (count($msg)>0) {
                $msg = '<strong>'.implode('<br />',$msg).'</strong>';
                $this['body']->prepend($msg);
            }

    }

    RichWebPage::Init('MyPage');
    ?>

    
Top of page

Selecting the <a> tags on a web page

Use this snippet to change to color of the links on a web page



    <?php

    require_once("raxan/pdi/gateway.php");

    class MyPage extends RichWebPage {

        protected function _load() {
            // load the html page
            $this->source('views/my-web-page.html');
            // change the color of the links
            $this['a']->css('color','green');
        }
    }

    RichWebPage::Init('MyPage');
    ?>

    
Top of page

Finding external Hyperlinks on a web page

Find all the extneral http:// links on page



    <?php

    require_once("raxan/pdi/gateway.php");

    class MyPage extends RichWebPage {

        protected function _load() {
            // load the html page
            $this->source('views/my-web-page.html');
            // find external links
            $this['a[href~="http://"]']
                ->css('color','purple') // change the color to purple
                ->css('border-bottom','1px dotted red'); // add a border
        }
    }

    RichWebPage::Init('MyPage');
    ?>

    
Top of page

Zebra Stripes

Add alternate colors to your html tables



    <?php

    require_once("raxan/pdi/gateway.php");

    class MyPage extends RichWebPage {

        protected function _load() {
            // load the html page
            $this->source('views/my-web-page.html');
            // find even table rows
            $this['table tr:even']
                ->css('background','#eee'); // add a background color
        }
    }

    RichWebPage::Init('MyPage');
    ?>

    
Top of page

Loading an XML document

How to load an XML document using the source() method



    <?php

    require_once("raxan/pdi/gateway.php");

    class MyPage extends RichWebPage {

        protected function _load() {
            // load the XML document
            $this->source('data/bills.xml', 'xml');
            // find all cable bills
            $bills = $this['bill[type="cable"]'];

            echo $bills->length." Cable Bill(s) found.";
        }
    }

    RichWebPage::Init('MyPage');
    ?>

    

Sample bills.xml file:


    <bills>
        <bill type="cable" date="1/10/2009" amount="20.00" paid="yes" />
        <bill type="internet" date="2/1/2009" amount="30.00" paid="yes" />
        <bill type="phone" date="2/1/2009" amount="40.00" paid="yes" />
        <bill type="cable" date="3/10/2009" amount="20.00" paid="no" />
    </bills>
    
Top of page

How to create a new web Page

There are two ways you can create a webpage.


    <?php

    require_once("raxan/pdi/gateway.php");

    $page = new RichWebPage();
    $page->content("Hello world");

    $page->reply(); // send page to client

    ?>
    
You can also extend the RichWebPage class

    <?php

    require_once("raxan/pdi/gateway.php");

    class MyPage extends RichWebPage {
        protected fuction _load() {
            $this->content("Hello world");
        }
    }

    $page = new MyPage();
    $page->reply(); // sends page to client

    ?>
    
Top of page

How to load external html views


    <?php

    require_once("raxan/pdi/gateway.php");

    $view = 'views/contact-form.html';
    $page = new RichWebPage($view);

    //... code here

    $page->reply(); // sends page to client

    ?>
    

You can also use the source() method


    $page = new RichWebPage();
    $page->source($view);
    
Top of page