是否在PHP中使用另一个JSON来验证JSON对象
请我有一个表单参数如下。我正在尝试根据下面的模式构建动态形式。在我的数据库中,我有一个表列,即JSON。我试图指导不要输入任何数据,除了在“对象”表格中定义的数据
formDefinition = [
{
"key": "name",
"value": "",
"datatype": "string"
},
{
"key": "sex",
"value": " ",
"datatype": "choice"
},
{
"key": "occupation",
"value": "",
"datatype": "string"
},
{
"key": "isRetired",
"value": ,
"datatype": "boolean"
}
]
表:员工和字段名称为详细信息是JSON类型。
details = [{}]
当用户填写表单并提交时,如下所示。
details = [{"name": "something something"}, {"sex": "male"}, {"occupation": "something" }]
我需要一些验证检查,以查看详细信息是否具有与表格中定义的键相同的键,否则应该丢弃错误。
我没有尝试过此代码,因为我不确定是否可能。只是为了说明我要实现的目标。
public function dataValidator($data)
{
foreach($data as $key => $value){
foreach(formDefinition as form){
if($key !== form['key']){
return "invalid"
}
}
}
}
Please I have a form parameter as follows. I am try to build a dynamic form based on the schema below. In my database, I have a table column which is json. I am trying to guide against entering anyhow data except those ones defined in the form array of objects
formDefinition = [
{
"key": "name",
"value": "",
"datatype": "string"
},
{
"key": "sex",
"value": " ",
"datatype": "choice"
},
{
"key": "occupation",
"value": "",
"datatype": "string"
},
{
"key": "isRetired",
"value": ,
"datatype": "boolean"
}
]
Table: employee and field name is details which is a json type.
details = [{}]
When the user fills the form and on submission, it would look as follows.
details = [{"name": "something something"}, {"sex": "male"}, {"occupation": "something" }]
I need some of validation check to see if the details has the same keys as it is defined in the form otherwise it should throw error.
I haven't tried this code as I am not sure if it is possible. Just to illustrate what I am trying to achieve.
public function dataValidator($data)
{
foreach($data as $key => $value){
foreach(formDefinition as form){
if($key !== form['key']){
return "invalid"
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来还不错。但是,只需要一个循环:
下次短提示:尝试一下即可。这可能避免了不必要的问题,因为您可能已经从结果中获得了答案。如果没有,您至少有足够的调试信息与我们分享;-)
Doesn't look that bad. However, only one loop is required:
Short tip for next time: Just try it out. That might avoid unnecessary questions since you might already get the answers from the result. And if not, you at least have sufficient debugging information to share with us ;-)