我已在docs之后添加了此模型转换器:
->add('subtotal', MoneyType::class, array(
'attr' => array('readonly' => true),
))
->addModelTransformer(new CallbackTransformer(
// transform <br/> to \n so the textarea reads easier
function ($originalDescription) {
return $originalDescription;
},
function ($submittedDescription) {
var_dump($submittedDescription); //<<<<<<<<<<<<
// remove most HTML tags (but not br,p)
$cleaned = strip_tags($submittedDescription, '<br><br/><p>');
// transform any \n to real <br/>
return str_replace("\n", '<br/>', $cleaned);
}
))
问题:您可以看到var_dump()正在输出整个实体,如下所示。我只期待subtotal
提交字段的内容。
object(DefaultBundle\Entity\Bill)[682]
protected 'id' => null
private 'client' =>
object(Proxies\__CG__\DefaultBundle\Entity\Client)[788]
public '__initializer__' => null
public '__cloner__' => null
public '__isInitialized__' => boolean true
protected 'id' => int 1
private 'bills' (DefaultBundle\Entity\Client) =>
object(Doctrine\ORM\PersistentCollection)[436]
private 'snapshot' =>
array (size=0)
...
private 'owner' =>
&object(Proxies\__CG__\DefaultBundle\Entity\Client)[788]
private 'association' =>
array (size=15)
...
private 'em' =>
object(Doctrine\ORM\EntityManager)[151]
...
private 'backRefFieldName' => string 'client' (length=6)
private 'typeClass' =>
object(Doctrine\ORM\Mapping\ClassMetadata)[465]
...
private 'isDirty' => boolean false
protected 'collection' =>
object(Doctrine\Common\Collections\ArrayCollection)[801]
...
protected 'initialized' => boolean false
protected 'name' => string 'Jose Manuel Fernandez Fernandez' (length=31)
protected 'nif' => string '03113434P' (length=9)
protected 'address' => string 'Bulevar' (length=39)
protected 'phone' => string '633553423' (length=9)
protected 'email' => string 'me@gmail.com' (length=21)
protected 'numberPlate' => string 'fasdfdasf' (length=9)
protected 'createdAt' =>
object(DateTime)[453]
public 'date' => string '2016-02-28 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Madrid' (length=13)
protected 'serialNumber' => string '5' (length=1)
private 'servicesPerformed' =>
array (size=1)
0 =>
array (size=4)
'description' => string 'fasdf' (length=5)
'quantity' => string '4234' (length=4)
'unitPrice' => float 4234
'price' => float 17926756
protected 'subtotal' => null
protected 'taxRate' => float 0.21
protected 'tax' => float 3764618.76
protected 'total' => float 21691374.76
这里有实体和表单类型:
class BillType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['em'];
$builder
->add('serialNumber', TextType::class, array(
'label' => 'Nº serie',
'attr' => array('readonly' => true),
))
->add('createdAt', DateType::class, array(
'label' => 'Fecha',
'data' => new \DateTime("today"),
))
->add('client', null, array('label' => 'Cliente'))
->add('numberPlate', null, array(
'label' => 'Número matrícula',
//'data' => 'prueba'
))
->add('servicesPerformed', CollectionType::class, array(
'label' => false,
'entry_type' => ServicePerformedType::class,
'allow_add' => true,
'allow_delete' => true,
'attr' => array('data-prototype' => 'jander'),
))
->add('subtotal', MoneyType::class, array(
'attr' => array('readonly' => true),
//'grouping' => true
))
->addModelTransformer(new CallbackTransformer(
// transform <br/> to \n so the textarea reads easier
function ($originalDescription) {
return $originalDescription;
},
function ($submittedDescription) {
var_dump($submittedDescription);
die("jlfs");
// remove most HTML tags (but not br,p)
$cleaned = strip_tags($submittedDescription, '<br><br/><p>');
// transform any \n to real <br/>
return str_replace("\n", '<br/>', $cleaned);
}
))
->add('tax', MoneyType::class, array(
'label' => 'I.V.A.',
'attr' => array('readonly' => true),
'grouping' => true
))
->add('total', MoneyType::class, array(
'label' => 'Total',
'attr' => array('readonly' => true),
'grouping' => true
))
->add('Guardar', SubmitType::class)
->addEventListener(FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($em) {
$repository = $em->getRepository('DefaultBundle:Bill');
$result = count($repository->findAll()) + 1;
$form = $event->getForm();
$data = $event->getData();
if (!$data) {
$form->add('serialNumber', TextType::Class, array(
'data' => $result,
'label' => 'Nº serie',
'attr' => array('readonly' => true)
));
} else {
$form->add('serialNumber', TextType::Class, array(
'label' => 'Nº serie',
'attr' => array('readonly' => true)
));
}
}
)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DefaultBundle\Entity\Bill',
'em' => ''
));
}
}
/**
* DefaultBundle\Entity\Bill
*
* @ORM\Table
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Bill
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Client", inversedBy="bills")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false)
**/
private $client;
/**
* @ORM\Column(type="string")
*/
protected $numberPlate;
/**
* @ORM\Column(type="date")
*/
protected $createdAt;
/**
* @ORM\Column(type="string")
*/
protected $serialNumber;
/**
* Features of the product.
* Associative array, the key is the name/type of the feature, and the value the data.
* Example:<pre>array(
* 'size' => '13cm x 15cm x 6cm',
* 'bluetooth' => '4.1'
* )</pre>.
*
* @var array
* @ORM\Column(type="array")
*/
private $servicesPerformed = array();
/**
* @ORM\Column(type="float")
*/
protected $subtotal;
/**
* @ORM\Column(type="float")
*/
protected $taxRate = TaxRate::TAX_RATE;
/**
* @ORM\Column(type="float")
*/
protected $tax;
/**
* @ORM\Column(type="float")
*/
protected $total;
public function __toString()
{
return $this->numberPlate;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set numberPlate
*
* @param string $numberPlate
*
* @return Bill
*/
public function setNumberPlate($numberPlate)
{
$this->numberPlate = $numberPlate;
return $this;
}
/**
* Get numberPlate
*
* @return string
*/
public function getNumberPlate()
{
return $this->numberPlate;
}
/**
* Set servicesPerformed
*
* @param array $servicesPerformed
*
* @return Bill
*/
public function setServicesPerformed($servicesPerformed)
{
$this->servicesPerformed = $servicesPerformed;
return $this;
}
/**
* Get servicesPerformed
*
* @return array
*/
public function getServicesPerformed()
{
return $this->servicesPerformed;
}
/**
* Set subtotal
*
* @param string $subtotal
*
* @return Bill
*/
public function setSubtotal($subtotal)
{
$this->subtotal = $subtotal;
return $this;
}
/**
* Get subtotal
*
* @return string
*/
public function getSubtotal()
{
return $this->subtotal;
}
/**
* Set taxRate
*
* @param string $taxRate
*
* @return Bill
*/
public function setTaxRate($taxRate)
{
$this->taxRate = $taxRate;
return $this;
}
/**
* Get taxRate
*
* @return string
*/
public function getTaxRate()
{
return $this->taxRate;
}
/**
* Set tax
*
* @param string $tax
*
* @return Bill
*/
public function setTax($tax)
{
$this->tax = $tax;
return $this;
}
/**
* Get tax
*
* @return string
*/
public function getTax()
{
return $this->tax;
}
/**
* Set total
*
* @param string $total
*
* @return Bill
*/
public function setTotal($total)
{
$this->total = $total;
return $this;
}
/**
* Get total
*
* @return string
*/
public function getTotal()
{
return $this->total;
}
/**
* Set client
*
* @param \DefaultBundle\Entity\Client $client
*
* @return Bill
*/
public function setClient(\DefaultBundle\Entity\Client $client)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* @return \DefaultBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return Bill
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set serialNumber
*
* @param string $serialNumber
*
* @return Bill
*/
public function setSerialNumber($serialNumber)
{
$this->serialNumber = $serialNumber;
return $this;
}
/**
* Get serialNumber
*
* @return string
*/
public function getSerialNumber()
{
return $this->serialNumber;
}
}
编辑:在@ejuhjav的答案之后,这是我的buildForm()
函数:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $options['em'];
$builder
->add('serialNumber', TextType::class, array(
'label' => 'Nº serie',
'attr' => array('readonly' => true),
'data' => 6
))
->add('createdAt', DateType::class, array(
'label' => 'Fecha',
'data' => new \DateTime("today"),
))
->add('client', null, array('label' => 'Cliente'))
->add('numberPlate', null, array(
'label' => 'Número matrícula',
//'data' => 'prueba'
))
->add('servicesPerformed', CollectionType::class, array(
'label' => false,
'entry_type' => ServicePerformedType::class,
'allow_add' => true,
'allow_delete' => true,
'attr' => array('data-prototype' => 'jander'),
))
->add('subtotal', MoneyType::class, array(
'attr' => array('readonly' => true),
'data' => 6
))
->add('tax', MoneyType::class, array(
'label' => 'I.V.A.',
'attr' => array('readonly' => true),
'grouping' => true,
'data' => 6
))
->add('total', MoneyType::class, array(
'label' => 'Total',
'attr' => array('readonly' => true),
'grouping' => true,
'data' => 6
))
->add('Guardar', SubmitType::class)
;
$builder->get('subtotal')
->addModelTransformer(new CallbackTransformer(
// transform <br/> to \n so the textarea reads easier
function ($originalDescription) {
return $originalDescription;
},
function ($submittedDescription) {
//var_dump($submittedDescription);
die("IT IS _NOT_ ENTERING HERE!!! :)");
// remove most HTML tags (but not br,p)
$cleaned = strip_tags($submittedDescription, '<br><br/><p>');
// transform any \n to real <br/>
return str_replace("\n", '<br/>', $cleaned);
}
));
}
答案 0 :(得分:0)
您正在将模型转换器连接到整个表单。要将变换器添加到单个字段,请使用(来自引用的文档):
$builder->add('description', TextareaType::class);
$builder->get('description')
->addModelTransformer(new CallbackTransformer(
...
或
$builder->add(
$builder->create('description', TextareaType::class)
->addModelTransformer(...)
);