I have a class Products which I use for a form and for the database – it has 3 date fields with this or similar:
/**
* @ORM\Column(name="`date`", type="date")
* @Assert\NotBlank()
* @Assert\Type("\Date")
*/
It has also 3-4 other fields which has only NotBlank(), one without any constraints and a field which is used to save the category of the product (category is another class and table in the database). It looks like this:
/**
* @ORM\ManyToOne(targetEntity="Categories")
* @ORM\JoinColumn(name="categories_id", referencedColumnName="id")
**/
private $categories;
Here is my function:
public function addAction($id, Request $request)
{
$em = $this->getDoctrine()->getEntityManager();
$products = new Products();
$products_form = $this->createForm(new ProductsType(), $products);
$category = $em->getRepository('AcmeMyBundle:Categories')->find($id);
if($request->getMethod() == 'POST')
{
$products_form->bindRequest($request);
if($products_form->isValid())
{
$products->setDomains($category);
$em->persist($products);
$em->flush();
}
At the end it redirects. The problem is that even when I add correct values in the form it says that it is not valid. To add a date a simply write a string like this ‘2012-08-09’ in the form.
When I comment
if($products_form->isValid())
Everything works fine.
Any suggestions? Please help!
EDIT:
Here is the Products class:
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Products
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Categories")
* @ORM\JoinColumn(name="categories_id", referencedColumnName="id")
**/
private $categories;
/**
* @ORM\Column(name="`date`", type="date")
* @Assert\NotBlank()
* @Assert\Type("\Date")
*/
protected $date;
/**
* @ORM\Column(name="`from`", type="date")
* @Assert\NotBlank()
* @Assert\Type("\Date")
*/
protected $from;
/**
* @ORM\Column(name="`to`", type="date")
* @Assert\NotBlank()
* @Assert\Type("\Date")
*/
protected $to;
/**
* @ORM\Column(type="decimal", scale=2)
* @Assert\NotBlank()
*/
protected $price;
/**
* @ORM\Column(type="string", length=5)
* @Assert\NotBlank()
*/
protected $currency;
/**
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
*/
protected $paymentid;
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
protected $notes;
And here I create the form:
class ProductsType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('date', 'date', array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd'));
$builder->add('from', 'date', array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd'));
$builder->add('to', 'date', array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd'));
$builder->add('price', 'text');
$builder->add('currency', 'choice', array(
'choices' => array(
'empty_value'=>'--- Choose ---', 'USD'=>'USD', 'HKD'=>'HKD', 'EUR'=>'EUR', 'BGN'=>'BGN')));
$builder->add('paymentid', 'text');
$builder->add('notes', 'text', array(
'required' => false));
}
public function getName()
{
return 'payments';
}
I think in your form ,make sure that csrf token is there.
Add the following code at the below of the form widget inside the tag.
Hope this helps you.