Sonata Media Bundle Custom Provider 未映射请求数据
Sonata Media Bundle Custom Provider not mapping request data
我有一个自定义 ImageProvider
可以与 Sonata 的媒体包一起使用。
不幸的是,当我点击提交时,表单字段被放入 extra_data
而不是表单对象,导致错误消息 This form should not contain extra fields.
.
我的buildCreateForm
函数如下:
public function buildCreateForm(FormMapper $formMapper)
{
$formMapper
->with('Media Details', ['class' => 'panel-media-details'])
->add('binaryContent', 'file', [
'label' => 'Upload an Image File',
'help' => AdminUtils::helpPopover(
'The uploaded image file should be in one of the following types: <strong>png, gif, jpg, jpeg</strong>',
['placement' => 'right']
),
'constraints' => [
new NotBlank(),
new NotNull(),
new File([
'mimeTypes' => ['image/png', 'image/gif', 'image/jpeg', 'image/pjpeg'],
'mimeTypesMessage' => 'This file is not an image (should be a PNG, a GIF or a JPEG, but is a {{ type }})',
])
],
])
->add('name', 'text', [
'label' => 'Title',
'help' => AdminUtils::helpPopover(
'The title for the image. It will be displayed as the image caption when embedding the image in text or when rendering it as part of a gallery. It will also be displayed as the title on the individual media item page, and in search results.',
['placement' => 'right']
),
'required' => true,
])
->add('alt_tag', 'text', [
'label' => 'Alternative Text',
'help' => AdminUtils::helpPopover(
'The text that will be used by screen readers, search engines, and when the image cannot be loaded. If not provided, the title will be used.',
['placement' => 'right']
),
'mapped' => false,
'required' => true,
])
->add('description', CKEditorType::class, [
'help' => AdminUtils::helpPopover('The description will be displayed on the media item page, in search results, and possibly when the image is displayed in a popup (lightbox) by itself or as part of a media gallery.', ['placement' => 'right']),
'required' => false,
])
->end()
->with('Notes', ['class' => 'panelr-notes'])
->add('notes', CKEditorType::class, [
'label' => 'Notes',
'help' => AdminUtils::helpPopover('Notes for administrative purposes; these are visible to Editors only and they will never be displayed on the frontend site.'),
'required' => false,
])
->end()
;
}
我的 $mediaAdmin->configureFormFields()
看起来像这样:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Publishing', ['class' => 'panelr-publishing'])
->add('enabled', null, [
'label' => 'Published',
'help' => AdminUtils::helpPopover('Media items that are unpublished will not be displayed anywhere on the frontend site, even when they are attached to content or embedded into text. They will still be visible in the backend CMS site.'),
'required' => false,
])
->end()
;
$media = $this->getSubject();
if (!$media) {
$media = $this->getNewInstance();
}
if (!$media || !$media->getProviderName()) {
return;
}
$formMapper->getFormBuilder()->addModelTransformer(
new ProviderDataTransformer($this->pool, $this->getClass()), true
);
$provider = $this->pool->getProvider($media->getProviderName());
if ($media->getId()) {
$provider->buildEditForm($formMapper);
} else {
$provider->buildCreateForm($formMapper);
}
// Put any hidden fields at the bottom of the form so that they don't
// break :first-child css rules used. Also, wrap them in a closed form
// group so that it doesn't break form groups loaded through
// buildEditForms or configureFormFields defined in child Admin classes.
$formMapper
->with('Provider', ['class' => 'hidden-provider'])
->add('providerName', 'hidden')
->end()
;
}
验证失败后表单对象的调试为:
我正在覆盖 sonata.media.provider.iamge
因此显示为提供者。
对于那些在这里找到路的人...
首先,通过额外字段违规
public function getFormBuilder()
{
$this->formOptions['allow_extra_fields'] = true;
return parent::getFormBuilder();
}
下一步手动映射字段
public function prePersist($media)
{
// This is a bit messy, but get the extra_data array and manually map it
$extraData = $this->getForm()->getExtraData();
$media->setProviderName($extraData['providerName']);
$media->setName($extraData['name']);
$media->setDescription($extraData['description']);
$media->setBinaryContent($extraData['binaryContent']);
$media->setNotes($extraData['notes']);
parent::prePersist($media);
}
我有一个自定义 ImageProvider
可以与 Sonata 的媒体包一起使用。
不幸的是,当我点击提交时,表单字段被放入 extra_data
而不是表单对象,导致错误消息 This form should not contain extra fields.
.
我的buildCreateForm
函数如下:
public function buildCreateForm(FormMapper $formMapper)
{
$formMapper
->with('Media Details', ['class' => 'panel-media-details'])
->add('binaryContent', 'file', [
'label' => 'Upload an Image File',
'help' => AdminUtils::helpPopover(
'The uploaded image file should be in one of the following types: <strong>png, gif, jpg, jpeg</strong>',
['placement' => 'right']
),
'constraints' => [
new NotBlank(),
new NotNull(),
new File([
'mimeTypes' => ['image/png', 'image/gif', 'image/jpeg', 'image/pjpeg'],
'mimeTypesMessage' => 'This file is not an image (should be a PNG, a GIF or a JPEG, but is a {{ type }})',
])
],
])
->add('name', 'text', [
'label' => 'Title',
'help' => AdminUtils::helpPopover(
'The title for the image. It will be displayed as the image caption when embedding the image in text or when rendering it as part of a gallery. It will also be displayed as the title on the individual media item page, and in search results.',
['placement' => 'right']
),
'required' => true,
])
->add('alt_tag', 'text', [
'label' => 'Alternative Text',
'help' => AdminUtils::helpPopover(
'The text that will be used by screen readers, search engines, and when the image cannot be loaded. If not provided, the title will be used.',
['placement' => 'right']
),
'mapped' => false,
'required' => true,
])
->add('description', CKEditorType::class, [
'help' => AdminUtils::helpPopover('The description will be displayed on the media item page, in search results, and possibly when the image is displayed in a popup (lightbox) by itself or as part of a media gallery.', ['placement' => 'right']),
'required' => false,
])
->end()
->with('Notes', ['class' => 'panelr-notes'])
->add('notes', CKEditorType::class, [
'label' => 'Notes',
'help' => AdminUtils::helpPopover('Notes for administrative purposes; these are visible to Editors only and they will never be displayed on the frontend site.'),
'required' => false,
])
->end()
;
}
我的 $mediaAdmin->configureFormFields()
看起来像这样:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Publishing', ['class' => 'panelr-publishing'])
->add('enabled', null, [
'label' => 'Published',
'help' => AdminUtils::helpPopover('Media items that are unpublished will not be displayed anywhere on the frontend site, even when they are attached to content or embedded into text. They will still be visible in the backend CMS site.'),
'required' => false,
])
->end()
;
$media = $this->getSubject();
if (!$media) {
$media = $this->getNewInstance();
}
if (!$media || !$media->getProviderName()) {
return;
}
$formMapper->getFormBuilder()->addModelTransformer(
new ProviderDataTransformer($this->pool, $this->getClass()), true
);
$provider = $this->pool->getProvider($media->getProviderName());
if ($media->getId()) {
$provider->buildEditForm($formMapper);
} else {
$provider->buildCreateForm($formMapper);
}
// Put any hidden fields at the bottom of the form so that they don't
// break :first-child css rules used. Also, wrap them in a closed form
// group so that it doesn't break form groups loaded through
// buildEditForms or configureFormFields defined in child Admin classes.
$formMapper
->with('Provider', ['class' => 'hidden-provider'])
->add('providerName', 'hidden')
->end()
;
}
验证失败后表单对象的调试为:
我正在覆盖 sonata.media.provider.iamge
因此显示为提供者。
对于那些在这里找到路的人...
首先,通过额外字段违规
public function getFormBuilder()
{
$this->formOptions['allow_extra_fields'] = true;
return parent::getFormBuilder();
}
下一步手动映射字段
public function prePersist($media)
{
// This is a bit messy, but get the extra_data array and manually map it
$extraData = $this->getForm()->getExtraData();
$media->setProviderName($extraData['providerName']);
$media->setName($extraData['name']);
$media->setDescription($extraData['description']);
$media->setBinaryContent($extraData['binaryContent']);
$media->setNotes($extraData['notes']);
parent::prePersist($media);
}