Formidable
Formidable是一个可以处理表单的PHP库。它解析了HTML表单,并允许您从PHP代码中操作它,然后渲染它。
它如何工作?
步骤1:下载并安装Formidable
通过作曲家:
Formidable\”: \”dev–master\”
}
}\”>
{
\"require\" : {
\"gregwar/ Formidable \" : \" dev-master \"
}
}
或带有存储库的克隆:
git clone https://githu*b*.c*om/Gregwar/Formidable.git
或下载它:
- 下载.zip
- 下载.tar.gz
步骤2:在HTML中写下您的表格
首先,您必须在HTML中编写代码,例如:
<form method=\”post\”>
Enter your name:
<input type=\”text\” name=\”name\” /><br />
<input type=\”submit\” />
</form>\’>
<!-- forms/example.html --> < form method =\" post \" > Enter your name: < input type =\" text \" name =\" name \" /> < br /> < input type =\" submit \" /> </ form >
步骤3:给它Formidable
在您的php代码中,将您的表格赋予Formidable :
Formidable will parse the form and use it to check integrity
// on the server-side
$form = new Gregwar\\ Formidable \\Form(\’forms/example.html\’);
$form->handle(function() {
echo \”Form OK!\”;
}, function($errors) {
echo \”Errors: <br/>\”;
foreach ($errors as $error) {
echo \”$error<br />\”;
}
});
echo $form;\”>
<?php session_start (); include ( \' vendor/autoload.php \' ); // Formidable will parse the form and use it to check integrity // on the server-side $ form = new Gregwar \\ Formidable \\ Form ( \' forms/example.html \' ); $ form -> handle ( function () { echo \" Form OK! \" ; }, function ( $ errors ) { echo \" Errors: <br/> \" ; foreach ( $ errors as $ error ) { echo \" $ error <br /> \" ; } }); echo $ form ;
简单,对吗?
步骤4:享受魔术
然后,您可以使用Formidable API播放您的表格:
<?php // Will set the value of the field $ form -> name = \" Bob \" ; // Will get the value of the field $ name = $ form -> name ; // Adds a constraint on the name $ form -> addConstraint ( \' name \' , function ( $ value ) { if ( strlen ( $ value ) < 10 ) { return \' Your name should be at least 10 characters! \' ; } }); // Adds a constraint on the whole form $ form -> addConstraint ( function ( $ form ) { if ( $ form -> getValue ( \' pass1 \' ) != $ form -> getValue ( \' pass2 \' )) { return \' The passwords are different \' ; } });
您也可以尝试更改表单并直接在HTML代码中添加约束:
< input type =\" text \" name =\" name \" minlength =\" 10 \" />
当将检查服务器端约束时,这将迫使文本至少长10个字符。
想要验证码来保护您的表格吗?
< input type =\" captcha \" name =\" code \" />
这将在客户端上生成图像和输入字段,并在服务器端上使用会话来检查代码是否正确。
请注意,这将使用Gregwar/Captcha库的依赖关系(您必须使用Composer安装依赖项)。
类型
支持以下输入类型:
-
input标签,具有类型:-
text -
number或numeric,请参阅min和max属性 -
int或integer,请参阅min和max属性 file-
checkbox -
radio -
hidden -
password -
captcha,将自动生成图像 -
date,将生成三个选择,并将DateTime作为数据返回 -
multiradio和multicheckbox(请参阅源部分)
-
-
textarea -
select
属性
请注意,某些属性不是html-valid,例如maxlength :
< input type =\" text \" name =\" name \" maxlength =\" 10 \" />
它不会以HTML形式渲染,但将用于检查完整性。
这是可用属性的列表:
-
minlength:值的最小长度 -
maxlength:值的最大长度 -
regex:值应尊重的正则 -
min(数字):最小值 -
max(数字):最大值 -
required:说明该字段是必需的 -
readonly:该字段已阅读,不应是修饰符 -
value:该字段的默认值 -
min-entries:指定您应该提供多个的最小条目数(请参见下文) -
max-entries:指定您可以提供多个的最大条目数(请参见下文) -
entries:指定多个倍数的最小条目和最大条目数(见下文)
API
您可以在$form对象上调用这些方法:
-
posted():返回true如果表格发布 -
check():检查表格并返回有效性错误的数组 -
handle($callback, $errorCallback),此快捷方式呼叫发布并检查(),并且如果表格有效,将调用$callback,$errorCallbackelse -
setAttribute($field, $attr, $value):在字段上设置一个额外属性 -
getAttribute($field, $attr):在字段上获取一个额外的属性 -
source($source, $values):馈送源(请参阅“源”部分) -
setPlaceholder($name, $value):设置一个占位符价值(见下文) -
addConstraint($field, $callback):在字段上添加自定义约束,将带有字段值调用callback,如果没有问题,则应返回false或错误字符串。如果您只是通过封闭,则将闭合以作为参数传递的表格来调用,然后可以进行一些涉及多个字段或表单信息的测试。 -
setValue($field, $value):设置字段的值 -
getValue($field):获取字段的值 -
setValues(array $values):设置某些字段的值 -
getValues():获取所有字段的值
CSRF保护
额外的CSRF令牌会自动以表格插入并在提交时检查。因此,您的所有表格都将得到保护。
CSRF令牌的存在和有效性用于检查呼叫posted方法时发布的表格是否已发布(它在handle内部使用)
如果您在form中指定name属性,则该特定表单的CSRF令牌将有所不同,如果同一页面上有多个表单,则可以实现Formidable的差异。
语言
可以使用setLanguage()设置错误的语言:
<?php
// Will set the language to french for errors
$ form -> setLanguage ( new Gregwar \\ Formidable \\ Language \\ French );
检查您的语言是否在Language目录中支持,请随时参与!
来源
您可以使用采购系统动态填充select ,一个multiradio或多multicheckbox :
< input type =\" multicheckbox \" name =\" colours \" source =\" colours \" />
然后用source填充它:
<?php $ form -> source ( \' colours \' , array ( \' red \' , \' yellow \' , \' blue \' ));
这将由一些复选框渲染。
您可以以select方式这样做:
< select name =\" colour \" > < options source =\" colours \" /> < option value =\" other \" > Other </ option > </ select >
然后用相同的方法来源
从字符串创建表单
您可以从文件或字符串创建表单,将自动检测到:
Formidable\\Form(\'<form method=\”post\”>
<select name=\”colour\”>
<option value=\”blue\”>Blue</option>
<option selected value=\”red\”>Red</option>
<option value=\”green\”>Green</option>
</select>
</form>\’);
echo $form->getValue(\’colour\’) . \”\\n\”;
// red
// Sets the color to blue
$form->setValue(\’colour\’, \’blue\’);
echo $form;
/* Will display:
<form method=\”post\”>
<select name=\”colour\” >
<option selected=\”selected\” value=\”blue\”>Blue</option>
<option value=\”red\”>Red</option>
<option value=\”green\”>Green</option>
</select>
<input type=\”hidden\” name=\”posted_token\” value=\”d293dc38017381b6086ff1a856c1e8fe43738c60\” />
</form>
*/\”>
<?php $ form = new Gregwar \\ Formidable \\ Form ( \' <form method=\"post\"> <select name=\"colour\"> <option value=\"blue\">Blue</option> <option selected value=\"red\">Red</option> <option value=\"green\">Green</option> </select> </form> \' ); echo $ form -> getValue ( \' colour \' ) . \"\\n\" ; // red // Sets the color to blue $ form -> setValue ( \' colour \' , \' blue \' ); echo $ form ; /* Will display: <form method=\"post\"> <select name=\"colour\" > <option selected=\"selected\" value=\"blue\">Blue</option> <option value=\"red\">Red</option> <option value=\"green\">Green</option> </select> <input type=\"hidden\" name=\"posted_token\" value=\"d293dc38017381b6086ff1a856c1e8fe43738c60\" /> </form> */
映射
您也可以使用mapping属性来填充表单或在数组或对象中获取表单数据,例如:
Formidable\\Form(\'<form method=\”post\”>
<input type=\”text\” name=\”name\” mapping=\”name\” />
</form>\’);
$form->setData($person);
echo $form;
/*
Will output something like:
<form method=\”post\”>
<input required=\”required\” type=\”text\” name=\”name\” value=\”Jack\” />
<input type=\”hidden\” name=\”posted_token\” value=\”aa27f437cc6127c244db14361fd614af51c79aac\” />
</form>
*/\”>
<?php class Person { protected $ name ; public function getName () { return $ this -> name ; } public function setName ( $ name ) { $ this -> name = $ name ; } } $ person = new Person ; $ person -> setName ( \' Jack \' ); $ form = new Gregwar \\ Formidable \\ Form ( \' <form method=\"post\"> <input type=\"text\" name=\"name\" mapping=\"name\" /> </form> \' ); $ form -> setData ( $ person ); echo $ form ; /* Will output something like: <form method=\"post\"> <input required=\"required\" type=\"text\" name=\"name\" value=\"Jack\" /> <input type=\"hidden\" name=\"posted_token\" value=\"aa27f437cc6127c244db14361fd614af51c79aac\" /> </form> */
请注意,该映射使用Symfony PropertyAccessor,然后可以使用上面示例中的访问者来填充属性。
您可以使用:
-
getData($entity = array()):填充和返回实体,带有数据填充的数据 -
setData($entity):用实体属性填充表单
创建多个子形式
您可以使用<multiple>标签将多个子形式添加到页面上:
< form method =\" post \" > Film name: < input type =\" text \" name =\" film_name \" mapping =\" name \" /> < h2 > Actors </ h2 > < multiple name =\" actors \" mapping =\" actors \" > First name: < input name =\" first_name \" mapping =\" firstName \" /> < br /> Last name: < input name =\" last_name \" mapping =\" lastName \" /> < br /> </ multiple > < input type =\" submit \" /> </ form >
这样, <multiple>可以完全像一个字段一样使用,但它将包含一系列元素。
某些JS将在页面中注入,并允许您添加/删除一些元素。
您可以使用min-entries和max-entries约束来设置倍数中条目数的限制。
如果您指定min-entries和max-entries的相同值,或指定entries的值(实际上是这样做的别名),则将osr输入的数量固定,不需要JavaScript。
将动态数据添加到表单中
在某些情况下,您需要将自定义数据添加到表单中,有两种方法可以做到这一点。
第一种方法:使用占位符
{{ something }}语法允许您简单地从代码注入数据,例如:
Formidable\\Form(\'<form method=\”post\”>
Hello {{ name }}!
</form>\’);
$form->setPlaceholder(\’name\’, \’Bob\’);
echo $form;\”>
<?php $ form = new Gregwar \\ Formidable \\ Form ( \' <form method=\"post\"> Hello {{ name }}! </form> \' ); $ form -> setPlaceholder ( \' name \' , \' Bob \' ); echo $ form ;
在上面的示例中, {{ name }}将呈现为Bob 。
请注意,无论如何都可以使用<form>和输入标签中的占位符:
Formidable\\Form(\'<form method=\”post\”>
<span style=\”color:{{ color }}\”>Hello</span>
</form>\’);
$form->setPlaceholder(\’color\’, \’red\’);
echo $form;\”>
<?php $ form = new Gregwar \\ Formidable \\ Form ( \' <form method=\"post\"> <span style=\"color:{{ color }}\">Hello</span> </form> \' ); $ form -> setPlaceholder ( \' color \' , \' red \' ); echo $ form ;
第二种方法:使用PHP表格
您也可以使用PHP(例如模板)编写表单:
<form>
<?php echo $ label ; ?> : <input type=\"text\" name=\"name\" />
<input type=\"submit\" />
</form>
然后实现您的表单将模板变量作为第二个参数:
<?php
$ form = new Gregwar \\ Formidable \\ Form ( \' the-above-form.php \' , array ( \' label \' => \' Your name \' ));
$label将使用PHP解释。
缓存
出于表演原因,您可能需要缓存解析的表格。
为此,只需将true传递为构造函数的第三个论点:
Formidable\\Form(\’form.html\’, null, true);
\”>
<?php /** * Parsed data for the form will be serialized and stored in a cache file, * if you use this form often, this will offer you better performances. */ $ form = new Gregwar \\ Formidable \\ Form ( \' form.html \' , null , true );
这将使用Gregwar/Cache系统,您需要获得此存储库的作曲家依赖性或手动安装。默认情况下,缓存文件将在运行脚本的位置中写入cache目录中。
尝试在examples/目录中运行performances.php脚本,这将为您提供缓存性能增益的示例。
您还可以将Gregwar\\Cache\\Cache的实例作为第三个参数,该参数将允许您设置高速缓存目录。
执照
Gregwar\\ Formidable属于MIT许可,请查看LICENSE文件以获取更多信息。
历史
v2.0.0最终支持PHP <5.6
v2.1.0删除验证库库的硬依赖
