我在使用Form创建新的Collection实体时遇到问题。 我想用表单创建新的Collection实体,然后使用' collection_user_collections'重定向到集合页面。路由,并能够在用户的收藏列表中看到新的集合。但是当我按下表单上的提交按钮时,我得到以下错误:
var redirect_uri = "http://localhost/callback";
if(options !== undefined) {
if(options.hasOwnProperty("redirect_uri")) {
redirect_uri = options.redirect_uri;
}
}
var browserRef = window.open('https://accounts.google.com/o/oauth2/auth?client_id='
+ clientId + '&redirect_uri=' + redirect_uri
+ '&scope=' + appScope.join(" ")
+ '&prompt=select_account&response_type=token', '_blank',
'location=no');
browserRef.addEventListener("loadstart", function(event) {
if((event.url).indexOf(redirect_uri) === 0) {
browserRef.removeEventListener("exit",function(event){});
browserRef.close();
var callbackResponse = (event.url).split("#")[1];
var responseParameters = (callbackResponse).split("&");
var parameterMap = [];
for(var i = 0; i < responseParameters.length; i++) {
parameterMap[responseParameters[i].split("=")[0]] = responseParameters[i].split("=")[1];
}
if(parameterMap.access_token !== undefined && parameterMap.access_token !== null) {
deferred.resolve({ access_token: parameterMap.access_token, token_type: parameterMap.token_type, expires_in: parameterMap.expires_in });
} else {
deferred.reject("Problem authenticating");
}
}
});
browserRef.addEventListener('exit', function(event) {
deferred.reject("The sign in flow was canceled");
});
以下是我的代码:
google:
我正在使用CollectionType来构建表单:
No route found for "POST /profile/": Method Not Allowed (Allow: GET, HEAD)
这是createAction,在这里我尝试在创建实体时插入当前用户的用户名和日期。我正在使用FOSUserBundle来管理应用用户:
class Collection{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $name;
private $url;
private $type;
const STATUS_PRIVATE = 0;
const STATUS_PUBLIC = 1;
/**
* @ORM\ManyToOne(targetEntity="MyMini\UserBundle\Entity\User", inversedBy="collections")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
private $date_created;
private $date_modified;
/* getters and setters are here*/
}
表格形式:
class CollectionType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name','text')
->add('type', 'choice', array('choices' => array(
Collection::STATUS_PRIVATE => 'Private',
Collection::STATUS_PUBLIC => 'Public',
)))
->add('save', 'submit', array('label' => 'Create Collection'))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyMini\CollectionBundle\Entity\Collection'
));
}
public function getName()
{
return 'mymini_collectionbundle_collection';
}
}
答案 0 :(得分:1)
您预计收到的例外情况。您正在调用createForm
方法而不传递所有必要的参数。创建表单的正确方法是:
$this->createForm(
new CollectionType(),
$collection,
array(
'action' => $this->generateUrl('collection_create_collection') ),
'method' => 'PUT', // or 'POST'
)
);