简体   繁体   English

Phalcon框架中的表单元素验证(检查元素是否存在)

[英]Form element validation in Phalcon framework (check if element exists)

I am beginner in Phalcon framework, I have to validate form elements in .volt page 我是Phalcon框架的初学者,我必须在.volt页面中验证表单元素

I have one form class file where I write hidden filed for record edit purpose, I'm storing record's id in hidden filed when its in edit mode 我有一个表单类文件,我写了隐藏的文件用于记录编辑目的,当它处于编辑模式时,我将记录的id存储在隐藏文件中

if ($options["edit"] == 1) {
       // $tax_categories_id = new Hidden("tax_categories_id");
        $this->add(new Hidden('tax_categories_id'));
        //$this->add($tax_categories_id); 
    }  

The problem is when I rendering this hidden filed in add.volt 问题是当我在add.volt渲染这个隐藏的文件时

 {{ form.render('tax_categories_id')}} 

Its working fine in time of edit mode, but in new record time its give error 它在编辑模式下工作正常,但在新的记录时间它给出了错误

Phalcon\\Forms\\Exception: Element with ID=tax_categories_id is not a part of the form Phalcon \\ Forms \\ Exception:ID = tax_categories_id的元素不是表单的一部分

I know the why error is coming but i am not able to validate this field in .volt file 我知道错误即将发生但我无法在.volt文件中验证此字段

In the controller can you set your $options variable and then check for it inside of the view? 在控制器中你可以设置$ options变量然后在视图内部检查它吗?

//controller.php
$this->view->setVar('options', $options);


//view.volt
{% if options['edit'] %}
    {{ form.render('tax_categories_id')}} 
{% endif %]

Just check if the element is exist 只需检查元素是否存在

// add.volt
{% if form.has('tax_categories_id') %}
    {{ form.render('tax_categories_id') }}
{% endif %}

Assuming you have crated something close to: 假设你有一些接近的东西:

<?php

use Phalcon\Forms\Form,
    Phalcon\Forms\Element\Text,
    Phalcon\Forms\Element\Hidden;

class UsersForm extends Form
{
    public function initialize($options = [])
    {
        if ( isset($options['edit']) && $options['edit'] ) {
            $this->add(new Hidden('id'));
        }

        $this->add(new Text('name'));
    }
}

So! 所以! Depending on options, you may have one field declared, or two instead. 根据选项,您可以声明一个字段,或者两个字段。 Now when someone sends you this form back, for validation you have to set it up again with proper $options['edit'] , depending on if you have $_REQUEST['id'] declared or not: 现在,当有人向您发送此表单时,为了进行验证,您必须使用正确的$options['edit']再次设置它,具体取决于您是否声明了$_REQUEST['id']

$form = null;
if( isset($_REQUEST['id']) ) {
    $form = new UsersForm();
} else {
    $form = new UsersForm(['edit' => true]);
}

$form->bind($_REQUEST);
if($form->isValid()) {
    //...
}

Quite an advanced (but with some gaps anyway) manual is here . 相当先进(但有一些差距)手册就在这里 Bet you were there already, but just in case. 打赌你已经在那里了,但为了以防万一。

Btw, form are iterators & traversables, so you can loop over them to render only elements, that are declared. 顺便说一句,表单是迭代器和遍历,所以你可以循环它们只渲染声明的元素。 Writing this because you have put {{ form.render('tax_categories_id')}} as an example and that makes me feel like you are generating fields by hand. 写这个是因为你把{{ form.render('tax_categories_id')}}作为一个例子,这让我觉得你是手工生成字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM