I'm creating a simple list of shop carts with users and products assigned to it. My form for new cart looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('cartName', 'text', array('label' =>'Nazwa koszyka:'))
->add('user', new UserForm(), array('data_class' => 'Zadanie\Bundle\Entity\User', 'label' => false))
->add('products','entity', array('label' => 'Wybierz produkty:', 'class' =>'Zadanie\Bundle\Entity\Product' , 'multiple' => true, 'required' => true))
->add('Zapisz', 'submit');
}
and everything is great except that i can submit the form even without selecting any product.
By far i just added "required" by jquery, but i don't like that. Can somebody explain to me why it is not working properly? :P
EDIT: Here is the code from controller:
/**
* @Route("/cart/edit/{id}",name="_edit_cart")
* @Template()
*/
public function editAction($id, Request $request)
{
$cart = $this->getDoctrine()->getRepository('ZadanieBundle:Cart')->find($id);
if($cart == null)
{
throw $this->createNotFoundException('Nie znaleziono rekordu');
}
$form = $this->createForm(new CartForm(), $cart);
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$data = $form->getData();
$em->persist($data);
$em->flush();
$this->get('session')->getFlashBag()->set('message', 'Koszyk zaktualizowano.');
return $this->redirect($this->generateUrl('_main_carts'));
}
return array('form' => $form->createView());
}
SECOND EDIT:
i found a SOLUTION, ( don't know if the best, but works :) ) so if anybody encounters that:
You have to create your validation file ( validation.yml for example) under YourBundle/Resources/config, in which you have to put information about properties. In my case it was:
Zadanie\Bundle\Entity\Cart:
properties:
cartname:
- NotBlank: ~
user:
- NotBlank: ~
constraints:
- Callback:
methods:
- [Zadanie\Bundle\Form\MyValidator, isUserValid]
and then i created MyValidator:
namespace Zadanie\Bundle\Form;
use Symfony\Component\Validator\ExecutionContextInterface;
use Zadanie\Bundle\Entity\Cart;
class MyValidator {
public static function isUserValid(Cart $cart, ExecutionContextInterface $context)
{
if(!$cart->getUser()->getName())
$context->addViolationAt('name', 'Proszę podać imię.', array(), null);
if(!$cart->getUser()->getSurname())
$context->addViolationAt('surname', 'Proszę podać nazwisko.', array(), null);
if(count($cart->getProducts()) == 0)
$context->addViolationAt('products', 'Proszę wybrać produkt.', array(), null);
}
}
@Mati, regarding your first question about how the required option works, this option only sets the required attribute in HTML5 so does not do anything server side. From the documentation
As of HTML5, many browsers can natively enforce certain validation constraints on the client side. The most common validation is activated by rendering a required attribute on fields that are required. For browsers that support HTML5, this will result in a native browser message being displayed if the user tries to submit the form with that field blank.
Regarding your solution, that will certainly work though you may want to consider relying on the built-in validators. I'm fairly sure the product count constraint can use the built-in Count Collection constraint.
来源:https://stackoverflow.com/questions/19818548/symfony2-multiple-choice-is-not-validating