自定义字段类型中的路径
Workaround :现在,将表单父项从表单转换为文本可以实现这一功能。
我刚刚创建了一个父类型为form的自定义字段类型。
有谁知道我该如何获得正确的property_path? 我的意思是,在MyFieldType内部,我想访问使用my_field_type字段的MyFormType属性,这样我就可以通过dinamically设置正确的property_path。
这是我的自定义字段类型。 在下面的类中想要将使用ColorPaletteField的Form Type属性设置为propery_path值。
namespace WEBobbyWebAppBundleFormField;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormView;
use SymfonyComponentFormFormInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
use SymfonyComponentPropertyAccessPropertyAccess;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormExtensionCoreEventListenerTrimListener;
class ColorPaletteField extends AbstractType
{
public function setDefaultOptions( OptionsResolverInterface $resolver )
{
$resolver->setDefaults( array(
'mapped' => true,
'error_bubbling' => false,
'colors' => array()
)
);
}
/**
* Pass the help to the view
*
* @param FormView $view
* @param FormInterface $form
* @param array $options
*/
public function buildView( FormView $view, FormInterface $form, array $options )
{
$parentData = $form->getParent()->getData();
if( null !== $parentData )
{
$accessor = PropertyAccess::getPropertyAccessor();
$defaultColor = $accessor->getValue( $parentData, 'calendar_color' );
}
else { $defaultColor = null; }
if( array_key_exists( 'colors', $options ) )
{
$colors = $options[ 'colors' ];
}
else { $colors = array(); }
$view->vars[ 'colors' ] = $colors;
$view->vars[ 'defaultColor' ] = $defaultColor;
}
public function getParent()
{
return 'form';
}
public function getName()
{
return 'color_palette';
}
}
先谢谢了,
你可以通过选项传递它。 首先在自定义字段中设置默认值
$resolver->setDefaults(array(
'mapped' => true,
'error_bubbling' => false,
'colors' => array()
'property_name' => 'calendar_color'
));
然后添加此字段以在选项中形成和定义属性名称
->add('some_name', 'color_palette', array('property_name' => 'some_name'));
链接地址: http://www.djcxy.com/p/57887.html
