在我的應用程序中,我允許用戶創建一個包含他們想要的任何HTML表單字段(例如文本輸入,textarea,select等)的表單。我想讓用戶能夠爲每個字段定義0個或更多的累積驗證規則(最多可能有25個不同的驗證規則)。我應該如何建模?用於動態表單字段驗證的數據庫設計
這裏有一個潛在的解決方案:
============================================================
| Id | FieldId | ValidationRuleType | ValidationRuleDetail |
============================================================
| 1 | 25 | Required | NULL |
------------------------------------------------------------
| 2 | 26 | Minimum Length | 5 |
------------------------------------------------------------
| 3 | 26 | Maximum Length | 12 |
------------------------------------------------------------
...
使用上面的設計,也許在大多數情況下,ValidationRuleType可能只是「正則表達式」(或者從查找表中的值,如ValidationRuleTypeId = 1「正則表達式「),並將以下內容用於ValidationRuleDetail:
// Added bonus of this approach is that users who know regex could define their own patterns
.{1,} // Any character, 1 or more times. Use for "Required"
.{5,} // Any character, 5 or more times. Use for "Minimum Length = 5"
.{,12} // Any character, 12 or less times. Use for "Maximum Length = 12"
問題是此解決方案是EAV。這是一件壞事,對吧?
另一種可能的解決方案:
=============================================================
| Id | FieldId | Required | Minimum Length | Maximum Length |
=============================================================
| 1 | 25 | TRUE | NULL | NULL |
-------------------------------------------------------------
| 2 | 26 | NULL | 5 | 12 |
-------------------------------------------------------------
...
這是更好?我對使用哪種方法存在爭議。任何指導,我可以得到非常感謝。
我更喜歡創建基於規則表的設計。感謝@Explosion Pills – 2014-05-16 03:16:35