Beego – 创建模型表单和ORM

huangapple go评论73阅读模式
英文:

Beego - Creating Model Form and ORM

问题

我已经使用以下代码创建了一个模型:

type UserProfile struct {
    Id      int     `orm:"auto"`
    Name    string  `orm:"size(100)"`
    Email   string  `orm:"size(100)"`
    Type    string  `orm:"size(30)"`
    Admin   bool
    Car     []*Car  `orm:"reverse(many)"`
}

有没有办法直接使用这个结构来渲染一个表单?我认为 valid:required 负责验证,但我们如何控制表单的渲染呢?

英文:

I have created a model using the following code:

type UserProfile struct {
    Id    				int    			`orm:"auto"`
    Name  				string 			`orm:"size(100)"`
    Email 				string 			`orm:"size(100)"`
    Type 				string 			`orm:"size(30)"`
    Admin 				bool
    Car 				[]*Car 			`orm:"reverse(many)"`
}

Is there any way I can render a form directly using this structure? I think valid:required rakes care of validation, but how do we control form rendering.

答案1

得分: 7

在控制器中:

func (this *AddController) Get() {
   this.Data["Form"] = &UserProfile{}
   this.TplNames = "index.tpl"
}

在模板中:

<form action="" method="post">
    {{.Form | renderform}}
</form>
英文:

In controller:

func (this *AddController) Get() {
   this.Data[&quot;Form&quot;] = &amp; UserProfile{}
   this.TplNames = &quot;index.tpl&quot;

}

In template :

&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
    {{.Form | renderform}}
&lt;/form&gt;

huangapple
  • 本文由 发表于 2015年3月30日 08:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/29336869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定