ZF2过滤选择除空选项以外的设置

时间:2014-03-26 11:27:00

标签: php zend-framework select filter zend-framework2

如果选择框的选项不是空选项,我的目标是过滤。 如果我设置过滤器' required'如果选择框不必为空,我会得到错误。 通过设置' required'如果我得到一个有效的表格,即使我没有在选择元素中做出选择。 有没有其他方法来检查另一个选择而不是空选项?

//class for creating a Fieldset
class FieldsetCreate extends Fieldset implements InputFilterProviderInterface
{
...
  public function getRoofProductsElement()
  {
     $element = new Select(ProductFormInterface::FIELD_ROOFPRODUCT);
     $element->setLabel('Set roof products');
     $element->setAttribute('id', ProductFormInterface::FIELD_ROOFPRODUCT);
     $element->setEmptyOption('Select...');
     $element->setValueOptions(array('1'=>'None', '2' => 'Is Roof'));
     $element->setAttribute('onchange', 'setRoofProductSelection(this);');

     return $element;
  }
...
   public function getInputFilterSpecification()
   {
     return array(
         array(
             'name' => ProductFormInterface::FIELD_TYPESELECTION,
             'required' => false,

         ),
         array(
             'name' => ProductFormInterface::FIELD_ROOFPRODUCT,
             'required' => false,
            'validators' => array(
                new \Zend\Validator\GreaterThan(array('min' => 0)),
             )
          ),
          array(
             'name' => ProductFormInterface::FIELD_MANUFACTURERS,
             'required' => false,
          )
       );
  }
}

//Form Factory to Set Fieldset To Form and return the form
class ProductTypeFormFactory extends ProductAbstractFormFactory 
implements FactoryInterface
{

   public function createService(ServiceLocatorInterface $serviceLocator)
   {
       $form = new ProductForm('productForm');
       $form->addCsrfElement();

       $service = $serviceLocator->get('Product\Service\Product');
       $form->
           add($this->createFieldsetCreate($service->fetchAllProductTypes()));
       $form->setInputFilter(new InputFilter());
       $form->setValidationGroup($this->createValidationGroup());
       return $form;
   }
}

//The Service with Method createProduct
class ProductService
   implements EventManagerAwareInterface, ProductServiceInterface
{
   public function createProduct($typeId, $manufacturerId, $isRoof, $form)
   {
      $formArray = array(
          ProductFormInterface::FIELD_TYPESELECTION => $typeId,
          ProductFormInterface::FIELD_MANUFACTURERS => $manufacturerId,
          ProductFormInterface::FIELD_ROOFPRODUCT => $isRoof

      );

      $form->setData($formArray); 
      if ($form->isValid()) {
          var_dump('VALID');
      } else {
          var_dump($form->getMessages());
      }
   }

这是我能提供的最短代码。 虽然我使用GreaterThan Validator表单是有效的,只需在RoofProduct元素上保留空选项即0

1 个答案:

答案 0 :(得分:0)

创建自己的自定义验证器并在其中设置逻辑。在module / YourModule / src / YourModule / Form / Validator中为您的自定义验证器创建一个类:

CustomValidator.php

<?php
/**
 * Class CustomValidator
 *
 * filename:   CustomValidator.php
 */ 
namespace YourModule\Form\Validator;

use Zend\Validator\AbstractValidator;

class CustomValidator extends AbstractValidator
{
    const INVALID  = 'invalid';
    const WRONG    = 'WRONG';

    protected $messageTemplates     =   array
                                    (
                                        self::INVALID  => "This is invalid."
                                        self::WRONG    => "This is wrong."
                                    );

    public function isValid($value)
    {
        // your logic //

        return $isValid;

    }

    public function isWrong($value)
    {
        // your logic //

        return $isWrong;

    }
}

然后,在表单getInputFilter()中,对于您正在创建的特定输入,添加新的自定义验证器:

'validators' => array
                (
                    array
                    (
                        'name'    => 'YourModule\Form\Validator\CustomValidator',
                        'options' => array
                                     (
                                         'messages' => array
                                                       (
                                                           YourModule\Form\Validator\CustomValidator::INVALID => 'This is just invalid.',
                                                           YourModule\Form\Validator\CustomValidator::WRONG   => 'This is just wrong.',
                                                       )
                                     ),
                        'break_chain_on_failure' => true,
                    ),
                ),

您可以阅读自定义验证器here