定制 acfo/form-validation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

acfo/form-validation

Composer 安装命令:

composer require acfo/form-validation

包简介

Form validation

README 文档

README

#Form Validation

Strict typed HTML 5 compatible form validation classes.

###Installation

composer require acfo/form-validation

###Usage

Implement the Form interface using the supplied FormImpl trait on your form validation class. Add your form fields as properties (e.g. login form):

class LoginForm implements Form
{
    use FormImpl;
    
    private $email;
    private $password;
    
    public function __construct()
    {
        $this->email = new Email(Requirement::Required);
        $this->password = new Password(Requirement::Required);
    }
    
    public function getEmail()
    {
        return $this->email;
    }
    
    public function getPassword()
    {
        return $this->password();
    }
}

The form validation class can be used to supply the view with client side validation parameters.

<form method="post" action="#">
    <input 
        name="email" 
        type="email" 
        minlength="<?= $loginForm->getEmail()->getMinLength() ?>" 
        maxlength="<?= $loginForm->getEmail()->getMaxLength() ?>"
        pattern="<?= $loginForm->getEmail()->getPattern() ?>"
        <?= $loginForm->getEmail()->getRequired() ?>
        >
    <input 
        name="password" 
        type="password" 
        minlength="<?= $loginForm->getPassword()->getMinLength() ?>" 
        maxlength="<?= $loginForm->getPassword()->getMaxLength() ?>"
        pattern="<?= $loginForm->getPassword()->getPattern() ?>"
        <?= $loginForm->getPassword->getRequired() ?>
        >
    <input type="submit">    
</form>

To perform server side validation of GET or POST form inputs call validate.

$loginForm = new LoginForm();

if (!$loginForm->validate($_POST)) {
    die('please do not try and feed me invalid data');
}

In most scenarios client side validation will trigger error messages and prevent invalid data from being sent to the server. The sole purpose of server side validation is to guard the server from malicious input.

For those scenarios where form data validation relies solely on server side validation, the responsibility for displaying errors may also lie on the server side.

Validation errors are stored on the form field objects. The form field method getError will return an empty string, 'error missing', 'error invalid' or when specifically set by additional business logic 'error requirements'. The error strings can be used to display error messages in the view, e.g.:

CSS

.feedback.missing, 
.feedback.invalid {
    display: none;
}

.error.missing .feedback.missing,
.error.invalid .feedback.invalid {
	color: red;
	display: block;
}

HTML

<form method="post" action="#">
    <div class="<?= $loginForm->getEmail()->getError() ?>">
        <input 
            name="email" 
            type="email" 
            >
        <p class="feedback missing">Please enter your email.</p>
        <p class="feedback invalid">What you entered does not seem to be an email. Please try again.</p>
    </div>
    <div class="<?= $loginForm->getPassword()->getError() ?>">
        <input 
            name="password" 
            type="password" 
            >
        <p class="feedback missing">Please enter your password</p>
        <p class="feedback invalid">Your password has to be between 6 and 60 characters long</p>
    </div>
    <input type="submit">    
</form>

###Predefined form fields

Generic form fields

  • Checkbox
  • ConstListItem
  • Date
  • ListItem
  • Number
  • Text

Account form fields

  • Email
  • Password

Address form fields

  • City
  • Name
  • Street
  • ZipCode

Search form fields

  • SearchString

Enjoy!

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固