【问题标题】:phalcon validate form fields without saving to databasephalcon 验证表单字段而不保存到数据库
【发布时间】:2014-04-14 10:03:18
【问题描述】:

我需要验证表单字段并在不保存到数据库的情况下对其进行操作。 这就是我所做的

在控制器中

<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;

class UsersController extends ControllerBase {

    public function loginAction() {

        if($this->request->isPost()) {

            $user = new Users();
            $validates = $user->validation($this->request->getPost());  
            // now validation works fine, but cancelOnFail in model doesn't seems to work, 

            if($validates) {
                echo 'valid inputs';
            }
            else {
                print_r($user->getMessages());
                // now how can we show these error messages below the corresponding input fields in the view.
                // we would also like to show error message as follows, if a field has more than one validation conditions,
                // Eg: say username have notempty and valid e-mail validation set in model so if username is empty show only not empty message,
                // similarly if username is not empty and if its not a valid e-mail , show not valid email message.
            }
            exit();
        }
    }

}
?>   

我正在尝试从模型中进行验证,如下所示

<?php
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\Validator\PresenceOf;
use Phalcon\Mvc\Model\Validator\Email;

class Users extends \Phalcon\Mvc\Model {

    public function validation() {

        $this->validate(new PresenceOf(
            array(
               'field'  => 'username',
               'message' => 'Username is required.',
               'cancelOnFail' => true
            )
        ));

        $this->validate(new Email(
            array(
                'field'  => 'username',
                'message' => 'Username must be a valid e-mail.'
            )
        ));

        $this->validate(new PresenceOf(
            array(
                'field'  => 'password',
                'message' => 'Password is required.'
            )
        ));

        return $this->validationHasFailed() != true;
    }
}
?>

我的视图文件如下

 <?php 
    echo $this->tag->form(array("users/login", "role" => "form"));
    echo $this->tag->textField(array('username', 'class' => 'form-control', 'placeholder' => 'E-mail', 'type' => 'email', 'tabindex' => 1)); 
    echo $this->tag->passwordField(array('password', 'class' => 'form-control', 'placeholder' => 'Password', 'type' => 'password', 'tabindex' => 2)); 
    echo $this->tag->submitButton(array('Login','class' => 'btn btn-sm btn-success btn-block', 'tabindex' => 5)); 
?>
</form>

我怎样才能实现以下目标,

1) 检查表单字段是否按照控制器模型中给出的正确验证。

2) 不希望保存表单数据,仅验证它。

3) 在视图中的输入字段下方显示相应的错误消息。

谢谢

【问题讨论】:

    标签: php validation phalcon


    【解决方案1】:

    您需要创建表单,绑定您的实体,然后在发布请求时进行验证。见http://docs.phalconphp.com/en/latest/reference/forms.html#validation

    编辑:要显示错误消息,您可以在控制器中执行此操作

    // ...
    $messages = array();
    foreach ($user->getMessages() as $message) {
        $messages[$message->getField()] = $message->getMessage();
    }
    
    $this->view->messages = $messages;
    //...
    

    现在您的视图中有$messages

    我认为在这种情况下你真的应该使用表格。您试图验证模型中的用户登录,但这是一个简单的表单验证。在模型中,您在应用中验证用户的业务规则。

    【讨论】:

    • 我看到了那个链接,但是我以不同的方式完成了我的视图页面,我更新了我上面的模型、视图和控制器以获得更好的想法,验证现在工作正常..检查我的 cmets在控制器中现在要实现的目标..thankz.
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 2017-03-31
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多