Laravel - 常量表达式在x:\ project \ app \ Model.php

时间:2017-05-19 09:47:38

标签: php laravel validation

我正在尝试使用laravel验证一些数据,我已经在模型中实现了这样的数据:

class Collection扩展了Model {     protected $ guarded = ['id'];

public $allowedDataTypes = [

'Date',
'Text',
'Number',
'Email',
'Checkbox',
'Image',
'File',

];





25    public static $rules =  array([
26
27  'fields.*.fieldName' =>  
28      [       
29          'unique' => 'Please ensure that the fields are uniquely named.',
30          'required' => 'You must specify a name for your fields.'
31      ],
32
33  'fields.*.dataType' =>
34      [
35          'required', 'You must specify a data type for your fields.',
36          'in:'.implode(',', $allowedDataTypes)
37      ]
38
39  ]);

然而,在提交数据时,据说让它经历了这个验证,它给了我在标题中看到的错误:

  

常量表达式包含{link to中的无效操作   模型}:39

我也对代码行进行了编号,我不确定为什么会这样做。

提前感谢任何指导。

1 个答案:

答案 0 :(得分:1)

不可能在类变量声明中使用implode函数。这是因为类变量是在运行时之前启动的。

如果要使用implode函数,可以使用函数或使用构造函数。

例如:

public function rules()
{
    return [
        'fields.*.fieldName' => [
            'unique' => 'Please ensure that the fields are uniquely named.',
            'required' => 'You must specify a name for your fields.'
        ],

        'fields.*.dataType' =>      [
            'required', 'You must specify a data type for your fields.',
            'in:'.implode(',', $this->allowedDataTypes)
        ]

    ];
}
相关问题