simettric/simple-form

在PHP中处理表单的一种简单方法。

v1.1 2016-12-07 22:21 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:18:09 UTC


README

Build Status

SensioLabsInsight

在PHP中处理表单的一种简单方法。

安装

composer require simettric/simple-form

创建表单

使用 FormBuilder

$builder = new FormBuilder($config);

$builder->create("message")
        ->add("firstName")
        ->add("lastName")
        ->add("email",   new InputTypeField(array("type"=>"email", "validators"=> new Email() )))
        ->add("subject", new ChoiceField(array("choices"=>array()))) //InArray is implicit unless we configure our own ChoiceValidator in the "validators" key
        ->add("message", new TextareaField(array(
              "validators" => array(
                    new NotEmpty(), 
                    new StringLength(array("min"=>4))
        )));
        
$data_array = array("firstName"=>"John");
$form       = $builder->getForm($data_array);

创建表单类

class MessageForm extends AbstractForm{

    public function configure(FormBuilder $builder)
    {


        $builder->add("firstName")
                ->add("lastName")
                ->add("email",   new InputTypeField(array("type"=>"email", "validators"=> new Email() )))
                ->add("subject", new ChoiceField(array("choices"=>array()))) //ChoiceValidator is implicit unless we configure our own ChoiceValidator in the "validators" key
                ->add("message", new TextareaField(array(
                                               "validators" => array(
                                                     new NotEmpty(), 
                                                     new StringLength(array("min"=>4))
                                         )))

    }
    
    
    public function getName()
    {
        return 'message';
    }


}

$data_array = array("firstName"=>"John");
$form       = new MessageForm($data_array, new FormBuilder());

验证表单

SimpleForm 使用 Zend 验证器 来管理表单中的字段验证。

$builder->add("message", new TextareaField(array(
                              "label"      => "Write your message",
                              "validators" => array(
                                    new NotEmpty(), 
                                    new StringLength(array("min"=>4)
                              )))
);

在控制器中,您可以绑定请求数据并检查表单是否有效

$form->bind( $_POST["message"] );

if($form->isValid()){

   echo $form->getValue("firstName");

}

渲染表单

<?php echo $form["firstName"] ?>

输出

<div>
  
  <label for="contact_firstName">firstName</label>
  <input type="text" name="contact[firstName]" required="required">
  <span class="error">Error message</span>

</div>

*注意:您可以在 Form::getName() 方法中返回一个空值,以便设置一个干净的输入名称,如

  <input type="text" name="firstName" required="required">

*注意2:使用 Field::getRow("label") 方法,结果类似,但带有自定义标签。

<?php echo $form["firstName"]->getRow('Text for the label tag') ?>

您可以单独渲染每个HTML标签

<?php echo $form["firstName"]->getLabelTag() ?>

<?php echo $form["firstName"]->getInputTag(array("class"=>"the attribute value")) ?>

<?php echo $form["firstName"]->getErrorTag() ?>

此外,您还可以获取错误值的数组

<?php foreach( $form["firstName"]->getErrorArray() as $error ){} ?>