atk14/company-number-field

CompanyNumberField 是 ATK14 应用中用于输入和验证公司注册号的表单字段

v0.1.2 2024-05-21 16:16 UTC

This package is auto-updated.

Last update: 2024-09-21 17:01:18 UTC


README

CompanyNumberField 是 ATK14 应用中用于在表单中输入公司注册号(在捷克语中为 "IČ" 或 "IČO")的字段。

安装

只需使用 Composer

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

可选地,您可以将 CompanyNumberField 文件链接到您的项目

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

在 ATK14 应用中使用

CompanyNumberField 有 is_valid_for() 方法,用于在选定国家的上下文中进行重新验证。

在表单中

<?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",
        // ...
      ],
    ]));
  }
}

在控制器中

<?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);
      // ...
    }
  }
}

可以将 CompanyNumberField 设置为仅接受来自一个特定国家的邮政编码。在这种情况下不需要重新验证。

<?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"
    ]));
  }
}

可以指定无效公司号代码或格式的错误信息

<?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)"),
      ],
    ]));
  }
}

测试

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

许可证

CompanyNumberField 是免费软件,根据MIT 许可证条款分发。