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"]]],
];
}
}