PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP-Phalcon验证表单字段而不保存到数据库大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_801_0@我需要验证表单字段并在不保存到数据库的情况下进行操作.
这就是我所做的

@H_801_0@在控制器中

@H_801_0@
<?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();
        }
    }

}
?>   
@H_801_0@我正在尝试从模型进行验证,如下所示

@H_801_0@
<?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;
    }
}
?>
@H_801_0@我的查看文件如下

@H_801_0@
 <?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>
@H_801_0@我如何实现以下目标,

@H_801_0@1)检查表单字段是否按照控制器中模型中的规定正确验证.

@H_801_0@2)我不想保存表单数据,仅对其进行验证.

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

@H_801_0@谢谢

解决方法:

@H_801_0@您需要创建表单,绑定您的实体,然后根据请求进行验证.查看http://docs.phalconphp.com/en/latest/reference/forms.html#validation

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

@H_801_0@
// ...
$messages = array();
foreach ($user->getmessages() as $messagE) {
    $messages[$message->getField()] = $message->getmessage();
}

$this->view->messages = $messages;
//...
@H_801_0@现在您可以看到$messages.

@H_801_0@我认为您确实应该在这种情况下使用表格.您尝试验证模型中的用户登录,但这是一个简单的表单验证.在模型中,您可以验证应用程序中用户的业务规则.

大佬总结

以上是大佬教程为你收集整理的PHP-Phalcon验证表单字段而不保存到数据库全部内容,希望文章能够帮你解决PHP-Phalcon验证表单字段而不保存到数据库所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: