Zend Basic Tutorial
Zend Forms
Zend Database
Zend Advanced
Zend\Form is a powerful component used to manage HTML forms. It supports form creation, validation, filtering, CSRF protection, and file uploads — all using PHP code, not HTML.
composer require laminas/laminas-form composer require laminas/laminas-i18n
namespace Application\Form; use Laminas\Form\Form; class LoginForm extends Form { public function __construct($name = null) { parent::__construct('login'); $this->add([ 'name' => 'email', 'type' => 'Text', 'options' => ['label' => 'Email'], ]); $this->add([ 'name' => 'password', 'type' => 'Password', 'options' => ['label' => 'Password'], ]); $this->add([ 'name' => 'submit', 'type' => 'Submit', 'attributes' => [ 'value' => 'Login', 'class' => 'btn btn-primary' ], ]); } }
use Application\Form\LoginForm; public function loginAction() { $form = new LoginForm(); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { // Process login } } return new ViewModel(['form' => $form]); }
// In login.phtml $form->prepare(); echo $this->form()->openTag($form); echo $this->formRow($form->get('email')); echo $this->formRow($form->get('password')); echo $this->formSubmit($form->get('submit')); echo $this->form()->closeTag();
You can use:
formRow()
– full label + input + errorformInput()
– just inputformLabel()
– just label<form method="post"> <label>Email</label> <input type="text" name="email"> <label>Password</label> <input type="password" name="password"> <input type="submit" value="Login"> </form>
Use Case |
Form Class |
---|---|
Login Form |
LoginForm |
Registration Form |
RegisterForm |
Product Entry Form |
ProductForm |