laravel 如何验证输入不包含特定单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18080638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:14:08  来源:igfitidea点击:

How can I validate input does not contain specific words?

laravellaravel-4

提问by Gareth Oakley

In my signup form I have a nickname field that users can enter text in to identify themselves on my site. In the past some users have entered nicknames which others might find offensive. Laravel provides validation functionality for forms, but how can I ensure that a form field doesn't contain words users might find offensive?

在我的注册表单中,我有一个昵称字段,用户可以在其中输入文本以在我的网站上标识自己。过去,一些用户输入了其他人可能会觉得冒犯的昵称。Laravel 为表单提供验证功能,但我如何确保表单字段不包含用户可能会觉得冒犯的词?

回答by Gareth Oakley

Whilst Laravel has a wide range of validations rules included, checking for the presence of a word from a given list isn't one of them:

虽然 Laravel 包含广泛的验证规则,但检查给定列表中的单词是否存在并不是其中之一:

http://laravel.com/docs/validation#available-validation-rules

http://laravel.com/docs/validation#available-validation-rules

However, Laravel also allows us to create our own custom validation rules:

然而,Laravel 也允许我们创建自己的自定义验证规则:

http://laravel.com/docs/validation#custom-validation-rules

http://laravel.com/docs/validation#custom-validation-rules

We can create validation rules using Validator::extend():

我们可以使用Validator::extend()以下方法创建验证规则:

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

The code above defines a validation rule called not_contains- it looks for presence of each word in $wordsin the fields value and returns false if any are found. Otherwise it returns true to indicate the validation passed.

上面的代码定义了一个名为的验证规则not_contains- 它$words在字段值中查找每个单词的存在,如果找到则返回 false。否则它返回 true 以指示验证通过。

We can then use our rule as normal:

然后我们可以正常使用我们的规则:

$rules = array(
    'nickname' => 'required|not_contains',
);

$messages = array(
    'not_contains' => 'The :attribute must not contain banned words',
);

$validator = Validator::make(Input::all(), $rules, $messages);

if ($validator->fails())
{
    return Redirect::to('register')->withErrors($validator);
}

回答by racl101

In Laravel 5.7 and possibly earlier versions, you could use the built in not_regexrule to check for some strings. Like this for example, within a controller using the validatemethod. Validate form input that expects a name for a dog. :

在 Laravel 5.7 和可能更早的版本中,您可以使用内置not_regex规则来检查某些字符串。像这样,例如,在控制器内使用该validate方法。验证需要狗名的表单输入。:

...
public function update(Request $request) {
  $custom_validation_messages = [
    'not_regex' => "C'mon! Be original. Give your dog a more interesting name!"
  ];
  $this->validate($request, [
    'pet_name' => [ 'not_regex:/^(fido|max|bingo)$/i' ],
  ], $custom_validation_messages);

  ...
}

In this case if the submitted 'pet_name'value is:

在这种情况下,如果提交的'pet_name'值为:

  • fido
  • FIDO
  • MaX
  • MAx
  • BinGO
  • bingo
  • etc.
  • 菲多
  • 菲多
  • 最大限度
  • 最大限度
  • 答对了
  • 答对了
  • 等等。

Then validation will fail.

那么验证将失败。

For the inverse of this, i.e. you only want Fido, Max or Bingo then you could use the regexrule like so:

与此相反,即您只想要 Fido、Max 或 Bingo,那么您可以使用如下regex规则:

[ 'regex:/^(fido|max|bingo)$/i' ]

See Laravel Validation (not regex).

请参阅Laravel 验证(不是正则表达式)