Issue
I have entity with property reverse, type boolean and form for this property type choice - checkbox and always after form handleRequest I have true
why not understand, if check box not checked have "0"
but after submit form have true
/**
* @var boolean
*
* @ORM\Column(name="reversed_vat", type="boolean", nullable=false, options={"default"=0})
*/
private $reversedVat;
/**
* Set reversedVat
*
* @param boolean $reversedVat
*
* @return OutboundInvoice
*/
public function setReversedVat($reversedVat)
{
$this->reversedVat = (boolean)$reversedVat;
return $this;
}
In form I create data transformer with check property but in transfomer I have true
/**
* Get reversedVat
*
* @return boolean
*/
public function getReversedVat()
{
return (boolean)$this->reversedVat;
}
$builder
->add('reversedVat', CheckboxType::class, ['required' => false])
$builder->get('reversedVat')
->addModelTransformer(new CallbackTransformer(
function($mypropertyAsBoolean) {
},
function($mypropertyAsString) {
$r = $mypropertyAsString;//have always true
}
));
if I not checked still have true after form handleRequest ?
Solution
The dataTransformer is not necessary. CheckBoxType has transformer functionality in itself.
ReversedVat property is simply below.
/**
* @var boolean
* @ORM\Column(name="reversed_vat", type="boolean", nullable=false, options={"default"=0})
*/
private $reversedVat = false;
/**
* @param bool $reversedVat
* @return Temp
*/
public function setReversedVat($reversedVat)
{
$this->reversedVat = $reversedVat;
return $this;
}
/**
* @return bool
*/
public function getReversedVat()
{
return $this->reversedVat;
}
Confirm boolean is set to $reversedVat as default value.
And the form type is below.
$builder
->add('reversedVat', CheckboxType::class, ['required' => false])
Answered By - Miwanoushi
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.