dimensi0n/simple-form

简单表单生成器

1.0.2 2021-05-06 17:03 UTC

This package is auto-updated.

Last update: 2024-09-09 20:56:14 UTC


README

PHP Composer

提供简单的表单生成器和数据验证器

如何使用它?

安装它

composer require dimensi0n/simple-form

这个

$form = new \SimpleForm\Form('Login');
        $form->addInput('email', 'email', 'Email address') // name, type, label
		   ->addInput('password', 'password', 'Mot de passe') // name, type, label
           ->addSubmit('Submit'); // content

echo $form->renderHTML;

将渲染这个

<form action="" method="POST">
    <label for="email">Email Address</label>
    <input type="email" name="email" class="form-control" id="email"/>
    <label for="password">Password</label>
    <input type="password" name="password" class="form-control" id="password"/>
    <button type="submit" class="btn btn-primary" id="Submit">Submit</button>
</form>

为了验证表单并获取其值,你只需写下这个

$values = $form->getValues($_POST); // check if all fields are filled, also applies trim(), stripslashes() and htmlspecialchars()

if (isset($values['empty'])) {
    foreach($values['empty'] as $empty_value) {
        echo $empty_value.' is empty'; // 'Name of the empty field' is empty
    }
} else {
    echo 'Email : '.$values['email'];
    echo 'Password : '.$values['password'];
}

自定义渲染

使用bootstrap的示例

$form = new \SimpleForm\Form('Login');
        $form->addInput('email', 'email', 'Email address') // name, type, label
		   ->addInput('password', 'password', 'Mot de passe') // name, type, label
           ->addSubmit('Submit'); // content

<div class="container">
    <?= $form.formStart() ?>
    <div class="form-group">
        <?= $form.formInput('email') ?> // Will render label and input
    </div>
    <div class="form-group">
        <?= $form.formInput('password') ?> // Will render label and input
    </div>
    <?= $form.formEnd() ?>
</div>

获取更多信息,请查看API文档:https://dimensi0n.github.io/simple-form/api