mremi / ContactBundle

Provides a contact form for a Symfony project.
27 stars 23 forks source link

Surcharger Type pour enlever un champ #43

Closed tbredillet closed 8 years ago

tbredillet commented 8 years ago

Hi remi, je I would like remove the fields "title" of your bundle in override the Type but it doesn't works and I'm blocked since few days

# app/config/config.yml
mremi_contact:
    store_data:            false
    contact_class:         Mremi\ContactBundle\Model\Contact

    form:
        type:              mb_contact
        name:              contact_form
        validation_groups: [Default]
        subject_provider:  mremi_contact.subject_provider.noop

    email:
        mailer:            mremi_contact.mailer.twig_swift
        from:              [ address: thomas@nnn.nnn]
        to:                [ address: thomas@nnn.nnn]
        template:          MremiContactBundle:Contact:email.txt.twig
// src/MB/ContactBundle/MBContactBundle

<?php

namespace MB\ContactBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MBContactBundle extends Bundle
{
    public function getParent()
    {
        return 'MremiContactBundle';
    }
}
// src/MB/ContactBundle/Form/Type/ContactType.php

<?php

namespace MB\ContactBundle\Form\Type;

use Mremi\ContactBundle\Form\Type\ContactType as ContactTypeMremi;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ContactType extends ContactTypeMremi
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->remove('title');
    }

    public function getName()
    {
        return 'mb_contact';
    }
}

Thank you for helping me !

mremi commented 8 years ago

Hi @razbounak,

Can you translate this issue and fix the format plz? Can be useful for other people.

Thx

tbredillet commented 8 years ago

It's done !

mremi commented 8 years ago

Did you check if your form type is called?

tbredillet commented 8 years ago

Where can I see that ?

mremi commented 8 years ago

With a die('ok'); in buildForm for instance :)

tbredillet commented 8 years ago

Yes, 'ok' is display, so the good Type is used for create the form but the wrong Type is used for check the data of the form

mremi commented 8 years ago

The form does not validate the data, you have to override this https://github.com/mremi/ContactBundle/blob/master/Resources/config/validation.xml, see http://symfony.com/doc/current/bundles/override.html.

tbredillet commented 8 years ago

Finally, I have add this line in my view :

    {% do form.title.setRendered %}
    <input type="hidden" id="contact_form_title_0" name="contact_form[title]" value="mr" />

I know this is not the better way but I don't achieve in override the Type and validation

mremi commented 8 years ago

Indeed, this is a wrong way, you should override the validation (and probably tweak the validation groups). I let you read the documentation to do it.