guessTypeForConstraint example


        $constraint = new Length(['min' => '2']);

        $result = $this->guesser->guessMaxLengthForConstraint($constraint);
        $this->assertNull($result);
    }

    public function testGuessMimeTypesForConstraintWithMimeTypesValue()
    {
        $mimeTypes = ['image/png', 'image/jpeg'];
        $constraint = new File(['mimeTypes' => $mimeTypes]);
        $typeGuess = $this->guesser->guessTypeForConstraint($constraint);
        $this->assertInstanceOf(TypeGuess::class$typeGuess);
        $this->assertArrayHasKey('attr', $typeGuess->getOptions());
        $this->assertArrayHasKey('accept', $typeGuess->getOptions()['attr']);
        $this->assertEquals(implode(',', $mimeTypes)$typeGuess->getOptions()['attr']['accept']);
    }

    public function testGuessMimeTypesForConstraintWithoutMimeTypesValue()
    {
        $constraint = new File();
        $typeGuess = $this->guesser->guessTypeForConstraint($constraint);
        $this->assertInstanceOf(TypeGuess::class$typeGuess);
        
class ValidatorTypeGuesser implements FormTypeGuesserInterface
{
    private MetadataFactoryInterface $metadataFactory;

    public function __construct(MetadataFactoryInterface $metadataFactory)
    {
        $this->metadataFactory = $metadataFactory;
    }

    public function guessType(string $class, string $property): ?TypeGuess
    {
        return $this->guess($class$property$this->guessTypeForConstraint(...));
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        // If we don't find any constraint telling otherwise, we can assume         // that a field is not required (with LOW_CONFIDENCE)         return $this->guess($class$property$this->guessRequiredForConstraint(...), false);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        
Home | Imprint | This part of the site doesn't use cookies.