TurboGears 2.3 を使用しており、formencode を使用してフォームの検証に取り組んでおり、ガイダンスが必要です
2 つの異なるオブジェクトをカバーするフォームがあります。それらはほとんど同じですが、いくつかの違いがありますフォームを送信するときに、2つのことを検証したい
- いくつかの基本データ
- 特定のオブジェクトの特定のデータ
ここに私のスキーマがあります:
class basicQuestionSchema(Schema):
questionType = validators.OneOf(['selectQuestion', 'yesNoQuestion', 'amountQuestion'])
allow_extra_fields = True
class amount_or_yes_no_question_Schema(Schema):
questionText = validators.NotEmpty()
product_id_radio = object_exist_by_id(entity=Product, not_empty=True)
allow_extra_fields = True
class selectQuestionSchema(Schema):
questionText = validators.NotEmpty()
product_ids = validators.NotEmpty()
allow_extra_fields = True
そして、ここに私のコントローラのメソッドがあります:
@expose()
@validate(validators=basicQuestionSchema(), error_handler=questionEditError)
def saveQuestion(self,**kw):
type = kw['questionType']
if type == 'selectQuestion':
self.save_select_question(**kw)
else:
self.save_amount_or_yes_no_question(**kw)
@validate(validators=selectQuestionSchema(),error_handler=questionEditError)
def save_select_question(self,**kw):
...
Do stuff
...
@validate(validators=amount_or_yes_no_question_Schema(),error_handler=questionEditError)
def save_amount_or_yes_no_question(self,**kw):
...
Do other stuff
...
私がやりたかったのは、異なるスキーマで 2 回検証することでした。最初の @validate のみが検証され、他は検証されない (おそらく無視される) ため、これは機能しません。
それで、私は何を間違っていますか?
助けてくれてありがとう