我有這個非常複雜的形式。所有的領域都必須填補,但填補的過程可以得到保存,繼續進行。所以我需要的是,當最終確認按下時,所有的數據都得到驗證。但因爲它已經保存到數據庫調用validate()不會工作。我用save(validate:false)保存數據,因爲當工作仍在progremm中時我不需要驗證。聖盃確認已經保存到數據庫的數據
如何驗證已保存到數據庫的數據?我必須手動執行嗎?
我有這個非常複雜的形式。所有的領域都必須填補,但填補的過程可以得到保存,繼續進行。所以我需要的是,當最終確認按下時,所有的數據都得到驗證。但因爲它已經保存到數據庫調用validate()不會工作。我用save(validate:false)保存數據,因爲當工作仍在progremm中時我不需要驗證。聖盃確認已經保存到數據庫的數據
如何驗證已保存到數據庫的數據?我必須手動執行嗎?
當您驗證一個已經存在的對象時會發生什麼? 有沒有辦法讓它在檢索之後和驗證之前顯得很髒?
我已經編輯了更詳細的解釋表單封裝的答案,通常可能來自表單輸入的層級步驟過程或更復雜的迭代對象,需要在別處。首先,如果您只需要一個步驟即可捕獲各種信息,然後您很樂意手動處理所有這些信息,並將其存儲在不同類別的大量參數轉儲中,然後查看jquery-ui tabs
。如果您選擇使用製表符的動態功能,即<li><a href="someurl">click</li>
然後將內容動態加載到給定的選項卡,那麼這也會覆蓋單個表單,或者如果您希望在DOM
內更復雜。
無論如何,我編輯的原因並不在它之上,是因爲捕捉多層表單更復雜一些。 所以你不得不step 1
是sent params
通過一個controller
這則passed those params
到一個新的gsp
或甚至的東西,belongs
另一個totally different object
內形式的iteration
。 通常,您將結束:
<g:form action=myDomain" action="doThis">
<!-- this is some iteration that belongs to some other class outside of what i am actually trying to do: -->
<g:each in="someObject" var="p">
<g:hiddenField name="form1.firstName" value="${p.firstName}"/>
<!-- in this case hidden -->
<g:hiddenField name="form1.surName" value="${p.surName}"/>
</g:each>
<!-- this is my actual form -->
<g:textField name="username" />
</g:form>
當表單被提交到控制器
Class MyDomainController {
def doThis(MyBean bean) {
save(bean)
}
}
//這是的src/main /常規
import grails.validation.Validateable
//important for some reason it needs entire collections
// have had issues initialising it without .*
import org.apache.commons.collections.*
Class MyBean implements Validateable {
//this is capturing the current form fields
String username
//This is now collecting our form1 fields
List<MyDetailsBean> form1 = ListUtils.lazyList([], { new MyDetailsBean() } as Factory)
//if that had been 1 instance of it or like as mentioned passed from pervious form and called form2
MyDetailsBean form2
static constraints={
username(nullable:false) //, validator: checkSomething)
}
}
這又是在src/main/groovy
並用於最初收集對象的每次迭代:
import grails.validation.Validateable
Class MyDetailsBean implements Validateable {
String firstName
String surName
}
我已經更新了答案,因爲我建議將對象封裝在一個bean中,而沒有任何關於如何去做這樣的事情的細節。我希望以上清楚。這一切都在飛行中,但如果經過測試,希望一切都如上所述。
在下次更新後添加說明form2的例子。終於驗證你打電話
if (bean.validate() && bean.form2.validate()) {
//all good
}
因爲你是它綁定到類的規則,現在可以作爲驗證過程的一部分被應用的另一個驗證類兩套。
老答案
很簡單地把它的分貝爲什麼地球上,你會想驗證一個驗證的輸入。不管怎樣的解決辦法是在Grails的2 src/groovy/package
這是@Validateable
或
Grails的3 validation bean
:src/main/groovy/package
這implements Validateable
class MyDmainBean {
// must declare id
def id
// Then each object in your real domain class
static constraints = {
id (nullable:true,bindable:true)
importFrom MyDomainClass//, exclude: ['field1']
//field 1 is not included but if field 1 was integer
// in actual domain class and BigDecimal in bean
//then exlude it since it won't bind
}
def formatObject(MyDomainClass domain) {
id=domain.id
..
}
}
現在,您可以撥打
MyDomain record = MyDomain.get(0L)
MyDmainBean bean = new MyDmainBean().formatObject(record)
bean.validate()
也許你需要另一個表來存儲臨時數據,臨時數據是未驗證的數據('確認'未按下) – Nico