Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error reproducing demo "You cannot define a sequence item when in a mapping"

Tags:

forms

php

symfony

I am getting an error when trying to reproduce a demo that Symfony gives. You can find it here. http://symfony.com/doc/current/book/forms.html#book-form-creating-form-classes

I can get the form to work just fine when I include the form inside of the controller but when I make the form its own class I end up getting an error that says.

You cannot define a sequence item when in a mapping 500 Internal Server Error - ParseException

Log returns:

CRITICAL - Uncaught PHP Exception Symfony\Component\Yaml\Exception\ParseException: "You cannot define a sequence item when in a mapping" at /vagrant/vendor/symfony/symfony/src/Symfony/Component/Yaml/Parser.php line 81

I can't seem to find where the issue lies.

Task.php File:

    <?php

namespace Acme\TaskBundle\Entity;

class Task
{
    protected $task;

    protected $dueDate;

    public function getTask()
    {
        return $this->task;
    }
    public function setTask($task)
    {
        $this->task = $task;
    }

    public function getDueDate()
    {
        return $this->dueDate;
    }
    public function setDueDate(\DateTime $dueDate = null)
    {
        $this->dueDate = $dueDate;
    }
}

DefaultController.php:

<?php
namespace Acme\TaskBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\TaskBundle\Entity\Task;
use Symfony\Component\HttpFoundation\Request;
use Acme\TaskBundle\Form\Type\TaskType;

class DefaultController extends Controller
{
    public function newAction(Request $request)
    {
        // create a task and give it some dummy data for this example
        $task = new Task();

        $form = $this->createForm(new TaskType(), $task);

        $form->handleRequest($request);

        if ($form->isValid()) {
            // perform some action, such as saving the task to the database

            return $this->redirect($this->generateUrl('task_success'));
        }

        return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

TaskType.php:

<?php

// src/Acme/TaskBundle/Form/Type/TaskType.php
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('task')
            ->add('dueDate', null, array('mapped' => false))
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'task';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Task',
        ));
    }
}

Let me know if you need anything else. I have tried this setup in multiple files. It has to be something small. Its right off of Symfony's site.


Edit


The only YML file I have is the Validation file i used form their tutorial. validation.yml file

# Acme/TaskBundle/Resources/config/validation.yml
Acme\TaskBundle\Entity\Task:
    properties:
        task:
            - NotBlank: ~
        dueDate:
            - NotBlank: ~
            - Type: \DateTime

Could the problem be that I don't have a yml file that defines an array?

like image 777
B Rad Avatar asked Oct 03 '22 23:10

B Rad


1 Answers

Do you have something in one your yml files like this...

stuff:
    thing1: one      // mapping
    thing2: two      // mapping
    thing3: three    // mapping
    - four           // sequence

From my guess the error is saying that you can't mix your yaml "mapping" and "sequence" in the same array statement.

so it would need to be either...

stuff:
    thing1: one
    thing2: two
    thing3: 
        - four

or

stuff:
    thing1: one
    thing2: two
    thing3: three
    thing4: four

depending on what type of array you were trying to create

like image 118
qooplmao Avatar answered Oct 11 '22 21:10

qooplmao