Skip to content
Advertisement

Validating data in a stored JSON file in the DB using YII framework

I am trying to write a rule in my Yii project to add an exempt rule when we have a data point collected on a stored JSON. The following is my sample Yii script for the validation rule

JavaScript

In my database, I have a column where I store extra variables as a stored JSON as shown in the diagram below, and all the extra attributes are stored under the column additional attributes which is a stored JSON

JavaScript

I would like to add a validation in my code that exempt a record under the additional attribute, I am new to the Yii framework and I have gone through the validation solution on the Yii documentation with no clear way forward

Advertisement

Answer

Yii2 don’t have JSON specific validations, it can validate is as a string, or you could validate each index from array.

But if you need more precise validation, you have to create your own validation rule for that. First add your custom rule name to the rules method:

JavaScript

Create your custom rule method:

JavaScript

Read more on Creating Validators

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement