Hi, I have a post form which is not setting values in the bound entity. The code looks like below:
$form = new PostForm;
// postfieldset constructor has parent::__construct('post');
$form->add( new PostFieldset() );
$form->setHydrator( \Doctrine\Laminas\Hydrator\DoctrineObject::class);
$request = $this->getRequest();
// post entity with two fields only title and description
$postEntity = new \Application\Entity\Post();
$form->bind($postEntity);
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
var_dump($postEntity->getTitle());
}
}
//------- Form ----- //
use Laminas\Form\Form;
class PostForm extends Form{
public function __construct($name = 'create-post')
{
parent::__construct($name);
$csrf = new Element\Csrf('csrf');
$this->add($csrf);
$submitElement = new Element\Button('submit');
$submitElement
->setLabel('Create')
->setAttributes(
[
'type' => 'submit',
]
);
$this->add(
$submitElement,
[
'priority' => -100,
]
);
}
}
//-------FieldSet Class ---------
use Laminas\Form\Fieldset;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Form\Element;
class PostFieldset extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct('post');
$this->add([
'name' => 'title',
'options' => [
'label' => 'Title',
],
]);
$this->add([
'name' => 'created_at',
'type' => Element\Date::class,
'options' => [
'label' => 'Created At',
],
]);
$this->add([
'name' => 'description',
'type' => Element\Textarea::class,
'options' => [
'label' => 'Description',
],
]);
}
/**
* @return array
*/
public function getInputFilterSpecification()
{
return [
'title' => [
'required' => true,
],
'description' => [
'required' => true,
],
'created_at' => [
'required' => true,
],
];
}
}
//----- Post Entity
/**
* @ORM\Entity
* @Gedmo\Loggable
* @ORM\Table(name="posts")
*/
class Post
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", options={"unsigned"=true})
*/
private $id;
/**
* @Gedmo\Versioned
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @Gedmo\Versioned
* @ORM\Column(type="text")
*/
private $description;
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created_at;
}
//--- Returned data from $this->getRequest()->getPost()
Laminas\Stdlib\Parameters Object
(
[storage:ArrayObject:private] => Array
(
[csrf] => csrf-value
[post] => Array
(
[title] => My first doctrine post in Laminas
[created_at] => 2021-02-03
[description] => My first doctrine post in Laminas
)
[submit] =>
)
)
Thanks in advance!