propelorm/Propel — Issue #283: Type Validator not working because of a cast in the mutator
Author: leandroluccerini
Created: 2012-02-06 15:16:16+00:00
State: open

Hi everybody,
i've found that if you have a column defined as type correspondig to a php primitive in the schema.xml (FLOAT, INT, DOUBLE, BOOLEAN) and you add a type validator, it doesn't work because of a cast added in the addDefaultMutator method of the PHP5ObjectBuilder.php

Example
```xml

```

This will generate the setter down below

```php
public function setPf1EurkwhSim($v)
{

if ($v !== null) {
$v = (double) $v;
}

if ($this->pf1_eurkwh_sim !== $v) {
$this->pf1_eurkwh_sim = $v;
$this->modifiedColumns[] = FarmSimulazioneoffertaEnergiaPeer::PF1_EURKWH_SIM;
}

return $this;
} // setPf1EurkwhSim()
}
```

So if you set the value using $obj->setPf1EurkwhSim("Hello") and then you call the validate() method it will return true because of the cast of the string "Hello" to a double (so = 0).

Since i wanted to catch the error in this case i have updated the addDefaultMutator() method

```php
//File: propel/generator/lib/builder/om/PHP5ObjectBuilder.php (line: 1739)

protected function addDefaultMutator(&$script, Column $col)
{
$clo = strtolower($col->getName());

$this->addMutatorOpen($script, $col);

// Perform type-casting to ensure that we can use type-sensitive
// checking in mutators.
// @modified added "&& is_numeric(\$v)" into the if
// to fix the TYPE VALIDATOR rule in the schema.xml file
if ($col->isPhpPrimitiveType()) {
$script .= "
if (\$v !== null && is_numeric(\$v)) {
\$v = (".$col->getPhpType().") \$v;
}
";
}

$script .= "
if (\$this->$clo !== \$v) {
\$this->$clo = \$v;
\$this->modifiedColumns[] = ".$this->getColumnConstant($col).";
}
";
$this->addMutatorClose($script, $col);
}
```
I hope it could be useful. If there are mistakes or known issues with this, please let me know.
Thank you.