setElementsBelongTo() effect on subform elements

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

setElementsBelongTo() effect on subform elements

artsylar
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi,

I have a subform($fileUploadSubform) within a subform ($requestSubform). I called setElementsBelongTo("requestRow[$rowNumber]") on the parent subform ($requestSubform).

    $requestSubform= new Zend_Form_Subform();
    $requestSubform->setElementsBelongTo("requestRow[$rowNumber]");

    // add elements to $requestSubform

    // now create the file upload subform
    $fileUploadSubform= new Zend_Form_SubForm();
    $fileUploadSubform->addElement('file', 'fileName')
            ->setLabel('File'); 

    $fileUploadSubform->addElement('text', 'fileDesc')
            ->setLabel('File Description'); 

    $requestSubform->addSubForm($fileUploadSubform, 'fileUpload');
When the form is rendered, $fileUploadSubform elements' names and ids are as follows

name="requestRow[1][requestRow][1][fileUpload][fileDesc]"
id="requestRow-1-fileUpload-fileDesc"

Why is the value I've set in the setElementsBelongTo() function is repeated twice in my element's name?

Thank you in advance!
Reply | Threaded
Open this post in threaded view
|

Re: setElementsBelongTo() effect on subform elements

artsylar
[Update 08/17/2015]

I have tried the following code I got from http://zend-framework-community.634137.n4.nabble.com/Improved-array-support-for-Zend-Form-td667215.html as it says in that post that subform elementsTobelong to is working properly.

    $form = new Zend_Form();
    $form->setElementsBelongTo('foobar');

    $form->addElement('text', 'firstName')
    ->getElement('firstName')
    ->setLabel('First Name')
    ->setRequired(true);

    $form->addElement('text', 'lastName')
    ->getElement('lastName')
    ->setLabel('Last Name')
    ->setRequired(true);

    $subForm = new Zend_Form_SubForm();
    $subForm->setElementsBelongTo('foobar[baz]');
    $subForm->addElement('text', 'email')
    ->getElement('email')
    ->setLabel('Email Address');

    $subSubForm = new Zend_Form_SubForm();
    $subSubForm->setElementsBelongTo('foobar[baz][bat]');
    $subSubForm->addElement('checkbox', 'home')
    ->getElement('home')
    ->setLabel('Home address?');
    $subForm->addSubForm($subSubForm, 'subSub');

    $form->addSubForm($subForm, 'sub')
    ->addElement('submit', 'save', array('value' => 'submit'));
    print_r($form->__toString());

But here's what I'm getting for the $subForm's and $subFubForm's elements.

<input id="foobar-foobar-baz-email" type="text" value="" name="foobar[foobar][foobar][baz][email]">

<input id="foobar-foobar-baz-foobar-baz-bat-home" type="checkbox" value="1" name="foobar[foobar][foobar][baz][foobar][foobar][baz][foobar][baz][bat][home]">

I hope someone can help me with this problem.
Reply | Threaded
Open this post in threaded view
|

Re: setElementsBelongTo() effect on subform elements

Frank Brückner
Hi,
the names are wrong. Please check this:

$form = new Zend_Form();
$form->setElementsBelongTo('foobar');

$form->addElement('text', 'firstName');
$form->addElement('text', 'lastName');

$subForm = new Zend_Form_SubForm();
$subForm->setElementsBelongTo('baz');
$subForm->addElement('text', 'email');

$subSubForm = new Zend_Form_SubForm();
$subSubForm->setElementsBelongTo('bat');
$subSubForm->addElement('checkbox', 'home');
$subForm->addSubForm($subSubForm, 'subSub');

$form->addSubForm($subForm, 'sub');


Kind regards,
Frank


Am .08.2015, 03:41 Uhr, schrieb artsylar <[hidden email]>:

> [Update 08/17/2015]
>
> I have tried the following code I got from
> http://zend-framework-community.634137.n4.nabble.com/Improved-array-support-for-Zend-Form-td667215.html
> as it says in that post that subform elementsTobelong to is working
> properly.
>
>
>
> But here's what I'm getting for the $subForm's and $subFubForm's  
> elements.
>
>
>
> I hope someone can help me with this problem.
>
>
>
>
> --
> View this message in context:  
> http://zend-framework-community.634137.n4.nabble.com/setElementsBelongTo-effect-on-subform-elements-tp4662645p4662651.html
> Sent from the Zend Framework mailing list archive at Nabble.com.

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Register validator DoctrineModule\Validator\UniqueObject

Cristian Bichis-4
Hi,

I am trying to use

DoctrineModule\Validator\UniqueObject

as validator within a zf2 form with annotations:

     /**
      * @var string
      *
      * @Annotation\Type("email")
      * @Annotation\Required(true)
      * @Annotation\Filter({"name":"Zend\Filter\StringTrim"})
      * @Annotation\Filter({"name":"Zend\Filter\StripTags"})
      * @Annotation\Validator({"name":"Zend\Validator\StringLength",
"options":{"min":6, "max":128, "encoding":"UTF-8"}})
      *
@Annotation\Validator({"name":"DoctrineModule\Validator\UniqueObject"})
      * @Annotation\Options({"label":"Email", "column-size":"sm-10",
"label_attributes":{"class":"col-sm-2"}})
      * @Annotation\Flags({"priority": 93})
      *
      * @ORM\Column(name="email", type="string", length=128, nullable=false)
      */
     protected $email;


Because I need to inject object_repository and object_manager I tried to
register a factory:

             'factories' => array(
                 'DoctrineModule\Validator\UniqueObject' => function ($sm){
                     $uniqueObject = new
DoctrineModule\Validator\UniqueObject(array(
                         'fields' => 'email',
                         'object_repository' =>
$sm->get('Doctrine\ORM\EntityManager')->getRepository('Crm\Entity\User'),
                         'object_manager' =>
$sm->get('Doctrine\ORM\EntityManager'),
                     ));
                     return $uniqueObject;
                 }
             )

I tried under different configs:
Module getValidatorConfig
Module getFormElementConfig
     or under
'form_elements'
'validators'
etc

No luck...

I am getting the:
PHP Catchable fatal error:  Argument 1 passed to
DoctrineModule\\Validator\\UniqueObject::__construct() must be of the
type array, none given, called in
/home/www/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php
on line 207 and defined in
/home/www/app/vendor/doctrine/doctrine-module/src/DoctrineModule/Validator/UniqueObject.php
on line 66


Any clue where to register this ?

Cristian

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Register validator DoctrineModule\Validator\UniqueObject

Stefano Torresi-2
Hello Cristian,

It looks like your validator is being instantiated as an invokable. Bear in
mind that usually plugin managers automatically instantiate invokables via
a new statement with no constructor arguments. This means that your factory
is not being correctly registered, so the plugin manager can't find your
validator service and tries to automatically add it as an invokable.

You have to register the factory in the ValidatorPluginManager. You can do
that by implementing the Zend\ModuleManager\ValidatorProvider interface in
your module.
This also entails that the service locator parameter of the factory will
not be the root ServiceManager, but the ValidatorPluginManager instead, so
you will have to invoke 'getServiceLocator()'  on it to grab the root one.

Cheers

Il giorno mar 15 set 2015 alle ore 20:39 Cristian Bichis <[hidden email]>
ha scritto:

> Hi,
>
> I am trying to use
>
> DoctrineModule\Validator\UniqueObject
>
> as validator within a zf2 form with annotations:
>
>      /**
>       * @var string
>       *
>       * @Annotation\Type("email")
>       * @Annotation\Required(true)
>       * @Annotation\Filter({"name":"Zend\Filter\StringTrim"})
>       * @Annotation\Filter({"name":"Zend\Filter\StripTags"})
>       * @Annotation\Validator({"name":"Zend\Validator\StringLength",
> "options":{"min":6, "max":128, "encoding":"UTF-8"}})
>       *
> @Annotation\Validator({"name":"DoctrineModule\Validator\UniqueObject"})
>       * @Annotation\Options({"label":"Email", "column-size":"sm-10",
> "label_attributes":{"class":"col-sm-2"}})
>       * @Annotation\Flags({"priority": 93})
>       *
>       * @ORM\Column(name="email", type="string", length=128,
> nullable=false)
>       */
>      protected $email;
>
>
> Because I need to inject object_repository and object_manager I tried to
> register a factory:
>
>              'factories' => array(
>                  'DoctrineModule\Validator\UniqueObject' => function ($sm){
>                      $uniqueObject = new
> DoctrineModule\Validator\UniqueObject(array(
>                          'fields' => 'email',
>                          'object_repository' =>
> $sm->get('Doctrine\ORM\EntityManager')->getRepository('Crm\Entity\User'),
>                          'object_manager' =>
> $sm->get('Doctrine\ORM\EntityManager'),
>                      ));
>                      return $uniqueObject;
>                  }
>              )
>
> I tried under different configs:
> Module getValidatorConfig
> Module getFormElementConfig
>      or under
> 'form_elements'
> 'validators'
> etc
>
> No luck...
>
> I am getting the:
> PHP Catchable fatal error:  Argument 1 passed to
> DoctrineModule\\Validator\\UniqueObject::__construct() must be of the
> type array, none given, called in
>
> /home/www/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php
> on line 207 and defined in
>
> /home/www/app/vendor/doctrine/doctrine-module/src/DoctrineModule/Validator/UniqueObject.php
> on line 66
>
>
> Any clue where to register this ?
>
> Cristian
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Register validator DoctrineModule\Validator\UniqueObject

cbichis
Hi Stefano,

This doesnt seems to work.

I changed annotation to:

      *
@Annotation\Validator({"name":"DoctrineModule\Validator\UniqueObject"})

and added on Module (which now extends
Zend\ModuleManager\Feature\ValidatorProviderInterface) this:

     public function getValidatorConfig()
     {
         return array(
             'factories' => array(
                 'DoctrineModule\Validator\UniqueObject' => function ($sm){

                     $uniqueObject = new
DoctrineModule\Validator\UniqueObject(array(
                         'fields' => 'email',
                         'object_repository' =>
$sm->getServiceLocator()->get('Doctrine\ORM\EntityManager')->getRepository('Crm\Entity\User'),
                         'object_manager' =>
$sm->getServiceLocator()->get('Doctrine\ORM\EntityManager'),
                     ));
                     return $uniqueObject;
                 }
             )
         );
     }

However, the same error occurs...


> Hello Cristian,
>
> It looks like your validator is being instantiated as an invokable. Bear in
> mind that usually plugin managers automatically instantiate invokables via
> a new statement with no constructor arguments. This means that your factory
> is not being correctly registered, so the plugin manager can't find your
> validator service and tries to automatically add it as an invokable.
>
> You have to register the factory in the ValidatorPluginManager. You can do
> that by implementing the Zend\ModuleManager\ValidatorProvider interface in
> your module.
> This also entails that the service locator parameter of the factory will
> not be the root ServiceManager, but the ValidatorPluginManager instead, so
> you will have to invoke 'getServiceLocator()'  on it to grab the root one.
>
> Cheers
>
> Il giorno mar 15 set 2015 alle ore 20:39 Cristian Bichis <[hidden email]>
> ha scritto:
>
>> Hi,
>>
>> I am trying to use
>>
>> DoctrineModule\Validator\UniqueObject
>>
>> as validator within a zf2 form with annotations:
>>
>>       /**
>>        * @var string
>>        *
>>        * @Annotation\Type("email")
>>        * @Annotation\Required(true)
>>        * @Annotation\Filter({"name":"Zend\Filter\StringTrim"})
>>        * @Annotation\Filter({"name":"Zend\Filter\StripTags"})
>>        * @Annotation\Validator({"name":"Zend\Validator\StringLength",
>> "options":{"min":6, "max":128, "encoding":"UTF-8"}})
>>        *
>> @Annotation\Validator({"name":"DoctrineModule\Validator\UniqueObject"})
>>        * @Annotation\Options({"label":"Email", "column-size":"sm-10",
>> "label_attributes":{"class":"col-sm-2"}})
>>        * @Annotation\Flags({"priority": 93})
>>        *
>>        * @ORM\Column(name="email", type="string", length=128,
>> nullable=false)
>>        */
>>       protected $email;
>>
>>
>> Because I need to inject object_repository and object_manager I tried to
>> register a factory:
>>
>>               'factories' => array(
>>                   'DoctrineModule\Validator\UniqueObject' => function ($sm){
>>                       $uniqueObject = new
>> DoctrineModule\Validator\UniqueObject(array(
>>                           'fields' => 'email',
>>                           'object_repository' =>
>> $sm->get('Doctrine\ORM\EntityManager')->getRepository('Crm\Entity\User'),
>>                           'object_manager' =>
>> $sm->get('Doctrine\ORM\EntityManager'),
>>                       ));
>>                       return $uniqueObject;
>>                   }
>>               )
>>
>> I tried under different configs:
>> Module getValidatorConfig
>> Module getFormElementConfig
>>       or under
>> 'form_elements'
>> 'validators'
>> etc
>>
>> No luck...
>>
>> I am getting the:
>> PHP Catchable fatal error:  Argument 1 passed to
>> DoctrineModule\\Validator\\UniqueObject::__construct() must be of the
>> type array, none given, called in
>>
>> /home/www/app/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php
>> on line 207 and defined in
>>
>> /home/www/app/vendor/doctrine/doctrine-module/src/DoctrineModule/Validator/UniqueObject.php
>> on line 66
>>
>>
>> Any clue where to register this ?
>>
>> Cristian
>>
>> --
>> List: [hidden email]
>> Info: http://framework.zend.com/archives
>> Unsubscribe: [hidden email]
>>
>>
>>


--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]