atk14/company-number-field 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

atk14/company-number-field

最新稳定版本:v0.1.4

Composer 安装命令:

composer require atk14/company-number-field

包简介

CompanyNumberField is a form field for entering and validating company registration numbers in ATK14 applications

README 文档

README

CompanyNumberField is a field for entering company registration numbers ("IČ" or "IČO" in Czech) into forms in ATK14 applications.

Installation

Just use the Composer:

cd path/to/your/atk14/project/
composer require atk14/company-number-field

Optionally you can symlink the CompanyNumberField file into your project:

ln -s ../../vendor/atk14/company-number-field/src/app/fields/company_number_field.php app/fields/company_number_field.php

Usage in an ATK14 application

CompanyNumberField has method is_valid_for() for re-validation in context of the selected country.

In a form:

<?php
// file: app/forms/users/create_new_form.php
class CreateNewForm extends ApplicationForm {

  function set_up(){
    // ...
    $this->add_field("company_number", new CompanyNumberField([
      "label" => "Company registration number",
    ]));
    $this->add_field("country",new ChoiceField([
      "label" => "Country",
      "choices" => [
        "CZ" => "Czech Republic",
        "SK" => "Slovakia",
        "AT" => "Austria",
        "PL" => "Poland",
        // ...
      ],
    ]));
  }
}

In a controller:

<?php
// file: app/controllers/users_controller.php
class UsersController extends ApplicationController {

  function create_new(){
    // ...
    if($this->request->post() && ($d = $this->form->validate($this->params))){
      // postal code re-validation for the selected country
      if(!$this->form->fields["company_number"]->is_valid_for($d["country"],$d["company_number"],$err)){
        $this->form->set_error("company_number",$err);
        return;
      }

      $user = User::CreateNewRecord($d);
      // ...
    }
  }
}

It's possible to set up CompanyNumberField only to accept postal codes from one specific country. Re-validation is not necessary in this case.

<?php
// file: app/forms/users/create_new_form.php
class CreateNewForm extends ApplicationForm {

  function set_up(){
    // ...
    $this->add_field("company_number", new CompanyNumberField([
      "label" => "Company registration number",
      "country" => "CZ"
    ]));
  }
}

Error message for invalid company_number code or format hints can be specified

<?php
// file: app/forms/users/create_new_form.php
class CreateNewForm extends ApplicationForm {

  function set_up(){
    // ...
    $this->add_field("company_number", new CompanyNumberField([
      "error_messages" => [
        "invalid" => _("Invalid company number"),
      ],
      "format_hints" => [
        "CZ" => _("Please use format NNNNNNNN (8 digits)"),
        "LT" => _("Please use format NNNNNNNNN (9 digits)"),
      ],
    ]));
  }
}

Testing

composer update --dev
cd test
../vendor/bin/run_unit_tests

License

CompanyNumberField is free software distributed under the terms of the MIT license

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-15