如何验证数组大小

时间:2016-02-17 07:42:31

标签: php laravel laravel-5 laravel-5.1 laravel-5.2

我有一个名为tags的参数我现在以标签[1,2,3,4,5]格式获取该参数我需要在标签参数中验证该事物只有4个值允许。

我该怎么做?

我尝试了各种方法但没有成功。

$rules = array(
    'ids'  => 'required',
    'note' => 'required|array|min:3',
    'tags' => 'required|array|max:4',           
);

我无法正确验证这件事。

我甚至不允许输入4个参数。

2 个答案:

答案 0 :(得分:0)

创建自定义验证规则(例如class WIFISegment( object ): """Equivalent of radio WiFi channel. Only Ap and WDS devices support SendFrom().""" def __init__( self ): # Helpers instantiation. self.channelhelper = ns.wifi.YansWifiChannelHelper.Default() self.phyhelper = ns.wifi.YansWifiPhyHelper.Default() self.wifihelper = ns.wifi.WifiHelper.Default() self.machelper = ns.wifi.NqosWifiMacHelper.Default() # Setting channel to phyhelper. self.channel = self.channelhelper.Create() self.phyhelper.SetChannel( self.channel ) def add( self, node, port=None, intfName=None, mode=None ): """Connect Mininet node to the segment. Will create WifiNetDevice with Mac type specified in the MacHelper (default: AdhocWifiMac). node: Mininet node port: node port number (optional) intfName: node tap interface name (optional) mode: TapBridge mode (UseLocal or UseBridge) (optional)""" # Check if this Mininet node has assigned an underlying ns-3 node. if hasattr( node, 'nsNode' ) and node.nsNode is not None: # If it is assigned, go ahead. pass else: # If not, create new ns-3 node and assign it to this Mininet node. node.nsNode = ns.network.Node() allNodes.append( node ) # Install new device to the ns-3 node, using provided helpers. device = self.wifihelper.Install( self.phyhelper, self.machelper, node.nsNode ).Get( 0 ) mobilityhelper = ns.mobility.MobilityHelper() # Install mobility object to the ns-3 node. mobilityhelper.Install( node.nsNode ) # If port number is not specified... if port is None: # ...obtain it automatically. port = node.newPort() # If interface name is not specified... if intfName is None: # ...obtain it automatically. intfName = Link.intfName( node, port ) # classmethod # In the specified Mininet node, create TBIntf bridged with the 'device'. tb = TBIntf( intfName, node, port, node.nsNode, device, mode ) return tb )并在服务提供商中声明。使用此ValidateArraySize

之类的验证规则
array_size:4

答案 1 :(得分:0)

public function rules()
    {
        $limit = 3;

        return [
            'notas' => 'required|array|size:'.$limit
        ];
    }

public function messages()
    {
        return [
            'notas.required' => 'O valor de Notas é obrigatório.',
            'notas.array' => 'O valor de Notas deve ser um array.',
            'notas.size' => 'Aquantidade de notas não pode ser maior que :size'
        ];
    }