私の CI4 学習では、ユーザーのサインイン機能をシミュレートすることから始めました。コントローラー、2 つのビュー (ここには示されていませんが、実際には単純なページです。1 つはほとんど単一のフォームであり、もう 1 つは「空白の」成功 HTML ページです)、Validation.php ファイルに一連のカスタム ルールがあります。そして、すべてのカスタム ルールを実装する最初のメソッドを含む CustomRule.php ファイル (最終的には、すべてを Validation.php ファイルに設定したいと考えています)。より良いアイデアがないため、CustomRules.php ファイルを app\Config\ フォルダーに貼り付けました。
これが私の問題です:
私の人生では、「user_validated」と呼ばれるカスタム ルール関数に (フォームから) 追加のパラメーターを Validation サービスに渡す方法がわかりません。CI4 ドキュメントでは、追加のパラメーターを受け入れるときにカスタム関数が対応する必要があるものについて説明していますが、これらの追加パラメーターをカスタム関数に渡すために検証サービスをトリガーする方法については説明していません。文字列のように-私が知る限り、他には何も入りません。どうすればこれを回避できますか?
< $validation->setRuleGroup('user_signin'); を挿入しようとしました。> 検証の呼び出しの前に、次を使用して、ルール グループの設定を検証の呼び出しに移動できることがわかりました。パラメータとしてルールグループがないと機能しないようです(?)。これは、追加データがカスタム ルールのメソッドに渡されるトリガーとなるものではないようです。
私のハックからの抜粋を以下に追加します。
知識のある方が正しい方向に向けていただければ幸いです。
app\Controllers\SignupTest.php 内:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class SignupTest extends BaseController
{
public function index() { // redirection from the default to signup(), signin(), ...
return $this->signup();
}
public function signup() {
helper(['form']);
$validation = \Config\Services::validation();
if ($this->request->getPost()) { // still TBD: any different to using $this->request->getGetPost() ?
$validationResult = $this->validate('user_signin'); // set the rules to use: 'user_signin', 'user_signup'
if (!$validationResult) {
$validationErrors = $validation->getErrors();
return view('SignupTestView', $validationErrors); // redisplay simple html form view with list of validation errors
} else {
return view('SignupTestViewSuccess'); // display view to show success
}
} else {
return view('SignupTestView'); // initial display, in the event of there being no POST data
}
}
}
\app\Config\CustomRules.php 内:
<?php
namespace Config;
use App\Models\UserModel;
//--------------------------------------------------------------------
// Custom Rule Functions
//--------------------------------------------------------------------
class CustomRules
{
public function user_validated(string $str, string $fields = NULL, array $data = NULL, string &$error = NULL) : bool{
$user_email_offered = $str;
$user_password_offered = ''; // to be extracted using $fields = explode(',', $fields), but $fields is never provided in the call to this user_validated method
if (($user_email_offered !== NULL) && ($user_password_offered !== NULL)) {
$usermodel = new UserModel(); // intended to create a UserEntity to permit connectivity to the database
$user_found = $usermodel->find($user_email_offered); // we're going to assume that user_email is unique (which is a rule configured in the database table)
if ($user_found === NULL) { // check if user exists before doing the more involved checks in the else-if section below, which may throw exceptions if there's nothing to compare (?)
...
}
}
\app\Config\Validation.php 内:
?php
namespace Config;
class Validation
{
//--------------------------------------------------------------------
// Setup
//--------------------------------------------------------------------
/**
* Stores the classes that contain the
* rules that are available.
*
* @var array
*/
public $ruleSets = [
\CodeIgniter\Validation\Rules::class,
\CodeIgniter\Validation\FormatRules::class,
\CodeIgniter\Validation\FileRules::class,
\CodeIgniter\Validation\CreditCardRules::class,
\Config\CustomRules::class,
];
/**
* Specifies the views that are used to display the
* errors.
*
* @var array
*/
public $templates = [
'list' => 'CodeIgniter\Validation\Views\list',
'single' => 'CodeIgniter\Validation\Views\single',
];
//--------------------------------------------------------------------
// Custom Rules
//--------------------------------------------------------------------
/* configurable limits for validation rules array below*/
const user_email_min_lenth = 9;
const user_email_max_lenth = 50;
const user_password_min_lenth = 6;
const user_password_max_lenth = 25;
public $user_signin = [
'user_email' => [
'label' => 'e-mail address',
'rules' => 'trim|required|valid_email|user_validated', // user_validated is custom rule, that will have a custom error message
'errors' => [
'required' => 'You must provide an {field}',
'valid_email' => 'Please enter a valid {field}',
]
],
'user_password' => [
'label' => 'password',
'rules' => 'trim|required',
'errors' => [
'required' => 'Enter a {field} to sign in',
'user_password_check' => 'No such user/{field} combination found',
]