Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hi Guys,

I want to embed the multiple entities into a single Symfony2 Form and I am following following document from Symfony

https://symfony.com/doc/2.8/form/form_collections.html

1: User

2: UserProfile

So I have a Form that has default data_class is User but I need few fields from UserProfile entity as well. So I did

           ->add('profile', CollectionType::class, array(
			    'entry_type' => UserProfileType::class,
			    'entry_options' => array('label' => false)
			    //'by_reference'=> false,
			    //'allow_add'=> true,
			    //'allow_delete'=> true
		     ))

In ContactType.php (a form class where the data_class is User)

And then in view I am trying to get the field from UserProfile like this

                    {{ form_label(formLocationContact.profile.title, '', {'label_attr': {'class': 'control-label'}}) }}
                    {{ form_widget(formLocationContact.profile.title, {'attr': {'class': 'form-control'}}) }}
                    {{ form_errors(formLocationContact.profile.title) }}

But I am getting the issue

   Neither the property "title" nor one of the methods "title()", "gettitle()"/"istitle()" or "__call()" exist and have public access in class 
 "Symfony\Component\Form\FormView".

Can anyone help me in this please what is wrong here.

Thanks

You must be logged in to vote

Replies: 7 comments · 1 reply

Comment options

CollectionType is used when you have an array of entities. You might use it if a User can have multiple UserProfiles. Which seems unlikely. I am assuming it is a one to one relation. In which case you simply pass an array of data to your form:

    $data = ['user' => $user, 'profile' => $userProfile];
    $form = $this->createFormBuilder($data)
            ->add('user', UserType::class)
            ->add('profile', UserProfileType::class)
You must be logged in to vote
1 reply
@connecttosunil
Comment options

Hi @ahundiak my J1LocationContactType.php

<?php

namespace AppBundle\Form;

use AppBundle\Entity\J1UserProfile;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormError;
use Doctrine\ORM\EntityRepository;

class J1LocationContactType extends AbstractType {

   private $contactType;
   private $locationId;

  /**
   * @param $contactType
   * @param $locationId
   */
    public function __construct($contactType, $locationId) {
       $this->contactType = $contactType;
       $this->locationId = $locationId;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
     public function buildForm(FormBuilderInterface $builder, array $options) {

    /** @noinspection FormTypeAsClassConstant */
    $builder
		
	   ->add('profile', UserProfileType::class)         
              ->add('forename')
              ->add('surname')
              ->add('email', 'email',  array('required' => false ))
              ->add('save', 'submit', array('label' => 'Submit','attr' => [
                  'class' => 'btn btn-default g-round-50 btn-yellow-2 margin-contact',
              ]))                
    ;     

      //$builder->addEventListener(FormEvents::POST_SET_DATA, $myExtraFieldValidator, 1);

    }

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\User'
    ));
}

/**
 * @return string
 */
public function getName() {
    return 'appbundle_j1locationcontact';
}

}

Can you please suggest what changes need in this, and yes user and profile having oneToOne relation, I am using Symfony2.8

Comment options

Does your UserProfileType have a property called 'title' and is the data class set to J1UserProfile? Perhaps you could post it as well. It looks like your UserType should work.

By the way the constructor for J1LocationContactType looks very suspicious. If your code still has new J1LocationContactType() in it then it will no longer work in 2.8.

You must be logged in to vote
0 replies
Comment options

Yes UserProfileType does not having title added in class but the entity data class in UserProfileType type that is J1UserProfile entity has the title field.

My UserProfileType.php is

<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;

class UserProfileType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('roleids', 'hidden', array('mapped' => false))
        ->add('profileImageId', 'hidden')
        ->add('position', 'text', array(
            'required' => false,
        ))
        ->add('department', 'choice', array(
            'choice_list' => new ChoiceList(
                array('None','Enquiries', 'Acquisitions','Marketing'),
                array('None','Enquiries', 'Acquisitions','Marketing')
            )))
        ->add('mostRecentEnjoyedFilm', 'text', array(
            'required' => false,
        ))
        ->add('mostRecentDisappointedFilm', 'text', array(
            'required' => false,
        ))
        ->add('currentFavouriteTvShow', 'text', array(
            'required' => false,
        ))
        ->add('strangeFact', 'text', array(
            'required' => false,
        ))
        ->add('favoriteCollectiveExclusive1')
        /*->add('favoriteCollectiveExclusive2', 'text', array(
            'required' => false,
        ))
        ->add('favoriteCollectiveExclusive3', 'text', array(
            'required' => false,
        ))*/
        ->add('favouriteLocationAndWhy', 'text', array(
            'required' => false,
        ))
        ->add('favoriteShootWorkedDate', 'text', array(
            'required' => false,
        ))
        ->add('favoriteShootWorkedDate', 'text', array(
            'required' => false,
        ))
        ->add('favoriteShootWorkedReason', 'text', array(
            'required' => false,
        ))
        ->add('bucketListShoot1', 'text', array(
            'required' => false,
        ))
        ->add('bucketListShoot2', 'text', array(
            'required' => false,
        ))
        ->add('bucketListShoot3', 'text', array(
            'required' => false,
        ))
        ->add('landline', 'text', array(
            'required' => false,
        ))
        ->add('mobileNumber', 'text', array(
            'required' => false,
        ));

    $builder->add('user', new UserEditType());
  }

   /**
    * Configures the options for this type.
    *
    * @param OptionsResolver $resolver The resolver for the options.
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
    'data_class' => 'AppBundle\Entity\J1UserProfile',
    'cascade_validation' => true
    ));
}

   /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return 'appbundle_userprofile';
    }
}
You must be logged in to vote
0 replies
Comment options

My controller is

   if ($contactId > 0) {
		$entityLocationContact = null;
		/* @var $entityLocationContactAssociation J1LocationContactAssociation */
		$entityLocationContactAssociation = $em->getRepository('AppBundle:J1LocationContactAssociation')->find($contactId);
		$locationContactId = $entityLocationContactAssociation->getj1LocationContact()->getId();
	} else {
		$entityLocationContact = new User();
	}

      $formLocationContact = $this->createForm(new J1LocationContactType($contactTypeLocationContact, $id), 
      $entityLocationContact,
  array('action' => $this->generateUrl('location_edit', array('id' => $id, 'attributeUrl' => 'contact-' . $contactId)), 'method' => 'POST'));
You must be logged in to vote
0 replies
Comment options

I might be blind but I am not seeing a add('title' in your UserProfileType so it is not surprising that formLocationContact.profile.title is generating an error. Is this a legacy project you inherited with no Symfony background? Are you trying to upgrade it by more or less randomly making changes?

Having $builder->add('user', new UserEditType()); in UserProfileType is also quite bizarre.

And as previously mentioned, new J1LocationContactType($contactTypeLocationContact, $id) no longer works in 2.8.

I think you might need to stop and just sort of think about what you are really trying to accomplish.

You must be logged in to vote
0 replies
Comment options

Hi @ahundiak in UserProfileType I do not have the add('title') because its not needed there as its present in J1UserProfile entity.

But Even I am trying to access the fields that having add method in UserProfileType
->add('mobileNumber', 'text', array(
'required' => false,
));

So if I am trying to access mobileNumber it also having the same issue. So if I am doing this

     formLocationContact.profile.mobileNumber

in view it still says same error.

You must be logged in to vote
0 replies
Comment options

This is the point where you should definitely stop and maybe work through some of the simple form examples in the docs. Make sure you are looking at the correct version in the docs. Once you understand what is wrong with the statement UserProfileType I do not have the add('title') because its not needed there as its present in J1UserProfile entity then you might be able to make some progress.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.