1

stackoverflow に関する質問「Slimphp で GUMP を使用してデータを検証する」がありますが、この質問は重複していません。

GUMP PHP 入力検証クラスを使用してフォーム データ ( https://github.com/Wixel/GUMPで入手可能) を検証したいのですが、それをフォームに追加する方法がわかりませんでした。

これはかなり単純なGUMPの例です

<?php
require "../gump.class.php";
$validator = new GUMP();

// Set the data
$_POST = array( 
    'username' => 'Sisi', 
    'password' => 'mypassword', 
    'email'    => 'sean@wixel.net', 
    'gender'   => 'm', 
    'bio'      => 'This is good! I think I will switch to another language');

$_POST = $validator->sanitize($_POST); // You don't have to sanitize, but it's safest to do so.

// Let's define the rules and filters
$rules = array( 
'username' => 'required|alpha_numeric|max_len,100|min_len,6',
'password' => 'required|max_len,100|min_len,6',
'email'    => 'required|valid_email',
'gender'   => 'required|exact_len,1',
'bio'      => 'required');

$filters = array( 
'username'    => 'trim|sanitize_string',
'password'    => 'trim|base64_encode',
'email'       => 'trim|sanitize_email',
'gender'      => 'trim');

$_POST = $validator->filter($_POST, $filters);

// You can run filter() or validate() first
$validated = $validator->validate($_POST, $rules);

// Check if validation was successful
if($validated === TRUE)
{
    echo "Successful Validation\n\n";
    print_r($_POST); // You can now use POST data safely
    exit;

}else{
    // You should know what form fields to expect, so you can reference them here for custom messages
    echo "There were errors with the data you provided:\n";
    // Or you can simply use the built in helper to generate the error messages for you
    // Passing a boolean true to is returns the errors as html, otherwise it returns an array
    echo $validator->get_readable_errors(true);
}

ここで、テスト目的で作成したデモ フォームで、ユーザー名とパスワードを収集して印刷します

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user_name = $_POST["user_name"];
$user_password = $_POST["user_password"];

echo $user_name;
echo $user_password;
}

私はこれを交換する必要があることを知っています

$_POST = array('username' => 'Sisi', 'password' => 'mypassword', 'email' => sean@wixel.net', 'gender' => 'male', 'bio' => 'This is good! I think I will switch to another language');

$user_name = $_POST["user_name"]; $user_password = $_POST["user_password"];

しかし、単にできません。

手伝ってくれますか?

4

1 に答える 1

0

これを試して

<?php
require "../gump.class.php";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

$validator = new GUMP();

$user_name = $_POST['user_name'];
$user_password = $_POST['user_password'];

$_POST = array(
    'username' => $user_name,
    'password' => $user_password);

$_POST = $validator->sanitize($_POST); // You don't have to sanitize, but it's safest to do so.

// Let's define the rules and filters
$rules = array( 
'username' => 'required|alpha_numeric|max_len,100|min_len,6',
'password' => 'required|max_len,100|min_len,6');

$filters = array( 
'username'    => 'trim|sanitize_string',
'password'    => 'trim|base64_encode');

$_POST = $validator->filter($_POST, $filters);

// You can run filter() or validate() first
$validated = $validator->validate($_POST, $rules);

// Check if validation was successful
if($validated === TRUE)
{
    echo $user_name;
    echo $user_password;

    exit;
}else{
    // You should know what form fields to expect, so you can reference them here for custom messages
    echo "There were errors with the data you provided:\n";
    // Or you can simply use the built in helper to generate the error messages for you
    // Passing a boolean true to is returns the errors as html, otherwise it returns an array
    echo $validator->get_readable_errors(true);
}
于 2015-10-23T19:38:37.733 に答える