【问题标题】:Validating Data using GUMP PHP class使用 GUMP PHP 类验证数据
【发布时间】:2016-01-23 07:15:26
【问题描述】:

请注意,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' =&gt; 'Sisi', 'password' =&gt; 'mypassword', 'email' =&gt; sean@wixel.net', 'gender' =&gt; 'male', 'bio' =&gt; 'This is good! I think I will switch to another language');

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

但根本做不到。

你能帮忙吗?

【问题讨论】:

  • 使用 Laravel 怎么样?
  • @dynamic,没想过用laravel。
  • altho 验证不是 Laravel 的强项,但是 laravel 有非常好的包
  • @dynamic,我明白,但我的项目不基于任何框架。
  • 表单数据在 $_POST 中发送。您需要调整 HTML 输入名称以匹配 PHP 变量,反之亦然。这是非常基本的东西。无论您在做什么,在继续之前,您都应该停下来掌握 PHP 的基础知识。

标签: php forms sanitization formvalidation-plugin


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多