I have the following entity:
<code>
<?php
// Materials entity - this is an extension of the MSSQSL Table
namespace Assets\Model;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Form\Annotation ;
/**
* Materials
* Holds a key, a materials_type and some text
* @ORM\Entity
* @ORM\Table(name="materials")
* @Annotation\Name("materials")
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ClassMethods")
*
* @property integer id
* @property string $text
* @property string $comments
*/
class Materials implements InputFilterAwareInterface
{
protected $inputFilter;
/**
* @var integer $id
*
*
* @ORM\Id
* @ORM\Column(name = "id",type="integer");
* @ORM\GeneratedValue(strategy="IDENTITY")
* @Annotation\Exclude()
*
*/
protected $id;
/**
* @ORM\Column(name="text",type = "string" )
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Validator({"name":"StringLength", "options":{"min":"1",
"max" : "50"}})
* @Annotation\Options({"label":"Material Type:"})
*
*/
protected $text;
/**
* @ORM\Column(name="comments",type = "string")
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Validator({"name":"StringLength", "options":{"min":"0",
"max" : "255"}})
* @Annotation\Options({"label":"Comments:"})
* @Annotation\Attributes({"type":"textarea"})
*/
protected $comments;
/**
*
* @ORM\Column(name="created_by",type= "integer")
* @Annotation\Exclude()
* @todo add reference to AUTH
*
*/
protected $created_by;
/**
*
*
* @ORM\Column(name="created_at",type= "datetime")
* @Annotation\Exclude()
* @todo add reference to AUTH
*
*/
protected $created_at;
/**
*
*
* @ORM\Column(name="updated_by", type= "integer")
* @Annotation\Exclude()
* @todo add pre-persist (set dateime = now)
*
*/
protected $updated_by;
/**
*
* @ORM\Column(name="updated_at", type= "datetime")
* @Annotation\Exclude()
* @todo add reference to AUTH
*
*/
protected $updated_at;
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property)
{
return $this->$property;
}
/**
* @method getId
*/
public function getId()
{
return $this->id;
}
public function setId($id)
{
return $this->id = $id;
}
/*
* getText
* for lookup's etc
*/
public function getText()
{
return $this->text;
}
public function setText($text)
{
return $this->text = $text;
}
public function getComments()
{
return $this->comments;
}
public function setComments($comments)
{
return $this->comments = $comments;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value)
{
$this->$property = $value;
}
/**
* Convert the object to an array.
*
* @return array
*/
public function getArrayCopy()
{
return get_object_vars ( $this );
}
/**
* Populate from an array.
*
* @param array $data
*/
public function populate($data = array())
{
/*
* Populate from input.
* BLS 20140420 - Need to add user and updated details.
*/
$this->id = $data ['id'];
$this->text = $data ['text'];
$this->comments = $data ['comments'];
$this->created_at = $data ['created_at'];
$this->created_by = $data ['created_by'];
$this->updated_at = $data ['updated_at'];
$this->updated_by = $data ['created_by'];
/* *
* @todo finish off
* @property int $created_by -- user that created the record
* @property datetime $created_at
* @property int $dated_by
* @property datetime $updated_at
*/
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception ( "Not used" );
}
public function getInputFilter()
{
if (! $this->inputFilter)
{
$inputFilter = new InputFilter ();
$factory = new InputFactory ();
// Id
// Do we need to check if id is in the asset_register?
$inputFilter->add ( $factory->createInput ( array ('name' =>
'id','required' => true,'filters' => array (array ('name' => 'Int' ) ) ) )
);
// Asset Type
$inputFilter->add ( $factory->createInput ( array ('name' =>
'text','required' => true,'filters' => array (array ('name' => 'StripTags'
),array ('name' => 'StringTrim' ) ),
'validators' => array (array ('name' =>
'StringLength','options' => array ('encoding' => 'UTF-8','min' => 1,'max'
=> 127 ) ) ) ) ) );
// TableName
$inputFilter->add ( $factory->createInput ( array ('name' =>
'comments','required' => false,'filters' => array (array ('name' =>
'StripTags' ),array ('name' => 'StringTrim' ) ),
'validators' => array (array ('name' =>
'StringLength','options' => array ('encoding' => 'UTF-8','min' => 1,'max'
=> 255 ) ) ) ) ) );
}
$this->inputFilter = $inputFilter;
return $this->inputFilter;
}
}
<code>
In my controller I have an 'editAction' :-
<code>
public function editAction()
{
$id = ( int ) $this->getEvent ()->getRouteMatch ()->getParam ( 'id'
);
if (! $id)
return $this->redirect ()->toRoute ( 'admin/materials');
$entityManager = $this->getEntityManager();
$repository = $this->getEntityManager
()->getRepository('Assets\Model\Materials');
$material = $repository->find($id);
if (!$material)
{
die("Couldn't find Material Record");
}
/* here comes the magic */
$builder = new AnnotationBuilder( $entityManager);
$form = $builder->createForm( $material );
$form->setHydrator ( new DoctrineHydrator ( $entityManager,
'Assets\Model\Materials' ) );
$SubmitButton = New Element('send');
$SubmitButton->setAttributes (array('type'=>'submit'));
$form->add($SubmitButton);
$form->bind($material);
$request = $this->getRequest();
if ($request->isPost())
{
// $form->remove('send';)
$form->setData($request->getPost());
if ($form->isValid())
{
try
{
$entityManager->persist($material);
$entityManager->flush();
}
catch ( \Exception $e )
{
echo "In file :".__FILE__." on line".__LINE__."<br>";
var_dump();
die("Hey");
}
return $this->redirect()->toRoute('admin_materials/index');
}
else
{
echo "<pre>";
$messages = $form->getMessages();
$filter = $form->getInputFilter();
$rawValues = $filter->getRawValues();
// echo "<p>Cant validate the form whats going on</p>";
var_dump($filter);
var_dump($rawValues);
// var_dump($form->getElements()) ;
echo "</pre>";
die("<Cant validate the form");
return null;
}
}
echo "Building a form";
// echo "In file :".__FILE__." on line".__LINE__."<br>";
// echo "<pre>";
// var_dump($request);
// die("Strange");
$view = new ViewModel();
$view->setVariable('form',$form);
return $view;
}
</code>
The form displays with the correct data in it but the form never validates.
I checked that the 'text' is required and that works, The dump statement
give:
<output>
object(Zend\InputFilter\InputFilter)#406 (6) {
["factory":protected] NULL
["data":protected] NULL
["inputs":protected] array(0) {
}
["invalidInputs":protected] NULL
["validationGroup":protected] NULL
["validInputs":protected] NULL
}
array(0) {
}
</output>
I'm obviously making a major error, but I cant work out.
I do not load the created_.updated_ fields into the form. If this the
issue?
I'm a little stuck.
Am I not using the hydrator properly?
I'm lost.
Thanks in advance Barry.
Stay well,
Barry
--
You received this message because you are subscribed to the Google Groups
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.