验证输入的自定义规则不包含错误词

custom rule for validate input does not contain bad words

我有一个 API,其中包含不同的表格,例如:帖子、问题、评论和 .. 我不想让用户使用不当词来填写这些表格的字段并显示它们使用这些词时出错。

所以我搜索并编写了这个 FormRequest 以验证类别描述:

 public function rules(){
        Validator::extend('not_contains', function($attribute, $value, $parameters){
            // Banned words
            $words = array('f***', 's***' , 'a****' , 'b***');
                foreach ($words as $word) {
                    if (stripos($value, $word) !== false) {
                        return false;
                    }
                }
            return true;
        });


        $rules = [
            'description'=>['required','max:500' ,'not_contains'],
        ];

        if ('PUT' === $this->method()) {
            $rules['title'] = 'required|unique:categories,id,' . $this->route('category_id');
        }
        else{
            $rules['title'] = 'required|unique:categories|';
        }

            return $rules;
    }

}

它有效,但我不想在任何 formRequest 中使用这个 not_contains 代码,所以我创建了这个自定义规则:

class CheckBadWords implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        Validator::extend('bad_words', function($attribute, $value, $parameters){
            // Banned words
            $words = array('f***', 's***' , 'a****' , 'b***');
            foreach ($words as $word) {
                if (stripos($value, $word) !== false) {
                    return false;
                }
            }
            return true;
        });
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'bad word used!';
    }
}


在表单请求中,我将其添加到 $rules 中:

   $rules = [
            'description'=>['required','max:500' ,new checkBadWords()],
        ];

所以当我发送请求时,此规则适用于每个包含并给出验证错误:bad word used! 即使对于不包含这些坏词的内容

将您的规则 类 passes() 方法更改为

public function passes($attribute, $value)
{
    $words = array('f***', 's***' , 'a****' , 'b***');

    foreach ($words as $word) {
        if (stripos($value, $word) !== false) {
            return false;
        }
    }

    return true;
}

您可以像下面这样编写自定义规则

class CheckBadWords implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string $attribute
     * @param  mixed $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $words = array('f***', 's***' , 'a****' , 'b***');
        $wordFound = false;
        foreach ($words as $badword) {

            if (str_contains($value, $badword)) {
                $wordFound = true;
                break;
            }


        }

        if ($wordFound) {

            return false;
        }

        return true;

    }


/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return 'bad word used!';
}
}

如果您使用的不是最新版本的 php,则将 str_contains 更改为 stripos