Hi,

I recently created a ticket about this:
http://trac.symfony-project.org/ticket/9295

The issue could be solved in two ways. Or we assume that all options are
passed as a string and, in the xxxValidator class (being xxx the
constraint), you check a boolean option as " $constraint->myBooleanOption
=== 'true' " or we could add the possibility to configure the type of each
option in the xxxValidator class. One way cound be:

// On my xxxValidator.php file
public function optionTypes()
{
    return array(
        'myOption1' => 'double',
        'myOption2' => 'boolean',
        'myOption3' => 'string'
    );
}

We could make the "string" type as default, so you don't have to configure
that type for string options.

With this method then, in the Constraint class, before it sets the option of
the constraint with the string value, we could call a method to cast the
value:

// Constraint.php
public function castOptionValue( $option, $value )
{
    $types = $this->optionTypes();

    if ( in_array( $option, array_keys( $types ) ) && $types[ $option ] !==
'string' )
    {
        switch ( $types[ $option ] )
        {
            case 'boolean':
                 if ( $value === 'false' )
                 {
                     $value = false;
                 }
                 else if ( $value === 'true' )
                 {
                     $value = true;
                 }
                 else
                 {
                     // Exception
                 }
            case 'integer':
                 $value = intval( $value );

                 break;
            // etc
            default:
                // Throw exception for an invalid type.

                break;
        }

    }

    return $value;
}

So, at the moment of the association of the constraint option with the
value:

$this->$option = $this->castOptionValue( $option, $value );



Of course, this is just an idea of how to implement this. Don't know still
how the options comes from the annotations / yml / xml to the constraint, so
maybe there's a much better way to handle this before the options are passed
to the constraint. But the point is to ask you guys if we should keep all
constraint options as strings or if it's better to have some types (at least
the simple ones like boolean). And if the last is preferred, how we should
implement this. I was implementing this idea but I wanted to ask you before
continue because maybe I don't know something important about Symfony
internals that makes this idea to have no sense.



Thanks!

Best regards.

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to