Advertisement
TheFan1968

ZF3: Formular-Definition mit InputFilter

May 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.45 KB | None | 0 0
  1. <?php
  2. namespace Hm\Forms;
  3. use Zend\Form\Form;
  4. use Zend\Form\Element\Text;
  5. use Zend\Form\Element\Select;
  6. use Zend\Form\Element\Submit;
  7. use Zend\Filter\StringTrim;
  8. use Zend\Filter\Digits;
  9. use Zend\Validator\StringLength;
  10. use Zend\Validator\InArray;
  11. use Zend\Validator\NotEmpty;
  12. // use Zend\Debug\Debug;
  13.  
  14.  
  15. class HmSelectForm extends Form {
  16.  
  17.     private $bsnr;
  18.    
  19.     public function __construct(array $bsnr){
  20.         parent::__construct();
  21.        
  22.         // <form>-Tag Eigenschaften
  23.         parent::setAttributes([
  24.             'method'    => 'post',
  25. //             'action'    => $url, <-- Muss im View erstellt werden, es sei denn es geht in die layout.phtml
  26.             'name'      => 'SelForm'
  27.         ]);
  28.        
  29.         // Feld: BSNR
  30.         $this->add([
  31.             'name'      => 'bsnr',
  32.             'type'      => Text::class,
  33.             'attributes'=> [
  34.                 'class'         => 'form-control',
  35.                 'placeholder'   => '72xxxxx',
  36.                 'maxlength'     => 7,
  37.                 'id'            => 'selForm01'
  38.             ],
  39.             'options'   => [
  40.                 'label'     => 'BSNR: '
  41.             ],
  42.         ]);
  43.        
  44.         // Feld: Jahresauswahl
  45.         $this->add([
  46.             'name'      => 'jahr',
  47.             'type'      => Select::class,
  48.             'attributes'=> [
  49.                 'class'     => 'form-control',
  50.                 'id'        => 'selForm02'
  51.             ],
  52.             'options'   => [
  53.                 'label'     => 'Jahr: ',
  54.             ],
  55.         ]);
  56.        
  57.         // Button: Submit
  58.         $this->add([
  59.             'name'      => 'auswahl',
  60.             'type'      => Submit::class,
  61.             'attributes'=> [
  62.                 'class' => 'btn btn-warning',
  63.                 'value'     => 'Anzeigen'
  64.             ],
  65.         ]);
  66.        
  67.        
  68.         //Definition der Filter und Validatoren
  69.         $this->getInputFilter()->add(
  70.            
  71.             [
  72.             'name'          => 'bsnr',
  73.             'required'      => true,
  74.             'filters'       => [['name' => StringTrim::class],
  75.                                 ['name' => Digits::class]],
  76.             'validators'    => [
  77.                 [
  78.                     'name'                  => NotEmpty::class,            
  79.                     'options'               => [
  80.                         'message'           => 'Bitte eine gültige 7-stellige BSNR eingeben!'
  81.                     ],
  82.                 ],
  83.                 [
  84.                     'name'                  => StringLength::class,
  85.                     'options'               => [
  86.                         'min'               => 7,
  87.                         'max'               => 7,
  88.                         'message'           => 'Eine BSNR hat immer %max% Zeichen Länge und enthält keine Buchstaben.'
  89.                     ],
  90.                 ],
  91.                 [
  92.                     'name'                  => InArray::class,
  93.                     'options'               => [
  94.                         'message'           => 'Die angegebene BSNR existiert nicht in der Datenbank!',
  95.                         'haystack'          => $bsnr
  96.                     ],
  97.                 ],
  98.             ],
  99.             ]
  100.            
  101.             );
  102.     }
  103.    
  104.     public function setOptionData($jahr){
  105.         $this->get('jahr')->setOptions(array_merge($this->get('jahr')->getOptions(),['value_options' => $jahr]));
  106.     }
  107.    
  108.    
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement