0
votes

Is it useful to validate type of data with the Type Constraint with Doctrine in Symfony?

Indeed, when you try to put data with wrong type in the database, Symfony throws an exception with the following message:

The type of the "name" attribute must be "string", "integer" given."

Thus, there is a priori no danger of SQL injection.

Moreover, in every tutorial I have seen, there was no Type checking.

What do you think about it?

2
Sql query takes time. So better to validate data before querying. This is where it is useful.Artur Yukhatov

2 Answers

1
votes

it depends, in certain cases it makes absolute sense to use type validation constraints. but with PHP7 some of the Type validations are redundant, when you make use Scalar type declarations with strict mode on.

/**
 * @param string $email
 */
public function setEmail(string $email)
{
    $this->email = $email;
}
1
votes

Your entity object can be stored in many kind of DBs. Today it's MySQL, tomorrow for some reasons, you could choose to pass to AWS DynamoDb storage (no type assertion when inserting data). So basically, using validators on your domain entities is something, in my opinion, we all have to do.