Created
December 26, 2015 18:26
-
-
Save Ocramius/02c3a9e6db97ca4a018a to your computer and use it in GitHub Desktop.
Form validation from HTML form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$formHtml = <<<'HTML' | |
<form action="%s" method="post"> | |
<label for="email">Email:</label> | |
<input | |
type="email" | |
id="email" | |
name="email" | |
value="" | |
aria-describedby="email-description" | |
data-reuse-submitted-value="true" | |
data-validator="email-address" | |
required="required" | |
/> | |
<span id="email-description" class="help">Enter a valid email address</span> | |
<input type="submit"/> | |
</form> | |
HTML; | |
$form = $formFactory->fromHtml($formHtml); | |
var_dump($form->validate($_POST)); // returns form validion result VO | |
echo $form->asString(); |
Those are controller concerns imo, not form's.
How about this: https://github.com/xtreamwayz/html-form-validator
@xtreamwayz that kinda resembles my thoughts. I forgot to mention that @jstoone already started work on this at jstoone/HTMLFormValidator#1
Opened https://github.com/xtreamwayz/html-form-validator/issues/1 to avoid you folks stumbling on each other by accident (do it on purpose!)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Ocramius nice idea. Probably I would use 3 different validation methods:
validateFromGlobals()
($_POST
or$_GET
depending on form method)validateArray()
andvalidateRequest()
for PSR-7 request object.Also, you probably need to know when form is submitted and when not.
Regarding implementation, using https://github.com/Level-2/Transphporm should be quite simple.