如何验证 Laravel 中的字数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25453262/
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
How to validate words count in laravel
提问by user3150060
I am trying to see how can I validate words count in laravel , for example if a text area accepts only 250 words?
我想看看如何验证 laravel 中的字数,例如,如果文本区域只接受 250 个字?
Could some one help me I am using laravel 4.1
有人可以帮助我我正在使用 laravel 4.1
Thanks
谢谢
采纳答案by c-griffin
I don't think Laravel has a speciic method for this, but you can do it with some simple php.
我不认为 Laravel 对此有特定的方法,但是您可以使用一些简单的 php 来完成。
In your controller:
在您的控制器中:
public function store(){
$text = Input::get('textarea');
if(count(explode(' ', $text)) > 250)
return 'more than 250 words';
}
回答by edumejia30
For Laravel 5.1 and using the advice of Lisa and Richard Le Poidevin, I did the nexts steps based on Laravel 5.1: Validation Docsand all works fine and clean:
对于 Laravel 5.1 并使用 Lisa 和 Richard Le Poidevin 的建议,我根据Laravel 5.1: Validation Docs 执行了以下步骤,并且一切正常:
Created a new ValidatorServiceProvider extending Service Provider in "app/Providers/" for all the validation rules including the Validator::extend method that does the validation and the Validator::replacer that returns a formmated message so we can tell the user the word limit.
在“app/Providers/”中为所有验证规则创建了一个新的 ValidatorServiceProvider 扩展服务提供者,包括进行验证的 Validator::extend 方法和返回格式化消息的 Validator::replacer 以便我们可以告诉用户字数限制.
namespace App\Providers;
use Validator;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(){
Validator::extend('maxwords', function($attribute, $value, $parameters, $validator) {
$words = preg_split( '@\s+@i', trim( $value ) );
if ( count( $words ) <= $parameters[ 0 ] ) {
return true;
}
return false;
});
Validator::replacer('maxwords', function($message, $attribute, $rule, $parameters) {
return str_replace(':maxwords', $parameters[0], $message);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register(){
//
}
}
Then, register the Service Provider in config/app.php:
然后,在 config/app.php 中注册服务提供者:
App\Providers\ValidatorServiceProvider::class,
For the validation language response is resources/lang/en/validation.php:
对于验证语言的响应是 resources/lang/en/validation.php:
"maxwords" => "This field must have less than :maxwords words.",
回答by Lisa
What I ended up doing when I ran into this problem last year was this:
去年遇到这个问题时,我最终做的是:
Validator::extend( 'word_count', function ( $field, $value, $parameters ) {
$words = preg_split( '@\s+@i', $value );
if ( count( $words ) <= $parameters[ 0 ] ) {
return true;
}
return false;
} );
This takes any non-whitespace set of characters and considers it a 'word', then counts the number of results. If it's less than what was sent as the max ($parameters[ 0 ]), then it returns true, otherwise it returns false.
这需要任何非空白字符集并将其视为“单词”,然后计算结果的数量。如果它小于发送的最大值 ($parameters[0]),则返回 true,否则返回 false。
It can be used with the validator feature of Laravel 4, but has not been tested with Laravel 5 yet.
它可以与 Laravel 4 的验证器功能一起使用,但尚未在 Laravel 5 上进行测试。