dracoblue.net

Agavi Age Validator

Hello,

a friend and I were wondering how would be best practice to implement an age validator in agavi. Since agavi already has the AgaviDateTimeValidator, my initial idea was, to extend that one and just re-interpret the min/max-parameters.

But a

minimum age of 18 would result into a max date of 1991-02-08. And a maximum age of 20 would result into a min date of 1989-02-08. So I had to swap the throwError's for max and min.

The resulting code:

class AgeValidator extends AgaviDateTimeValidator {
    protected function validate() {
        $min = $this->getParameter('min',null);
        $max = $this->getParameter('max',null);
        if (null !== $min) {
            $min_date = new DateTime(($min*(-1)).' years');
            $this->removeParameter('min');
            $this->setParameter('max',$min_date->format('Y-m-d'));
        }
        if (null !== $max) {
            $max_date = new DateTime(($max*(-1)).' years');
            if (null !== $min) $this->removeParameter('max'); // remove only, if _min_ was not set
            $this->setParameter('min',$max_date->format('Y-m-d'));
        }
        return parent::validate();
    }
    protected function throwError($index = null, $affectedArgument = null, $argumentsRelative = false, $setAffected = false)
    {
        if ($index == 'max' || $index == 'min') {
            // Swap those, since we faked them for the default behaviour
            $index = ($index == 'max' ? 'min' : 'max');
        }
        return parent::throwError($index, $affectedArgument, $argumentsRelative, $setAffected);
    }
}

Do you got any other ideas how to solve that in a gentle way?

  • Draco

In agavi, open source, php, validator by DracoBlue @ 08 Feb 2009 | 211 Words

Access HTTP_USER_AGENT/HTTP_* in Agavi

If you try to do retrive the users browsers by accessing $SERVER['HTTPUSER_AGENT'] in agavi, you'll have no luck.

It's common in agavi, that all user input must be validated. Thus for instance $SERVER['SERVERNAME'] can not be manipulated by the user and is therefor accessible by using the $_SERVER global.

But $_SERVER'HTTPUSERAGENT' are transfered by the browser and pretend to be valid and normalized. Therefor you need to validate them with a validator.

A sample validator, for retrieving the HTTPUSERAGENT of the accessing user:

<validator class="string" [strong]source="headers"[/strong]>
    <argument>USER_AGENT</argument>
</validator>

As you can see, I highlighted the [em]source="headers"[/em] part of the code. The reason is, that the AgaviWebRequest fills all received data (post with files and data, get parameters, cookies and headers) into different parts of the request data.

Since the user supplied data in the header all starts with a HTTP, it got stripped. So if you want to validate [em]HTTPUSERAGENT[/em] you have to validate [em]USERAGENT[/em].
As result a

$rd->getParameter('USER_AGENT')

will just fail, because the USER_AGENT is not send in the POST/GET-Parameters, but send as header. So correct access on that value is:

$rd->get('headers','USER_AGENT')

Since we are using AgaviWebRequestDataHolder ($rd), we may also use:

$rd->getHeader('User-Agent');

to access it. Thanks to

Wombert for pointing that out.

In agavi, open source, php, validator by DracoBlue @ 07 Feb 2009 | 231 Words

Registration closed

Hello,

while updating the site I have to switch of the registration. It will be available again in next days. Sorry for the inconvience!

  • Draco

In dracoblue.net by DracoBlue @ 07 Feb 2009 | 27 Words

htmlspecialchars_decode for Mootools

This is a short function for Mootools decoding a string containing html entities to text.

function htmlspecialchars_decode(text)
{
    var stub_object = new Element('span',{ 'html':text });
    var ret_val = stub_object.get('text');
    delete stub_object;
    return ret_val;
}

Remember that you usually have no use for that function, because every element of mootools has a .get('text') function, which does the same.

In javascript, mootools by DracoBlue @ 06 Feb 2009 | 62 Words

Hello Agavi-World! (Removing the default route)

In the previous step we

created our agavi project. Now we'll add some life!

Like defined in the previous step, the default action is Default.Index. As we just want to make a simple Hello World for that page, we head over to tambo/app/modules/Default/templates/IndexSuccess.php and change it's content to

<?php
    echo 'Hello World!';
?>

But if we visit http://localhost/ws/tambo/pub/ we still see the agavi logo! Open app/config/routing.xml and remove the 5th and 6th line stating:

<!-- this shows the welcome page after creating a project … ->
&lt;route pattern=”” module=”%actions.default_module%” … />

If we head over to http://localhost/ws/tambo/pub/ now, we see the brand new "Hello World!". Not so amazing :(. Don't worry, you just saw the rain drop on top of the tip of the ice berg.

In agavi, open source, php, tambo by DracoBlue @ 05 Feb 2009 | 140 Words

Page 29 - Page 30 - Page 31