如何验证和清理表单复选框数组?

时间:2017-03-08 09:01:41

标签: php html laravel laravel-5 octobercms

我根据OctoberCMSLaravel使用Twig

我有一个带有复选框import json import pymongo import tweepy consumer_key = "" consumer_secret = "" access_key = "" access_secret = "" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.API(auth) class CustomStreamListener(tweepy.StreamListener): def __init__(self, api): self.api = api super(tweepy.StreamListener, self).__init__() self.db = pymongo.MongoClient().test def on_data(self, tweet): self.db.tweets.insert(json.loads(tweet)) def on_error(self, status_code): return True # Don't kill the stream def on_timeout(self): return True # Don't kill the stream sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api)) to_track = ['keyword1', 'keyword2', 'keyword3'] sapi.filter(track = to_track) 的表单。使用循环使用Laravel提交和删除它们。

一切正常,但我怎样才能validate并清理阵列?

验证需要星号*? queued[]

清理,我收到错误'*' => Input::get('queued')

表格

trim() expects parameter 1 to be string, array given

PHP

<form method="POST" action="{{ url_current() }}"> 
    <input type="hidden" name="_handler" value="onDelete" />
    <input type="checkbox" name="queued[]" value="item1" />
    <input type="checkbox" name="queued[]" value="item2" />
    <input type="checkbox" name="queued[]" value="item3" />
    <button type="submit" name="submit" value="delete">Delete Checked</button>
</form>

1 个答案:

答案 0 :(得分:0)

您可以编写自己的自定义验证规则。 您可以在插件boot()方法中定义新规则,如下所示:

Validator::extend('myCustomRule', function($attribute, $value, $parameters) {
    # check if the $value is in valid
    # return true if it is and return false if it is not valid
    return true;
});

您可以预先处理数组,然后检查值。取决于您的验证规则,您可能不需要在此之后进行清理。

然后您可以按照您提供的名称使用自定义规则:

$validator = Validator::make(input(), [
    'queued' => 'myCustomRule',
    ]
);