<?php $loader = (require __DIR__ . '/../../vendor/autoload.php'); $loader->add('Formz\\Tests', __DIR__ . '/../../tests'); use Formz\Tests\Fixtures\Post; use Formz\Tests\Fixtures\Attribute; $builder = new \Formz\Builder(); $form = $builder->form([$builder->field('title')->nonEmptyText(), $builder->field('tags')->nonEmptyText()->multiple(), $builder->field('attributes', [$builder->field('name')->nonEmptyText(), $builder->field('value')->nonEmptyText()], function ($name, $value) { return new Attribute($name, $value); }, function (Attribute $attribute) { return ['name' => $attribute->getName(), 'value' => $attribute->getValue()]; })->multiple()], function ($title, array $tags, array $attributes) { return new Post($title, $tags, $attributes); }, function (Post $post) { return ['title' => $post->getTitle(), 'tags' => $post->getTags(), 'attributes' => $post->getAttributes()]; }); $post = new Post('Hello World', ['foo', 'bar'], [new Attribute('foo', 'bar'), new Attribute('baz', 'biz')]); $form->fill($post); if ('POST' == $_SERVER['REQUEST_METHOD']) { $form->bind($_POST); if ($form->isValid()) { echo '<pre>' . print_r($form->getData(), true) . '</pre>'; } } echo render('form.php.html', ['form' => $form]); function render($template, array $parameters = []) { ob_start(); extract($parameters); include __DIR__ . '/' . $template; return ob_get_clean();
public function __construct($subject, $message) { $this->subject = $subject; $this->message = $message; } public function getSubject() { return $this->subject; } public function getMessage() { return $this->message; } } $builder = new \Formz\Builder(); $form = $builder->form([$builder->field('subject')->nonEmptyText(), $builder->field('message')->nonEmptyText()], function ($subject, $message) { return new Contact($subject, $message); }, function (Contact $contact) { return ['subject' => $contact->getSubject(), 'message' => $contact->getMessage()]; }); if ('POST' == $_SERVER['REQUEST_METHOD']) { $form->bind($_POST); if ($form->isValid()) { print_r($form->getData()); } } echo render('form.php.html', ['form' => $form]); function render($template, array $parameters = []) { ob_start(); extract($parameters);
public function __construct($username, $password) { $this->username = $username; $this->password = $password; } public function getUsername() { return $this->username; } public function getPassword() { return $this->password; } } $builder = new \Formz\Builder(); $form = $builder->form([$builder->field('username')->nonEmptyText(), $builder->field('password', [$builder->field('main')->nonEmptyText(), $builder->field('confirm')])->verifying('Passwords don\'t match', function ($a, $b) { return $a === $b; }), $builder->field('accept')->boolean()->required()], function ($username, $passwords, $accept) { return new User($username, $passwords['main']); }, function (User $user) { return ['username' => $user->getUsername()]; }); if ('POST' == $_SERVER['REQUEST_METHOD']) { $form->bind($_POST); if ($form->isValid()) { echo '<pre>' . print_r($form->getData(), true) . '</pre>'; } } echo render('form.php.html', ['form' => $form]); function render($template, array $parameters = []) {