日期:类[class grailsappl1]的属性[date]不能为null

时间:2014-02-23 23:53:52

标签: grails groovy datepicker

我有一个表单,我正在创建特定灯具和地点名称的日期,我无法创建日期。我试图在我的夹具类中设置日期nullable:true来测试场地的创建是否有效,结果显示场地但是日期不是。

这是我的域类

package cricketfixturesgrailsapp


class Fixture {

   Date date 
   String venue

   // hasMany  = [scores:Scores]

     static hasMany = [team:Team]

    static constraints = {
      date nullable: true
    }
}

这是我的FixtureController类

package cricketfixturesgrailsapp

class FixtureController {

 //def index() { }
    def fixtureService
    //def scaffold=true

    def add() {
        if (request.method == 'GET') {
            [fixtureBean: new Fixture()]
        }
        else {
            def fixture = fixtureService.createFixture(params.fixture?.date, params.fixture?.venue)
            if (!fixture.hasErrors()) {
                redirect action: 'show', id: fixture.id
                return
            }

            [fixtureBean: fixture]
        }
    }

  def show() {
        def fixture = Fixture.get(params.id)
        if (fixture) {
            [fixtureBean: fixture]
        }

    }
            def find() {
        if (request.method == 'POST') {
            def fixtures = Fixture.findAllByDate(params.date)
            if (fixtures) {
                if (fixtures.size() > 1) {
                    render view: 'selection', model: [fixtures: fixtures]
                }
                else {
                    redirect action: 'show', id: fixtures[0].id
                }
            }
            else {
                [message: 'fixtures.not.found']
            }
        }
    }
}

add.gsp文件

<%@ page contentType="text/html;charset=UTF-8" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add fixture</title>
    </head>

    <body id = "add">
      <h2><g:if test="${!fixtureBean.id}">New </g:if>Fixture:</h2>

      <g:form action="${fixtureBean.id ? 'edit' : 'add'}" id="${fixtureBean?.id}">
            <table>
                <tr>
                    <th>
                        Date: <span class="errors"><g:fieldError bean="${fixtureBean}" field="date" /></span>
                        <br/>
                        <g:datePicker name="fixtureBean.date" value="${ fixtureBean.date}" precision="day"></g:datePicker>
                    </th>
                </tr>
                <tr>
                    <th>
                        <g:render template="/common/formField"
                                  model="[name:'fixture', bean:fixtureBean, field:'venue', label:'Venue']" />
                    </th>
                </tr>

                <tr>
                    <td>
                        <p><input type="submit" value="${ fixtureBean.id ? 'Update' : 'Add'} Fixture"/></p>
                    </td>
                </tr>
            </table>
        </g:form>
    </body>
</html>

    </body>
</html>

不确定问题是否与&lt;:datePicker&gt;有关。标记

另外,我有一个名为FixtureService的服务类,不确定日期问题是否是由此类引起的

package cricketfixturesgrailsapp

//import grails.transaction.Transactional
import java.util.Date

//@Transactional
class FixtureService {

    //def serviceMethod() {

      Fixture createFixture(Date date, String venue)
        {
            def fixture = new Fixture(date: date, venue:venue)
            fixture.save()
            fixture
        }

    void updateFixture(Fixture fixture,Date date, String venue)
    {
        fixture.date = date
        fixture.venue = venue
        fixture.save()
    }

    Team createTeam(String teamName1, String teamName2, long fixtureId){

        def team = new Team(teamName1: teamName1, teamName2: teamName2, fixture: Fixture.load(fixtureId))
        team.save()
        team

    }
    void updateTeam(String teamName1, String teamName2, long fixtureId) {
        team.teamName1 = teamName1
        team.teamName2 = teamName2
        team.fixture = Fixture.load(fixtureId)
        team.save()
    }

    }

1 个答案:

答案 0 :(得分:0)

你的名字不匹配。

如果你有:

<g:datePicker name="fixtureBean.date"/>

然后在控制器中应该站起来:

fixtureService.createFixture(params.fixtureBean?.date

而不是

fixtureService.createFixture(params.fixture?.date