【问题标题】:I want to remove Required and Form Validation from ZendFrameWork 2我想从 ZendFrameWork 2 中删除必需和表单验证
【发布时间】:2015-03-03 07:02:25
【问题描述】:

如何从以下代码中删除必填字段和表单验证。

<?php
namespace Application\Form;

use Zend\Form\Form;

/**
 * Class DomainForm
 * @package Application\Form
 */
class DomainForm extends Form
{
    /**
     * @param string $name
     * @param array $options
     */
    public function __construct($name = '', $options = array())
    {
        parent::__construct($name);
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'bottom-margin');
        $this->setAttribute('id', 'validateForm');

        $this->add(array(
            'name' => 'fullname',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'url',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'id_provider',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'class' => 'form-control'
            ),
            'options' => array(
                'empty_option' => '-',
                'value_options' => $options['provider']
            )
        ));

        $this->add(array(
            'name' => 'id_hosting',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'class' => 'form-control'
            ),
            'options' => array(
                'empty_option' => '-',
                'value_options' => $options['hosting']
            )
        ));

        $this->add(array(
            'name' => 'id_server',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'class' => 'form-control',
            ),
            'options' => array(
                'empty_option' => '-',
                'value_options' => $options['server']
            )
        ));

        $this->add(array(
            'name' => 'host_ftp',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'user_ftp',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'pwd_ftp',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'port_ftp',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'type_ftp',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'class' => 'form-control'
            ),
            'options' => array(
                'empty_option' => '-',
                'value_options' => $options['type_ftp']
            )
        ));

        $this->add(array(
            'name' => 'auth_code',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'expired',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control input-datepicker'
            )
        ));

        $this->add(array(
            'name' => 'info',
            'type' => 'Zend\Form\Element\Textarea',
            'attributes' => array(
                'class' => 'form-control'
            )
        ));

        $this->add(array(
            'name' => 'id_customers',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'class' => 'form-control'
            ),
            'options' => array(
                'empty_option' => '-',
                'value_options' => $options['customers']
            )
        ));

        $this->add(array(
            'name' => 'price',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control',
                'placeholder' => '9999,99'
            )
        ));

        //...
    }
}

【问题讨论】:

  • 更多信息?只有一个必填字段?任何控制器代码?

标签: php forms validation zend-framework2


【解决方案1】:

您可以从控制器更改过滤器以允许该字段为空值:

$form->getInputFilter()->get('YOUR_FIELD')->setAllowEmpty(true);

反正你的问题还不够清楚

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2021-04-25
    相关资源
    最近更新 更多