Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF2][SF] checkbox zawsze zaznaczony
wiciu010
post 19.08.2015, 20:10:38
Post #1





Grupa: Zarejestrowani
Postów: 195
Pomógł: 0
Dołączył: 29.04.2007

Ostrzeżenie: (0%)
-----


Cześć,

Mam checkbox w formularzu stworzony:
  1. $builder->add('is_active', 'checkbox', array('value'=>true ));


W kontrolerze ustawiam formularz:
  1. $form = $this->createForm(new AddClientType());
  2. $form->setData($user);


W modelu User, który mapuje tabelę User mam:
  1. /**
  2.   * Set is_active
  3.   *
  4.   * @param boolean $isActive
  5.   * @return User
  6.   */
  7. public function setIsActive($isActive)
  8. {
  9. $this->is_active = $isActive;
  10.  
  11. return $this;
  12. }
  13.  
  14. /**
  15.   * Get is_active
  16.   *
  17.   * @return boolean
  18.   */
  19. public function getIsActive()
  20. {
  21. return $this->is_active;
  22. }


W widoku twig mam:
Kod
{{ form_widget(generalForm.is_active, {'id':'is_active'}) }}


W bazie pole jest jako tinyint i przyjmuje wartość 1 albo 0. Niestety na stronie checkbox zawsze jest zaznaczony. Nawet jeśli w bazie jest ustawione 0. Jak to poprawić?
Go to the top of the page
+Quote Post
pyro
post 19.08.2015, 20:53:25
Post #2





Grupa: Zarejestrowani
Postów: 2 148
Pomógł: 230
Dołączył: 26.03.2008

Ostrzeżenie: (0%)
-----


Usunąć array('value'=>true ) biggrin.gif ?


--------------------
ET LINGUA EIUS LOQUETUR IUDICIUM
Go to the top of the page
+Quote Post
wiciu010
post 19.08.2015, 21:29:20
Post #3





Grupa: Zarejestrowani
Postów: 195
Pomógł: 0
Dołączył: 29.04.2007

Ostrzeżenie: (0%)
-----


Cytat(pyro @ 19.08.2015, 21:53:25 ) *
Usunąć array('value'=>true ) biggrin.gif ?


Nie pomaga
Go to the top of the page
+Quote Post
aras785
post 20.08.2015, 08:26:10
Post #4





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Cześć.

Może tak (?):

  1. $builder->add('is_active', 'checkbox', array('data'=>true ));


Go to the top of the page
+Quote Post
blahy
post 20.08.2015, 22:28:08
Post #5





Grupa: Zarejestrowani
Postów: 82
Pomógł: 22
Dołączył: 20.07.2010

Ostrzeżenie: (0%)
-----


Pokaz cale AddClientType

Powinienes miec data_class ustawione na User,
tworzyc formularz w kontrollerze przez $this->createForm(new AddClientType(), $user);
a w type $builder->add('is_active', 'checkbox');

jezeli dalej checbkox jest zaznaczony to dla pewnosci w kontrollerze dumpnij $user->getIsActive() czy jest false.

no i czy generalForm to ten sam form co AddClientType?
Go to the top of the page
+Quote Post
wiciu010
post 21.08.2015, 08:12:05
Post #6





Grupa: Zarejestrowani
Postów: 195
Pomógł: 0
Dołączył: 29.04.2007

Ostrzeżenie: (0%)
-----


Cytat(blahy @ 20.08.2015, 23:28:08 ) *
Powinienes miec data_class ustawione na User,


Hm tego mogę nie miec. Jak to powinno być ustawiona?

Cytat(blahy @ 20.08.2015, 23:28:08 ) *
tworzyc formularz w kontrollerze przez $this->createForm(new AddClientType(), $user);


W ten sposób mi nie binduje danych z bazy, dlatego, aby uzupełnić formularz danymi musiałem zrobić:

  1. $form = $this->createForm(new AddClientType());
  2. $form->setData($user);

Go to the top of the page
+Quote Post
blahy
post 21.08.2015, 20:23:00
Post #7





Grupa: Zarejestrowani
Postów: 82
Pomógł: 22
Dołączył: 20.07.2010

Ostrzeżenie: (0%)
-----


Nie wyslales tego o co prosilem, pokaz cale omawiane encje.
Fakt, ze nie binduje CI danych wskazuje na to, ze masz blad w innym miejscu.

Ponizej pliki do odtworzenia Twojego problemu na czystej instalacji - oczywiscie checkbox dziala prawidlowo:

User:
  1. <?php
  2.  
  3. namespace AppBundle\Entity;
  4.  
  5. class User {
  6. protected $is_active;
  7.  
  8. public function setIsActive($isActive)
  9. {
  10. $this->is_active = $isActive;
  11.  
  12. return $this;
  13. }
  14.  
  15. public function getIsActive()
  16. {
  17. return $this->is_active;
  18. }
  19. }


Type:
  1. <?php
  2.  
  3. namespace AppBundle\Form;
  4.  
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7.  
  8. class AddClientType extends AbstractType
  9. {
  10. public function buildForm(FormBuilderInterface $builder, array $options)
  11. {
  12. $builder->add('is_active', 'checkbox');
  13. }
  14.  
  15. public function getName()
  16. {
  17. return 'app_add_client';
  18. }
  19. }


Controller:
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use AppBundle\Entity\User;
  6. use AppBundle\Form\AddClientType;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use Symfony\Component\HttpFoundation\Request;
  10.  
  11. class DefaultController extends Controller
  12. {
  13. /**
  14.   * @Route("/", name="homepage")
  15.   */
  16. public function indexAction(Request $request)
  17. {
  18. $activeUser = new User();
  19. $activeUser->setIsActive(true);
  20.  
  21. $inactiveUser = new User();
  22. $inactiveUser->setIsActive(false);
  23.  
  24. $activeUserForm = $this->createForm(new AddClientType(), $activeUser);
  25. $inactiveUserForm = $this->createForm(new AddClientType(), $inactiveUser);
  26.  
  27. return $this->render('default/index.html.twig', array(
  28. 'activeUserForm' => $activeUserForm->createView(),
  29. 'inactiveUserForm' => $inactiveUserForm->createView(),
  30. ));
  31. }
  32. }


Twig:
  1. Active user:<br />
  2. {{ activeUserForm.vars.data.isActive }}
  3. {{ form(activeUserForm) }}
  4. <br />
  5. <br />
  6. Inactive user:<br />
  7. {{ activeUserForm.vars.data.isActive }}
  8. {{ form(inactiveUserForm) }}


No i wynikowy HTML:
  1. Active user:<br />
  2. 1
  3. <form name="app_add_client" method="post" action=""><div id="app_add_client"><div> <label for="app_add_client_is_active" class="required">Is active</label><input type="checkbox" id="app_add_client_is_active" name="app_add_client[is_active]" required="required" value="1" checked="checked" /></div><input type="hidden" id="app_add_client__token" name="app_add_client[_token]" value="QnPaNwvp2c05di8Bq6orChXYup5ZHQTvNsLRM45bmzk" /></div></form>
  4. <br />
  5. <br />
  6. Inactive user:<br />
  7. 1
  8. <form name="app_add_client" method="post" action=""><div id="app_add_client"><div> <label for="app_add_client_is_active" class="required">Is active</label><input type="checkbox" id="app_add_client_is_active" name="app_add_client[is_active]" required="required" value="1" /></div><input type="hidden" id="app_add_client__token" name="app_add_client[_token]" value="QnPaNwvp2c05di8Bq6orChXYup5ZHQTvNsLRM45bmzk" /></div></form>


Dla usera aktywnego checkbox jest checked, dla nieaktywnego nie.
Nie trzeba robic nic wiecej tylko do build form przekazac encje, i dodac pole typu checkbox.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.06.2025 - 05:22