llpereiras

zf2 Form

Nov 28th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.98 KB | None | 0 0
  1. <?php
  2. namespace Courses\Form;
  3.  
  4. use Zend\InputFilter\InputFilter,
  5.     Zend\Form\Element,
  6.     Zend\Form\Fieldset,
  7.     Zend\Form\Form,
  8.     Zend\InputFilter\Factory as InputFactory,
  9.     Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator,
  10.     Zend\Validator\StringLength,
  11.     Zend\Validator\NotEmpty,
  12.     Zend\Validator\Regex,
  13.     Zend\Form\Factory,
  14.     Zend\Form\Element\Textarea;
  15.  
  16. class CourseModeloSaveForm extends Form{
  17.    
  18.     public function __construct($categorias, $estruturaPai = false)
  19.     {
  20.         parent::__construct();
  21.         $this->setAttribute('method', 'post')
  22.              ->setAttribute('id', 'coursemodelosave')
  23.              ->setAttribute('enctype', 'multipart/form-data')
  24.              ->setHydrator(new ClassMethodsHydrator(false))
  25.              ->setInputFilter(new InputFilter());
  26.  
  27.         $ccmo_nome = new Element\Text('ccmo_nome');
  28.         $ccmo_nome->setLabel('Nome')
  29.                   ->setAttribute('title','Nome do curso')
  30.                   ->setAttribute('maxlength',200)
  31.                   ->setAttribute('class','span17')
  32.                   ->setAttribute('style','width:646px')
  33.                   ->setAttribute('id','ccmo_nome')
  34.                   ->setAttribute('placeholder','Nome do curso');
  35.        
  36.         $ccmo_url = new Element\Text('ccmo_url');
  37.         $ccmo_url->setLabel('Endereço da página')
  38.                  ->setAttribute('title','Endereço da página')
  39.                  ->setAttribute('id','ccmo_url')
  40.                  ->setAttribute('size',255)
  41.                  ->setAttribute('maxlength',255)
  42.                  ->setAttribute('class','span14')
  43.                  ->setAttribute('style','margin: 0px 4px;')
  44.                  ->setAttribute('id','ccmo_url')
  45.                  ->setAttribute('placeholder','Url do curso...Ex: curso-excel-2007-iniciante');
  46.        
  47.         $ccmo_preco = new Element\Number('ccmo_preco');
  48.         $ccmo_preco->setLabel('Valor do curso')
  49.                  ->setLabelAttributes(array('id' => 'lbl_ccmo_preco'))
  50.                  ->setAttribute('title','Preço entre R$ 29,90 e R$ 99,99')
  51.                  ->setAttribute('size', 7)
  52.                  ->setAttribute('maxlength',7)
  53.                  ->setAttribute('class','span3')
  54.                  ->setAttribute('id','ccmo_preco')
  55.                  ->setAttribute('style','display:block')
  56.                  ->setAttribute('step','0.01')
  57.                  ->setAttribute('description','reais')
  58.                  ->setAttribute('placeholder','29,90');
  59.  
  60.         $categoriasOk = array('' => 'Selecione uma categoria...');
  61.         foreach ($categorias as $categoria) {
  62.             $categoriasOk[$categoria->ccat_id] = $categoria->ccat_nome;
  63.         }
  64.         $categoria = new Element\Select('ccat_id');
  65.         $categoria->setLabel('Categoria')
  66.                   ->setAttribute('class','span6')
  67.                   ->setAttribute('id', 'categoria')
  68.                   ->setAttribute('style', 'margin: -5px 4px')
  69.                   ->setValueOptions($categoriasOk);
  70.  
  71.         if(!$estruturaPai){
  72.             $estruturaOk = array('' => 'Selecione uma sub-categoria...');
  73.         }else{
  74.             $estruturaOk = array();
  75.             foreach ($estruturaPai as $estrutura) {
  76.                 if($estrutura['cest_pai'])
  77.                     $estruturaOk[$estrutura['cest_id']] = $estrutura['cest_nome'];
  78.             }
  79.         }
  80.         $estruturaPai = $estruturaOk;
  81.         $estrutura = new Element\Select('cest_id');
  82.         $estrutura->setLabel('Sub-Categoria')
  83.                   ->setAttribute('class','span7')
  84.                   ->setAttribute('id', 'estrutura')
  85.                   ->setAttribute('style', 'margin: -5px 4px')
  86.                   ->setValueOptions($estruturaPai);
  87.  
  88.  
  89.         $ccmo_gratuito = new Element\Select('ccmo_gratuito');
  90.         $ccmo_gratuito->setLabel('Tipo de cobrança')
  91.                       ->setAttribute('id', 'ccmo_gratuito')
  92.                       ->setEmptyOption('Selecione o tipo de cobrança...')
  93.                       ->setValue(0)
  94.                       ->setAttribute('onChange', 'cursoGratuito(this.value)')
  95.                       ->setValueOptions(array(0 => 'Pago', 1 => 'Gratuito'));
  96.  
  97.         $ccmo_tag = new Element\Textarea('ccmo_tag');
  98.         $ccmo_tag->setLabel('Palavras-chave')
  99.                  ->setAttribute('title','Palavras-chave')
  100.                  ->setAttribute('style','margin-top:10px;')
  101.                  ->setAttribute('class','span17')
  102.                  ->setAttribute('id','ccmo_tag')
  103.                  ->setAttribute('placeholder','Ex: curso de excel, preparatório enem, matemática');
  104.  
  105.         $ccmo_tempo = new Element\Text('ccmo_tempo');
  106.         $ccmo_tempo->setLabel('Tempo')
  107.                  ->setAttribute('title','Tempo')
  108.                  ->setAttribute('size',100)
  109.                  ->setAttribute('maxlength',100)
  110.                  ->setAttribute('class','span2')
  111.                  ->setAttribute('id','ccmo_tempo')
  112.                  ->setAttribute('placeholder','10h');
  113.  
  114.         $data = array('' => 'Escolha o nível',
  115.                       1 => 'Básico',
  116.                       2 => 'Intermediário',
  117.                       3 => 'Avançado',
  118.                       4 => 'Todos os níveis',);
  119.  
  120.         $ccmo_nivel = new Element\Select('ccmo_nivel');
  121.         $ccmo_nivel->setLabel('Nivel')
  122.                  ->setAttribute('title','Nivel')
  123.                  ->setAttribute('class','span4')
  124.                  ->setAttribute('id','ccmo_nivel')
  125.                  ->setAttribute('placeholder','Básico')
  126.                  ->setAttribute('onchange','continuarPreenchimento()')
  127.                  ->setValueOptions($data);
  128.  
  129.         $ccmo_descricao = new Element\Textarea('ccmo_descricao');
  130.         $ccmo_descricao->setLabel('Descrição')
  131.                        ->setAttribute('title','Descrição do curso')
  132.                        ->setAttribute('id','ccmo_descricao')
  133.                        ->setAttribute('rows', 4)
  134.                        ->setAttribute('style', 'height:110px')
  135.                        ->setAttribute('class','span12')
  136.                        ->setAttribute('placeholder','Descrição do curso');
  137.  
  138.         $file = new Element\File('image-file');
  139.         $file->setLabel('Imagem do curso')
  140.              ->setAttribute('id', 'image-file');
  141.  
  142.         $submit = new Element\Submit('submit');
  143.         $submit->setAttribute('value','Salvar')
  144.                ->setAttribute('class','btn span3')
  145.                ->setAttribute('id','salvarcurso')
  146.                ->setAttribute('data-loading-text','Salvando...')
  147.                ->setAttribute('style','height:35px');
  148.        
  149.         $this->add($ccmo_nome)
  150.              ->add($estrutura)
  151.              ->add($categoria)
  152.              ->add($ccmo_gratuito)
  153.              ->add($ccmo_preco)
  154.              ->add($ccmo_tag)
  155.              ->add($ccmo_nivel)
  156.              ->add($ccmo_descricao)
  157.              ->add($file)
  158.              ->add($submit);
  159.     }
  160.    
  161.     public function getInputFilterForm($id)
  162.     {
  163.         $filter  = new InputFilter();
  164.         $factory = new InputFactory();
  165.         $filter->add($factory->createInput(array(
  166.                 'name'   => 'ccmo_nome',
  167.                 'filters'  => array(
  168.                         array('name' => 'StripTags'),
  169.                         array('name' => 'StringTrim'),
  170.                         array('name' => 'StripNewLines'),
  171.                 ),
  172.                 'validators' => array(
  173.                         array(
  174.                                 'name' => 'NotEmpty',
  175.                                 'options' => array(
  176.                                         'messages' => array(
  177.                                                 NotEmpty::IS_EMPTY => 'Digite o nome do curso',
  178.                                         ),
  179.                                 ),
  180.                         ),
  181.                 ),
  182.         )));
  183.         $filter->add($factory->createInput(array(
  184.                 'name'   => 'ccmo_descricao',
  185.                 'allow_empty' => true,
  186.                 'filters'  => array(
  187.                         array('name' => 'StripTags'),
  188.                         array('name' => 'StringTrim'),
  189.                         array('name' => 'StripNewLines'),
  190.                 ),
  191.                 'validators' => array(
  192.                         array(
  193.                                 'name' => 'NotEmpty',
  194.                                 'options' => array(
  195.                                         'messages' => array(
  196.                                                 NotEmpty::IS_EMPTY => 'Digite a descrição do seu curso',
  197.                                         ),
  198.                                 ),
  199.                         ),
  200.                         array(
  201.                                 'name'  => 'StringLength',
  202.                                 'options' => array(
  203.                                         'encoding' => 'UTF-8',
  204.                                         'min'     => 200,
  205.                                         'max'     => 999999,
  206.                                         'messages' => array(
  207.                                                 StringLength::TOO_LONG => 'Descrição com muitos caracteres',
  208.                                                 StringLength::TOO_SHORT => 'Descrição com poucos caracteres, a descrição deve ter no mínimo 200 caracteres',
  209.                                         )
  210.                                 ),
  211.                         ),
  212.                 ),
  213.         )));
  214.  
  215.         $filter->add($factory->createInput(array(
  216.                 'name'   => 'ccmo_preco',
  217.                 'allow_empty' => true,
  218.                 'filters' => array(
  219.                         array('name' => 'Zend\Filter\StringTrim')
  220.                 ),
  221.                 'validators' => array(
  222.                     array(
  223.                         'name'  => 'Between',
  224.                             'options' => array(
  225.                                 'encoding' => 'UTF-8',
  226.                                 'min'     => 14.90,
  227.                                 'max'     => 999999,
  228.                                 'message' => 'Valor mínimo do curso é de R$ 14,90'
  229.                             ),
  230.                         ),
  231.                     ),
  232.         )));
  233.  
  234.         $filter->add($factory->createInput(array(
  235.                 'name'   => 'ccat_id',
  236.                 'allow_empty' => true,
  237.                 'filters'  => array(
  238.                         array('name' => 'StripTags'),
  239.                         array('name' => 'StringTrim'),
  240.                         array('name' => 'StripNewLines'),
  241.                 ),
  242.                 'validators' => array(
  243.                         array(
  244.                                 'name' => 'NotEmpty',
  245.                                 'options' => array(
  246.                                         'messages' => array(
  247.                                                 NotEmpty::IS_EMPTY => 'Selecione a categoria do seu curso',
  248.                                         ),
  249.                                 ),
  250.                         ),
  251.                
  252.  
  253.                 ),
  254.         )));
  255.  
  256.         $filter->add($factory->createInput(array(
  257.                 'name'   => 'cest_id',
  258.                 'allow_empty' => true,
  259.                 'filters'  => array(
  260.                         array('name' => 'StripTags'),
  261.                         array('name' => 'StringTrim'),
  262.                         array('name' => 'StripNewLines'),
  263.                 ),
  264.                 'validators' => array(
  265.                         array(
  266.                                 'name' => 'NotEmpty',
  267.                                 'options' => array(
  268.                                         'messages' => array(
  269.                                                 NotEmpty::IS_EMPTY => 'Selecione a sub-categoria do seu curso',
  270.                                         ),
  271.                                 ),
  272.                         ),
  273.                
  274.  
  275.                 ),
  276.         )));
  277.  
  278.         $filter->add($factory->createInput(array(
  279.                 'name'   => 'ccmo_gratuito',
  280.                 'allow_empty' => true,
  281.                 'filters'  => array(
  282.                         array('name' => 'StripTags'),
  283.                         array('name' => 'StringTrim'),
  284.                         array('name' => 'StripNewLines'),
  285.                 ),
  286.                 'validators' => array(
  287.                         array(
  288.                                 'name' => 'NotEmpty',
  289.                                 'options' => array(
  290.                                         'messages' => array(
  291.                                                 NotEmpty::IS_EMPTY => 'Selecione o tipo de cobrança do seu curso',
  292.                                         ),
  293.                                 ),
  294.                         ),
  295.                         array(
  296.                                 'name'  => 'InArray',
  297.                                 'options' => array(
  298.                                         'haystack' => array(0,1),
  299.                                         'messages' => array(
  300.                                                 'notInArray' => '*'
  301.                                         ),
  302.                                 ),
  303.                         ),
  304.                 ),
  305.         )));
  306.  
  307.  
  308.         $filter->add($factory->createInput(array(
  309.                 'name'   => 'ccmo_nivel',
  310.                 'allow_empty' => true,
  311.                 'filters'  => array(
  312.                         array('name' => 'StripTags'),
  313.                         array('name' => 'StringTrim'),
  314.                         array('name' => 'StripNewLines'),
  315.                 ),
  316.                 'validators' => array(
  317.                         array(
  318.                                 'name' => 'NotEmpty',
  319.                                 'options' => array(
  320.                                         'messages' => array(
  321.                                                 NotEmpty::IS_EMPTY => 'Escolha o nível do seu curso',
  322.                                         ),
  323.                                 ),
  324.                         ),
  325.                
  326.  
  327.                 ),
  328.         )));
  329.  
  330.         $filter->add(array('name' => 'image-file',
  331.                            'allow_empty' => true,
  332.                            'validators' => array(
  333.                                 new \Zend\Validator\File\UploadFile(),
  334.                            ),
  335.         ));
  336.  
  337.         return $filter;
  338.     }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment