Home » Php » php – How to add zend form element at specific position, after once form object has already created?

php – How to add zend form element at specific position, after once form object has already created?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have created one user_form class that extends zend form, it has 4 elements username, password, hash for csrf and at last submit button.

Creating object of user_form renders all those four element.

After validating login in controller action i checks fail attempts, and after some fix number of fail attempts I want to add zend captch before submit button.

I added captcha element and it was appended at after submit button.

How can I add zend element at specific position? Or How can i add it before submit button?

Also let me know that the way am I doing is proper?
Waiting for your reply.
Thank you…

How to&Answers:

Give your elements order numbers from the very begining. Add an order number to the captcha element, when you add it.

$element->setOrder(10);

or

$form->addElement('text', 'username', array('order' => 10));

See also the Zend_Form manual.

Answer:

You can either use setOrder()as markus said, or when you render your form in your viewscript, you can render each field separately:

// .phtml
<form id="form" action="<?= $this->escape($this->form->getAction()); ?>" method="<?= $this->escape($this->form->getMethod()); ?>">
<table>
  <?= $this->form->username ?>
  <?= $this->form->password ?>
  <?= $this->form->hash ?>
  <?= $this->form->captcha ?>
  <?= $this->form->submit ?>
</table>
</form>