CFWheels - 使用相同的模型验证两种不同的形式

时间:2012-04-17 11:13:50

标签: model-view-controller coldfusion railo cfwheels

我是轮胎新手(我相信我会在这里发帖很多)所以请耐心等待。

我在“user”的控制器下有两个表单“register”和“login”。所以我的网址看起来像。

/用户/注册/ /用户/登录/

目前在我的模型文件夹中,我只需要在init方法中使用user.cfc验证“register”页面 - 这样就可以了。

基本上......我的问题是......关于我的登录表单的验证;我是否必须始终将验证放入init方法或其他方法?如果是这样,我该怎么做?每种形式的课程都有不同的领域......所以我需要了解一些关于检测当前正在发挥作用的形式的逻辑。

希望这是有道理的。作为参考,我的user.cfc模型目前看起来像这样:

<cfcomponent extends="Model" output="true">

    <cffunction name="init">

        <cfset validate( property='userName', method='validateAlphaNumeric') />
        <cfset validatesPresenceOf( properties='userName') />
        <cfset validatesUniquenessOf( properties='userName') />

        <cfset validatesFormatOf( property='userEmail', type='email', message="Email address is not in a valid format.") />
        <cfset validatesPresenceOf( properties='userEmail') />
        <cfset validatesUniquenessOf( properties='userEmail') />

        <cfset validatesPresenceOf( properties='userPassword') />
        <cfset validatesConfirmationOf( property='userPassword') />

        <cfset validatesLengthOf( property="userToken", allowBlank=true) />

    </cffunction>

    <cffunction name="validateAlphaNumeric" access="private">
        <cfif REFind("[^A-Za-z0-9]", this.userName, 1)>
            <cfset addError( property="userName", message="User name can only contain letters and numbers." ) />
        </cfif>
    </cffunction>

</cfcomponent>

谢谢, 迈克尔。

2 个答案:

答案 0 :(得分:1)

迈克尔,

您需要在init()方法中放置模型验证;车轮要求。但是,我不确定您是否希望或需要对登录页面/调用使用模型验证。

除非我遗漏某些内容,否则当用户登录网站时,您实际上并未改变模型(即创建新用户或更新现有用户)。您只是根据数据库值对它们进行身份验证(检查其用户名/密码组合)。

如果是我,我会使用客户端验证进行登录(字段已完成等)和模型验证以进行注册。

HTH! 克雷格

答案 1 :(得分:0)

转到此网址并向下滚动至:使用&#34;当&#34;,&#34;条件&#34;或&#34;除非&#34;限制验证范围 http://cfwheels.org/docs/1-1/chapter/object-validation

对于你的情况,你可能会在=&#34; onCreate&#34;

时使用