Category: Validator
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:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
Access HTTP_USER_AGENT/HTTP_* in Agavi
If you try to do retrive the users browsers by accessing $_SERVER['HTTP_USER_AGENT'] in agavi, you'll have no luck.
It's common in agavi, that all user input must be validated. Thus for instance $_SERVER['SERVER_NAME'] can not be manipulated by the user and is therefor accessible by using the $_SERVER global.
But $_SERVER['HTTP_USER_AGENT'] (and all HTTP_* variables) 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 HTTP_USER_AGENT of the accessing user:
2
3
<argument>USER_AGENT</argument>
</validator>
As you can see, I highlighted the source="headers" 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 HTTP_USER_AGENT you have to validate USER_AGENT.
As result a
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:
Since we are using AgaviWebRequestDataHolder ($rd), we may also use:
to access it. Thanks to Wombert for pointing that out.
How to validate an array with agavi
If you want to validate an array as parameter for an agavi action, you have to validate each of the array entries on its own.
Assuming we have an array called options with the value as an array:
2
3
4
'title'=>'string text',
'subtitle'=>'string subtext'
)
The following snippet validates it (every element is string - sorry for the laziness):
2
3
4
5
6
<arguments base="options[]">
<argument>title</argument>
<argument>subtitle</argument>
</arguments>
</validator>
Thanks to v-dogg and wombert for the hints on how to make that.


