Skip to content

[Form] Problems on the UrlType when passing an array #26557

Closed
@jvahldick

Description

@jvahldick
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 4.0.x

I got some error 500 trying to pass an array to the URL form type. The error happens on FixUrlProtocolListener.

Following the FormType

$builder
    ->add('images', CollectionType::class, [
        'property_path' => 'medias',
        'entry_type' => UrlType::class,
        'allow_add' => true,
    ])
;

I am handling an API which I send a list of images based on their URLs. In the form

So, in the request, I have something like the data below:

{
  "images": [
    "https:\/\/images.pexels.com\/photos\/754444\/pexels-photo-754444.jpeg?w=1260&h=750&dpr=2&auto=compress&cs=tinysrgb",
    "https:\/\/images.pexels.com\/photos\/789382\/pexels-photo-789382.jpeg?w=1260&h=750&dpr=2&auto=compress&cs=tinysrgb"
  ]
}

Then I changed the request to see the behavior of the application with the following request:

{
  "images": [
    {
      "original_path": "https:\/\/images.pexels.com\/photos\/754444\/pexels-photo-754444.jpeg?w=1260&h=750&dpr=2&auto=compress&cs=tinysrgb",
      "path": "example\/images\/5ea4313075e0896df583054ea99ea9bc.jpg",
      "created_at": "2018-03-16T08:27:02+00:00",
      "updated_at": "2018-03-16T08:27:09+00:00",
      "id": "98cb8ba6-18d0-419d-8bbe-fe2e56f5721f"
    },
    {
      "original_path": "https:\/\/images.pexels.com\/photos\/789382\/pexels-photo-789382.jpeg?w=1260&h=750&dpr=2&auto=compress&cs=tinysrgb",
      "path": "example\/images\/4adb1413f2c31e93ad1decd2d250a1c5.jpg",
      "created_at": "2018-03-16T08:27:02+00:00",
      "updated_at": "2018-03-16T08:27:09+00:00",
      "id": "ce83ce03-d672-47eb-bc30-70fbc9622e51"
    }
  ]
}

When I try to execute the handleRequest method, I got the following result:

{
  "error": {
    "code": 500,
    "message": "Internal Server Error",
    "exception": [
      {
        "message": "Warning: preg_match() expects parameter 2 to be string, array given",
        "class": "ErrorException",
        "trace": [
          {
            "namespace": "",
            "short_class": "",
            "class": "",
            "type": "",
            "function": "",
            "file": "\/var\/www\/app\/vendor\/symfony\/form\/Extension\/Core\/EventListener\/FixUrlProtocolListener.php",
            "line": 39,
            "args": [
              
            ]
          }
        ]
      }
    ]
  }
}

I guess the Form behavior should return some property error instead 500.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions