如何编写可读的 If 语句来验证输入?

发布于 2024-11-07 12:18:15 字数 1450 浏览 6 评论 0原文

我遇到了愚蠢但仍然很重要的问题。我正在为类似“注册”的页面创建验证。我需要检查输入的值是否有害并且符合我的需求。感谢 Fuel 框架,这很容易。接下来...我还需要检查用户名或电子邮件是否尚未被占用。我已经编写了完成这项工作的方法。但当我需要编写所有“if 语句”时,我的问题就出现了。

这就是我现在所拥有的。我不确定这是正确的方法。我可以用“if”和很多级别来编写它(我猜是嵌套)。我可以在那里使用“if/else..if”。你会用什么?

if ( $validation->run() === true ) {

    if ( Diesel::usernameExists( $username ) === false ) {

        $error = 'This username is already taken! Try again...';

    }

    if ( Diesel::emailExists( $email ) === false ) {

        $error = 'This e-mail is already taken! Try again...';

    }


    if ( !isSet( $error ) ) {

        Diesel::signUp( $username, sha1( $password ), $email );


        Session::set_flash( 'notification', 'You have been successfully signed-up! Thanks...' );
        Session::set_flash( 'location', 'sign-in' );

        $this->response->redirect( 'notification' );

    }

} else {

    Session::set_flash( 'error', 'There was something wrong with validation! Try again...' );

    $this->response->redirect( 'sign-up' );

}

换句话说,我的目标是:

1)检查输入的内容是否有害并且符合我的需求($validation->run()),

2)一步一步......用户名不是已被使用(usernameExists())吗?

3) 电子邮件是否已被占用(emailExists())?

4) 可能还有更多...

例如,如果步骤 #1 为 false,则应用程序不需要进一步执行步骤 #2。之后,我需要包含错误消息的字符串,但是,你知道,每个步骤的消息都是不同的。然后我可以传递消息来显示它(如果有消息)!当然,我可以编写有效的代码,但我正在寻找最佳实践......并且我必须遵循 DRY(不要重复自己)和 KISS(保持简单,愚蠢!)原则来保持我的代码可读且易于理解和维护。

I'm encountered silly, but still significant problem. I'm creating validation for "Sign Up"-like page. I need to check that inputted values aren't harmful and fit my needs. Thanks to Fuel framework it's easy enough. Next... I need to check also that username or e-mail isn't already taken. I have written methods that do the job. But my problem comes when I need to write all "if statements".

This is what I have exactly right now. I'm not sure that's the correct way. I could write it all with "if's" and a lot of levels (nesting, I guess). I could use "if/else..if" there. What would you use?

if ( $validation->run() === true ) {

    if ( Diesel::usernameExists( $username ) === false ) {

        $error = 'This username is already taken! Try again...';

    }

    if ( Diesel::emailExists( $email ) === false ) {

        $error = 'This e-mail is already taken! Try again...';

    }


    if ( !isSet( $error ) ) {

        Diesel::signUp( $username, sha1( $password ), $email );


        Session::set_flash( 'notification', 'You have been successfully signed-up! Thanks...' );
        Session::set_flash( 'location', 'sign-in' );

        $this->response->redirect( 'notification' );

    }

} else {

    Session::set_flash( 'error', 'There was something wrong with validation! Try again...' );

    $this->response->redirect( 'sign-up' );

}

My goal, in other words, is to:

1) Check that inputted content aren't harmful and fits my needs ($validation->run()),

2) Step by step... isn't username already taken (usernameExists())?

3) Isn't e-mail already taken (emailExists())?

4) There could be more...

Application don't need to go further to step #2 if step #1 is false, for example. After that I need to have string that contains error message, but, you know, each step's message is different. Then I could pass the message to display it (if there is a message)! Of course, I can write code that works, but I'm looking for best-practices... and I have to follow DRY (Don't Repeat Yourself) and KISS (Keep It Simple, Stupid!) principles to keep my code readable and easy to understand and maintain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-11-14 12:18:15

我会使用 if then else if,我认为这最有意义。
这样,如果第一个 if 为真,这就是它所要做的,
否则将进入下一个。
我认为这就是你想要的。

I would use if then else if, that would make the most sense I think.
That way if the first if is true that is all it will do,
otherwise it will go to the next.
Which I think is what you want.

北陌 2024-11-14 12:18:15

为什么不利用这些来制定验证方法呢?

http://fuelphp.com/docs/classes/validation.html 检查“扩展验证类”,这样它们将在运行验证时运行。您可以按照“错误消息”中所述设置错误消息
(顺便说一句,您也可以使用 $validation->add_callable('classname') 添加任何类、当前控制器或模型,其中 classname 可以是类名称或对象实例)

Why don't you make validation methods out of those?

http://fuelphp.com/docs/classes/validation.html check under "Extending Validation class", that way they'll be run when running validation. You can set the error messages as is described under "Error Messages"
(Btw you can add any class, the current controller or a model as well, using $validation->add_callable('classname') where classname can be both a class name or an object instance)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文