Vytautas Vytautas
Vytautas Vytautas

Reputation: 41

How to make choose file window contain only .png and .jpeg files?

I have a buildForm method:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('photo', 'file', array('label' => false, 'required'  => false));
}

How to make choose file window show only .png and .jpeg files?

Upvotes: 1

Views: 1720

Answers (2)

Rafael Figueiredo
Rafael Figueiredo

Reputation: 347

I think this should work:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('photo', 
                  'file', 
                  array('label' => false, 
                        'required' => false, 
                        'attr' => array('accept' => 'image/jpeg,image/png')
                  )
    );
}

The HTML for that is

<input type="file" accept="image/jpg,image/png">

However that's not supported by any browser.

Upvotes: 3

Halcyon
Halcyon

Reputation: 57719

Use accept like:

<input type="file" accept=".png,.jpg,.jpeg" />

Or in your case:

$builder->add('photo', 'file', array('label' => false, 'required'  => false,
    'accept' => ".png,.jpg,.jpeg" ));

Upvotes: 5

Related Questions