Witajcie.
Postanowiłem nadpisać formularz edycji profilu danego użytkownika w SF 2.7, dlatego w utworzonym bundle'u UserBundle utworzyłem plik o takiej zawartości:
Tech/UserBundle/Form/ProfileFormType.php
namespace Tech\UserBundle\Form;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
class ProfileFormType extends BaseType
{
public function buildForm
(FormBuilderInterface
$builder, array $options) {
parent::buildForm($builder, $options);
// Add custom fields
$builder->add('name');
$builder->add('surname');
$builder->add('address');
$builder->add('postCode');
$builder->add('city');
$builder->add('phone');
$builder->add('company');
$builder->add('nip');
// $builder->add('type'); // typ konta
}
public function getParent()
{
return 'fos_user_profile';
}
public function getName()
{
return 'tech_user_profile';
}
}
Nadpisałem też widok w pliku app/Resources/FOSUserBundle/views/Profile/edit_content.html.twig:
{% trans_default_domain 'FOSUserBundle' %}
{{ form_start(form, { 'action': path('fos_user_profile_edit'), 'attr': { 'class': 'fos_user_profile_edit' } }) }}
{# {{ form_widget(form) }} #}
{{ form_row(form.username, { 'label' : 'Nazwa użytkownika' }) }}
{{ form_row(form.email, { 'label' : 'E-mail' }) }}
{{ form_row(form.name, { 'label' : 'Imię' }) }}
{{ form_row(form.surname, { 'label' : 'Nazwisko' }) }}
{{ form_row(form.address, { 'label' : 'Adres' }) }}
{{ form_row(form.postCode, { 'label' : 'Kod pocztowy' }) }}
{{ form_row(form.city, { 'label' : 'Miejscowość' }) }}
{{ form_row(form.phone, { 'label' : 'Telefon' }) }}
{{ form_row(form.company, { 'label' : 'Firma' }) }}
{{ form_row(form.nip, { 'label' : 'NIP' }) }}
{{ form_rest(form) }}
<input type="submit" value="{{ 'profile.edit.submit'|trans }}" /> {{ form_end(form) }}
routing.yml:
fos_user_profile_edit:
path: /profile/edit
defaults: { _controller: TechUserBundle:Profile:edit }
requirements:
methods: GET|POST
services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services <a href="http://symfony.com/schema/dic/services/services-1.0.xsd">" target="_blank">http://symfony.com/schema/dic/services/ser...0.xsd"></a>
<services>
<service id="tech_user.profile.form.type" class="Tech\UserBundle\Form\ProfileFormType">
<tag name="form.type" alias="tech_user_profile" />
<argument>%fos_user.model.user.class%</argument>
</service>
</services>
</container>
Zatrzymałem się przy pewnej drobnej rzeczy.
Chciałbym, aby w formularzu edycji profilu poniższe pola:
{{ form_row(form.company, { 'label' : 'Firma' }) }}
{{ form_row(form.nip, { 'label' : 'NIP' }) }}
były widoczne tylko dla typu konta o fladze 1, dla pozostałych nie wyświetlały się. W bazie danych mam kolumnę o nazwie "type" i przyjmuje ona wartości: 0 dla zwykłego użytkownika, 1 dla firmy, 2 dla hurtownika.
W jaki sposób uzyskać ww. efekt?
Ten post edytował swiezak 13.04.2016, 23:13:19