Issue 1:
If you validate a form field with, for instance, Symfony\Component\Validator\Constraints\Length and submit the form with the value of the field as an array, the LengthValidator will throw an UnexpectedTypeException, generating a 500 response, instead of adding a violation to the form field.

I tried to work around this issue by adding a Type constraint, but the Length constraint seems to be validated first. I also believe that most of the constraints that are affected (see the bottom of this issue) should implicitly validate the type as well. Having to add Constraints\Type("File") to an already existing Constraints\File seems counter-intuitive to me.

To reproduce this, consider the following form type:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\Extension\Core\Type as FormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints;

class TestType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('test', FormType\TextType::class, array(
'constraints' => array(
new Constraints\NotBlank(),
new Constraints\Length(5),
),
))
;
}
}

If you submit the following request, you will receive an error page:

POST / HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: 19

test[test][foo]=bar

This is due to the following piece of code which is present in Symfony\Component\Validator\Constraints\LengthValidator:

public function validate($value, Constraint $constraint)
{
// [...]
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
// [...]
}

Similar checks seem to be present in the following constraints:

Country, Currency, DateTime, Date, Email, File, Iban, Ip, Isbn, Issn, Language, Length, Locale, Regex, Time, Url, Uuid

Issue 2:
I have this example, which i use in a small json-api where i use the form component to handle requests and validation.

->add('account', 'text', [
'constraints' => [
new NotBlank(),
new Type('string'),
new Regex("/^XX\d+$/i")
]
])

I have a form field for an accountname. The format of this name is XX1234.

The validation above works great if the data being validated is a string. If i pass in an array instead of the string i will get an exception that is originating from here:

https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Validator/Constraints/RegexValidator.php#L43

In my opinion the validation should just fail here instead of throwing an exception. I pass an array in, and try to run an regexp on it. It seems obvious to me that something is wrong with the input data then and the validation should fail.

Other validation constraints seems to work this way. Look at this example:

->add('disksize', 'text', [
'constraints' => [
new NotBlank(),
new Type('numeric'),
new Range(['min' => 10, 'max' => 200])
]
])

This works. I get an validation error and no exceptions. Other validations contraints, like Length seems to have the same problem as Regex

I think it would be great if all constraints just failed if they were passed an array if they cant handle an array. Or is there something i am overseeing?

Is there some way i can work around this to make it fail validation if i pass in an array?