📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Zend Framework / Laminas Forms in Laminas

Forms in Laminas

5 min read Quiz at the end
Build forms with Laminas Form, InputFilter, validators (NotEmpty, StringLength), and filters.

Forms with Laminas Form

namespace BlogForm;
use LaminasFormForm;
use LaminasInputFilterInputFilter;
use LaminasValidator;

class PostForm extends Form {
    public function __construct() {
        parent::__construct("post_form");
        $this->setAttribute("method", "post");

        $this->add(["name" => "title", "type" => "text",
            "options" => ["label" => "Title"],
            "attributes" => ["class" => "form-control", "required" => true]]);

        $this->add(["name" => "body", "type" => "textarea",
            "options" => ["label" => "Body"],
            "attributes" => ["rows" => 10, "class" => "form-control"]]);

        $this->add(["name" => "submit", "type" => "submit",
            "attributes" => ["value" => "Save Post"]]);
    }

    public function getInputFilterSpecification(): array {
        return [
            "title" => ["required" => true, "filters" => [["name" => "StringTrim"]],
                "validators" => [["name" => "StringLength", "options" => ["min" => 3, "max" => 255]]]],
            "body"  => ["required" => true, "filters" => [["name" => "StringTrim"]]],
        ];
    }
}
Topic Quiz · 2 questions

Test your understanding before moving on

1. What does isValid() do on a Laminas form?
💡 isValid() runs all input filters and validators returning true if all pass.
2. What does $this->escapeHtml($val) do in a view?
💡 escapeHtml() converts special chars to HTML entities — essential for all user output.