Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF2][SF] checkbox zawsze zaznaczony
wiciu010
post
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
 
Start new topic
Odpowiedzi
blahy
post
Post #2





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

Posty w temacie


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

 



RSS Aktualny czas: 8.10.2025 - 02:20