在Zend表单上禁用Inarray验证器

时间:2016-02-23 15:25:32

标签: php forms zend-framework

我有一个表单,直到我添加了两个下拉菜单。从那以后,在提交表格后我得到了错误:

  

haystack选项是必需的

这是我的ProjectForm.php

$.trim( $(this).text() ) == $.trim( searchString )

我想我需要通过添加<?php namespace Project\Form; use Zend\Form\Form; use Zend\Form\Element\Text; use Zend\Form\Fieldset; use Zend\Form\Element\Select; use Zend\Form\Element\Checkbox; use Zend\Form\Element\Date; use Zend\Config\Factory; use Zend\Form\Element\Button; use Zend\Form\Element\Textarea; use Zend\Validator\Callback; use Zend\InputFilter\InputFilterProviderInterface; use Zend\Form\Element\File; class ProjectForm extends Form implements InputFilterProviderInterface { const KEY_PROJECT_NAME = "project_name"; const KEY_PROJECT_DUE_DTTM = "project_due_dttm"; const KEY_PROJECT_DESCRIPTION = "project_description"; const KEY_PROJECT_CONFIG = "project_config"; const KEY_PROJECT_TYPES = "project_types"; const KEY_PROJECT_WORKFLOW_CONFIG = "project_workflow_config"; const KEY_PROJECT_WORKFLOW_TYPES = "project_workflow_types"; const KEY_PROJECT_FILE = "project_file"; const KEY_SAVE_BTN = "project_save_btn"; const KEY_CANCEL_BTN = "project_cancel_btn"; public function __construct($name = null, $options = array()) { parent::__construct($name); $this->setAttribute('method', 'post'); $this->setAttribute('class', 'form-horizontal'); $name = new Text(self::KEY_PROJECT_NAME); $name->setAttribute("id", self::KEY_PROJECT_NAME); $name->setLabel("Name"); $duedttm = new Text(self::KEY_PROJECT_DUE_DTTM); $duedttm->setAttribute("id", self::KEY_PROJECT_DUE_DTTM); $duedttm->setLabel("Due Date"); $description = new Textarea(self::KEY_PROJECT_DESCRIPTION); $description->setAttribute("id", self::KEY_PROJECT_DESCRIPTION); $description->setLabel("Description"); $config = new Textarea(self::KEY_PROJECT_CONFIG); $config->setAttribute("id", self::KEY_PROJECT_CONFIG); $config->setLabel("Configuration"); $projectTypeDropDown = new Select(self::KEY_PROJECT_TYPES); $projectTypeDropDown->setLabel('Project Type:'); $projectTypeDropDown->setValueOptions($options["project"]); $wfConfig = new Textarea(self::KEY_PROJECT_WORKFLOW_CONFIG); $wfConfig->setAttribute("id", self::KEY_PROJECT_WORKFLOW_CONFIG); $wfConfig->setLabel("Workflow Configuration"); $projectWorkflowDropDown = new Select(self::KEY_PROJECT_WORKFLOW_TYPES); $projectWorkflowDropDown->setLabel('Workflow Type:'); $projectWorkflowDropDown->setValueOptions(array()); $file = new File(self::KEY_PROJECT_FILE); $file->setAttribute("id", self::KEY_PROJECT_FILE); $file->setAttribute("multiple", true); $file->setLabel("File"); $save = new Button(self::KEY_SAVE_BTN); $save->setAttributes(array("id", self::KEY_SAVE_BTN)); $save->setLabel("Save"); $save->setValue("Save"); $cancel = new Button(self::KEY_CANCEL_BTN); $cancel->setAttributes(array("id", self::KEY_CANCEL_BTN)); $cancel->setLabel("Cancel"); $this->add($name); $this->add($duedttm); $this->add($description); $this->add($config); $this->add($projectTypeDropDown); $this->add($wfConfig); $this->add($projectWorkflowDropDown); $this->add($file); $this->add($save); $this->add($cancel); } public function isValidJSON($value, $options) { try { JSON::decode($value); return true; } catch (JSONException $e) { return false; } } public function isValidDateTime($value, $options) { if (false === date_create($value)) { return false; } return true; } /** * (non-PHPdoc) * @see \Zend\InputFilter\InputFilterProviderInterface::getInputFilterSpecification() */ public function getInputFilterSpecification() { return array( ProjectForm::KEY_PROJECT_CONFIG => array( 'required' => true, 'filters' => array( array( 'name' => 'Zend\Filter\StringTrim' ), ), 'validators' => array( new Callback( array( $this, 'isValidJSON' )), ), ), ProjectForm::KEY_PROJECT_WORKFLOW_CONFIG => array( 'required' => false, 'filters' => array( array( 'name' => 'Zend\Filter\StringTrim' ), ), 'validators' => array( new Callback( array( $this, 'isValidJSON' )), ), ), ProjectForm::KEY_PROJECT_DUE_DTTM => array( 'required' => false, 'filters' => array( array( 'name' => 'Zend\Filter\StringTrim' ), ), 'validators' => array( new Callback( array( $this, 'isValidDateTime' )), ), ), ); } } 来停用inarray_validator,但我不确定在哪里添加它。

2 个答案:

答案 0 :(得分:0)

我建议你传递一个空字符串作为唯一的valueOption。在你的javascript中,你刷新select的<option>标签以摆脱这个空选项。 此外,您还必须通过添加:

来禁用Select元素的默认验证器
$projectWorkflowDropDown->setOptions(array('disable_inarray_validator' => true));

或者

$projectWorkflowDropDown->setDisableInArrayValidator(true);

如果您不添加此选项,则在提交表单时,Zend Form将告诉您在构建表单时提供的值不在haystack中。这是一个安全过程,在许多情况下非常有用,而不是在你的。

答案 1 :(得分:0)

由于我使用的Zend版本,我必须在验证表单之前将其添加到控制器:

// Make certain to merge the files info!
$post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());

//Deals with validation of dynamic form 
$projectType = $projectTypes[$post["project_types"]]; 
$form->get('project_workflow_types')->setValueOptions($workflowTypes[$projectType]); 


// set the form instance's data
$form->setData($post);