Last active
December 21, 2015 04:29
-
-
Save gpfiel/6249976 to your computer and use it in GitHub Desktop.
Im having problems in the validation, there is an error but it dont show in the screen
Controller, Form, View, Fieldset, Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Form; | |
use Zend\Form\Form; | |
use Zend\InputFilter\InputFilter; | |
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator; | |
class CriarHabilidade extends Form | |
{ | |
public function __construct() | |
{ | |
parent::__construct('habilidade-form'); | |
$this->setAttribute('method', 'post') | |
->setAttribute('enctype','multipart/form-data') | |
->setHydrator(new ClassMethodsHydrator(false)) | |
->setInputFilter(new InputFilter()); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Collection', | |
'name' => 'habilidades', | |
'options' => array( | |
'label' => 'Please choose habilidades for this product', | |
'count' => 1, | |
'should_create_template' => true, | |
'allow_add' => true, | |
'target_element' => array( | |
'type' => 'Admin\Form\HabilidadeFieldset' | |
) | |
) | |
)); | |
$this->add(array( | |
'name' => 'submit', | |
'attributes' => array( | |
'type' => 'submit', | |
'value' => 'Adicionar', | |
'id' => 'submitbutton', | |
'class' => 'btn btn-large btn-wtg pull-left' | |
), | |
)); | |
} | |
public function getInputFilterSpecification() | |
{ | |
return array( | |
); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Gabriel P. Fiel <[email protected]> | |
* @package Habilidade | |
*/ | |
namespace Admin\Model; | |
use Zend\InputFilter\Factory as InputFactory; | |
use Zend\InputFilter\InputFilter; | |
use Zend\InputFilter\InputFilterAwareInterface; | |
use Zend\InputFilter\InputFilterInterface; | |
class Habilidade implements InputFilterAwareInterface | |
{ | |
public $cod_habilidade; | |
public $cod_habilidade_pai; | |
public $cod_idioma; | |
public $nom_habilidade; | |
public $ind_status; | |
public $inputFilter; | |
public function exchangeArray($data) | |
{ | |
$this->cod_habilidade = (isset($data['cod_habilidade'])) ? $data['cod_habilidade'] : null; | |
$this->cod_habilidade_pai = (isset($data['cod_habilidade_pai'])) ? $data['cod_habilidade_pai'] : null; | |
$this->cod_idioma = (isset($data['cod_idioma'])) ? $data['cod_idioma'] : null; | |
$this->nom_habilidade = (isset($data['nom_habilidade'])) ? $data['nom_habilidade'] : null; | |
$this->ind_status = (isset($data['ind_status'])) ? $data['ind_status'] : null; | |
} | |
public function setInputFilter(InputFilterInterface $inputFilter) | |
{ | |
throw new \Exception("Não é usado."); | |
} | |
public function getInputFilter() | |
{ | |
if (! $this->inputFilter) { | |
$inputFilter = new InputFilter(); | |
$factory = new InputFactory(); | |
// $inputFilter->add($factory->createInput(array( | |
// 'name' => 'cod_habilidade', | |
// 'required' => false, | |
// 'filters' => array( | |
// array( | |
// 'name' => 'Int' | |
// ) | |
// ) | |
// ))); | |
// $inputFilter->add($factory->createInput(array( | |
// 'name' => 'cod_habilidade_pai', | |
// 'required' => false, | |
// 'filters' => array( | |
// array( | |
// 'name' => 'Int' | |
// ) | |
// ) | |
// ))); | |
// $inputFilter->add($factory->createInput(array( | |
// 'name' => 'cod_idioma', | |
// 'required' => false, | |
// 'filters' => array( | |
// array( | |
// 'name' => 'Int' | |
// ) | |
// ) | |
// ))); | |
// $inputFilter->add($factory->createInput(array( | |
// 'name' => 'nom_habilidade', | |
// 'required' => false, | |
// 'filters' => array( | |
// array( | |
// 'name' => 'StripTags' | |
// ), | |
// array( | |
// 'name' => 'StringTrim' | |
// ) | |
// ), | |
// 'validators' => array( | |
// array( | |
// 'name' => 'StringLength', | |
// 'options' => array( | |
// 'encoding' => 'UTF-8', | |
// 'min' => 1, | |
// 'max' => 250 | |
// ) | |
// ) | |
// ) | |
// ))); | |
// $inputFilter->add($factory->createInput(array( | |
// 'name' => 'ind_status', | |
// 'required' => false, | |
// 'filters' => array( | |
// array( | |
// 'name' => 'Int' | |
// ) | |
// ) | |
// ))); | |
$this->inputFilter = $inputFilter; | |
} | |
return $this->inputFilter; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Gabriel P. Fiel <[email protected]> | |
* @package Habilidade | |
*/ | |
namespace Admin\Controller; | |
use Zend\Mvc\Controller\AbstractActionController; | |
use Zend\View\Model\ViewModel; | |
use Admin\Form\CriarHabilidade; | |
use Admin\Model\Habilidade; | |
use Admin\Form\HabilidadeForm; | |
use Zend\Paginator\Paginator; | |
use Zend\Paginator\Adapter\ArrayAdapter as paginatorIterator; | |
class HabilidadeController extends AbstractActionController | |
{ | |
protected $habilidadeTable; | |
public function indexAction() | |
{ | |
$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1; | |
$itemsPerPage = 10; | |
$idiomas_tmp = $this -> pluginFuncoes() -> getIdiomas(); | |
foreach ($idiomas_tmp as $idioma) { | |
$idiomas[] = $idioma; | |
} | |
$idiomas_lista = array(); | |
foreach ($idiomas as $idioma) { | |
$habilidades = $this->getHabilidadeTable()->fetchAll(null, $idioma -> cod_idioma); | |
foreach ($habilidades as $habilidade) { | |
$habilidades_tmp[] = $habilidade; | |
} | |
if(isset($habilidades_tmp)) | |
{ | |
$paginator = new Paginator(new paginatorIterator($habilidades_tmp)); | |
$paginator -> setCurrentPageNumber($page) | |
-> setItemCountPerPage($itemsPerPage) | |
-> setPageRange(10); | |
$idioma -> paginator = $paginator; | |
$idiomas_lista[] = $idioma; | |
} | |
unset($habilidades_tmp); | |
} | |
return new ViewModel(array( | |
'flashMessages' => $this->flashMessenger()->getMessages(), | |
'page' => $page, | |
'idiomas' => $idiomas_lista | |
)); | |
} | |
public function adicionarAction() | |
{ | |
$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1; | |
$form = new CriarHabilidade(); | |
$idiomas = $this -> pluginFuncoes() -> getIdiomas(); | |
$form->get('habilidades')->setCount($idiomas->count())->prepareFieldset(); | |
$form->get('submit')->setValue('Adicionar'); | |
$request = $this->getRequest(); | |
if ($request->isPost()) { | |
$habilidade = new Habilidade(); | |
$form->setInputFilter($habilidade->getInputFilter()); | |
$data = $request->getPost()->toArray(); | |
$form->setData($data); | |
//Verifica se existe ao menos um campo escrito | |
$checa_permissao = false; | |
foreach ($data['habilidades'] as $chave => $h) | |
{ | |
if(!empty($h['nom_habilidade'])) | |
{ | |
$checa_permissao = true; | |
} | |
if($h['ind_status'] == 2 and $checa_permissao) | |
{ | |
$form->getInputFilter()->get('habilidades')->get($chave)->get('nom_habilidade')->setRequired(false); | |
} | |
} | |
if ($form->isValid() and $checa_permissao) | |
{ | |
$codigos_dos_idiomas = array(); | |
foreach ($idiomas as $idioma) | |
{ | |
$codigos_dos_idiomas[] = $idioma->cod_idioma; | |
} | |
foreach ($data['habilidades'] as $chave => $h) | |
{ | |
if($chave != 0) | |
{ | |
$h['cod_habilidade_pai'] = $cod_habilidade_pai; | |
} | |
$h['cod_idioma'] = $codigos_dos_idiomas[$chave]; | |
$habilidade->exchangeArray($h); | |
if($chave == 0) | |
{ | |
$cod_habilidade_pai = $this->getHabilidadeTable()->cadastrarHabilidade($habilidade); | |
} | |
else | |
{ | |
$this->getHabilidadeTable()->cadastrarHabilidade($habilidade); | |
} | |
} | |
$this -> pluginMensagem() -> adicionarMensagem($this->params('action'),'ok'); | |
return $this->redirect()->toRoute('habilidade', array('page' => $page)); | |
} | |
else | |
{ | |
$this -> pluginMensagem() -> adicionarMensagem($this->params('action'),'erro',"Necessário o preenchimento de uma habilidade."); | |
} | |
} | |
$viewmodel = new ViewModel(array( | |
'form' => $form, | |
'page' => $page, | |
'flashMessages' => $this->flashMessenger()->getCurrentMessages(), | |
'idiomas' => $idiomas | |
)); | |
return $viewmodel; | |
} | |
public function deletarAction() | |
{ | |
$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1; | |
$cod_habilidade = $this->getEvent() | |
->getRouteMatch() | |
->getParam('cod_habilidade'); | |
$habilidade = $this->getHabilidadeTable()->getHabilidade($cod_habilidade); | |
$retorno = $this->getHabilidadeTable()->deletarHabilidade($cod_habilidade, $this -> pluginFuncoes()); | |
if(!$retorno) | |
{ | |
$this -> pluginMensagem() -> adicionarMensagem($this->params('action'),'ok'); | |
} | |
else | |
{ | |
$this -> pluginMensagem() -> adicionarMensagem($this->params('action'),'erro', $retorno); | |
} | |
return $this->redirect()->toRoute('habilidade', array('page' => $page)); | |
} | |
public function editarAction() | |
{ | |
$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1; | |
$cod_habilidade = ((int) $this->params('cod_habilidade') ? (int) $this->params('cod_habilidade') : (int) $this->params('cod_habilidade')); | |
$habilidade = $this->getHabilidadeTable()->getHabilidade($cod_habilidade); | |
$form = new CriarHabilidade(); | |
$idiomas = $this -> pluginFuncoes() -> getIdiomas(); | |
$form->get('habilidades')->setCount($idiomas->count())->prepareFieldset(); | |
foreach ($habilidade as $ha) { | |
$habilidades_lista[] = (array) $ha; | |
} | |
$habilidades_lista[] = array(); | |
$form->setData(array("habilidades" => $habilidades_lista)); | |
$form->get('submit')->setValue('Editar'); | |
$request = $this->getRequest(); | |
if ($request->isPost()) { | |
$habilidade = new Habilidade(); | |
$form->setInputFilter($habilidade->getInputFilter()); | |
$data = $request->getPost()->toArray(); | |
$form->setData($data); | |
// echo '<pre>'; | |
// print_r($data['habilidades']); | |
// exit; | |
//Verifica se existe ao menos um campo escrito | |
$checa_permissao = false; | |
foreach ($data['habilidades'] as $chave => $h) | |
{ | |
if(!empty($h['nom_habilidade'])) | |
{ | |
$checa_permissao = true; | |
} | |
if($h['ind_status'] == 2 and $checa_permissao) | |
{ | |
$form->getInputFilter()->get('habilidades')->get($chave)->get('nom_habilidade')->setRequired(false); | |
} | |
} | |
if ($form->isValid() and $checa_permissao) | |
{ | |
foreach ($data['habilidades'] as $h) | |
{ | |
$habilidade->exchangeArray($h); | |
$this->getHabilidadeTable()->editarHabilidade($habilidade); | |
} | |
$this -> pluginMensagem() -> adicionarMensagem($this->params('action'),'ok'); | |
return $this->redirect()->toRoute('habilidade', array('page' => $page)); | |
} | |
else | |
{ | |
$this -> pluginMensagem() -> adicionarMensagem($this->params('action'),'erro',"Necessário o preenchimento de uma habilidade."); | |
} | |
} | |
$viewmodel = new ViewModel(array( | |
'form' => $form, | |
'page' => $page, | |
'flashMessages' => $this->flashMessenger()->getCurrentMessages(), | |
'idiomas' => $idiomas | |
)); | |
return $viewmodel; | |
} | |
public function getHabilidadeTable() | |
{ | |
if (! $this->habilidadeTable) { | |
$sm = $this->getServiceLocator(); | |
$this->habilidadeTable = $sm->get('Admin\Model\HabilidadeTable'); | |
} | |
return $this->habilidadeTable; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Form; | |
use Admin\Model\Habilidade; | |
use Zend\Form\Fieldset; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator; | |
use Zend\Validator\NotEmpty; | |
use Zend\Validator\StringLength; | |
class HabilidadeFieldset extends Fieldset implements InputFilterProviderInterface | |
{ | |
public function __construct() | |
{ | |
parent::__construct('habilidade'); | |
$this->setHydrator(new ClassMethodsHydrator(false)) | |
->setObject(new Habilidade()); | |
$this->setLabel('Habilidade'); | |
$this->add(array( | |
'name' => 'cod_habilidade', | |
'attributes' => array( | |
'type' => 'hidden', | |
), | |
)); | |
$this->add(array( | |
'name' => 'cod_habilidade_pai', | |
'attributes' => array( | |
'type' => 'hidden', | |
), | |
)); | |
$this->add(array( | |
'name' => 'cod_idioma', | |
'attributes' => array( | |
'type' => 'hidden', | |
), | |
)); | |
$this->add(array( | |
'name' => 'nom_habilidade', | |
'attributes' => array( | |
'type' => 'text', | |
), | |
'options' => array( | |
'label' => 'Nome', | |
), | |
)); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Select', | |
'name' => 'ind_status', | |
'options' => array( | |
'label' => 'Situação', | |
'value_options' => array( | |
'2' => 'Inativo', | |
'1' => 'Ativo' | |
), | |
), | |
'attributes' => array( | |
'value' => '2' | |
) | |
)); | |
} | |
public function getInputFilterSpecification() | |
{ | |
return array( | |
'cod_habilidade' => array( | |
'required' => false, | |
'filters' => array( | |
array( | |
'name' => 'Int' | |
) | |
) | |
), | |
'cod_habilidade_pai' => array( | |
'required' => false, | |
'filters' => array( | |
array( | |
'name' => 'Int' | |
) | |
) | |
), | |
'cod_idioma' => array( | |
'required' => false, | |
'filters' => array( | |
array( | |
'name' => 'Int' | |
) | |
) | |
), | |
'nom_habilidade' => array( | |
'required' => true, | |
'validators' => array( | |
array( | |
'name' => 'StringLength', | |
'options' => array( | |
'encoding' => 'UTF-8', | |
'min' => 1, | |
'max' => 250, | |
'messages' => array( | |
StringLength::TOO_SHORT => 'Deve conter no mínimo um caracter.', | |
StringLength::TOO_LONG => 'Não pode exceder 250 caracteres.', | |
), | |
) | |
), | |
array( | |
'name' => 'NotEmpty', | |
'options' => array( | |
'messages' => array( | |
NotEmpty::IS_EMPTY => 'Preenchimento obrigatório.', | |
), | |
), | |
), | |
), | |
'filters' => array( | |
array( | |
'name' => 'StripTags' | |
), | |
array( | |
'name' => 'StringTrim' | |
) | |
), | |
), | |
'ind_status' => array( | |
'required' => false, | |
'filters' => array( | |
array( | |
'name' => 'Int' | |
) | |
) | |
) | |
); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="span12"> | |
<?php | |
$title = 'Editar habilidade'; | |
$this->ztbForm($this->form); | |
$form = $this->form; | |
$form->setAttribute('action', $this->url('habilidade', $this -> parametros() -> getTodosParametros())); | |
$form->setAttribute('class', 'form-horizontal'); | |
$form->setAttribute('class', 'pull-left'); | |
$form->setAttribute('class', 'span12'); | |
$form->prepare(); | |
echo $this->form()->openTag($form); | |
// echo $this->formCollection($form->get('habilidades')); | |
?> | |
<div class="page-header"> | |
<h1><?php echo $this->escapeHtml($title); ?></h1> | |
</div> | |
<?php | |
$idiomas_temp = array(); | |
if(count($idiomas)): | |
echo '<ul class="nav nav-tabs">'; | |
foreach ($idiomas as $idioma) : | |
?> | |
<li <?php echo (count($idiomas_temp) == 0) ? "class='active'" : "" ?>> | |
<a href="#idioma<?php echo $idioma->cod_idioma ?>" data-toggle="tab"> | |
<img src="<?php echo $this->basePath().'/assets/idioma/original/'.$this->escapeHtml($idioma->url_imagem) ?>" > | |
<?php echo $idioma->nom_idioma ?> | |
</a> | |
</li> | |
<?php | |
$idiomas_temp[] = $idioma; | |
endforeach; | |
echo '</ul>'; | |
endif; | |
?> | |
<?php | |
if(count($idiomas_temp)) : | |
?> | |
<div class="tab-content"> | |
<?php | |
$cont = 0; | |
foreach ($idiomas_temp as $idioma): | |
?> | |
<div class="tab-pane <?php echo ($cont == 0) ? "active" : "" ?>" id="idioma<?php echo $idioma->cod_idioma ?>"> | |
<?php | |
echo $this->ztbFormElement($form->get('habilidades')->get($cont)->get('cod_habilidade')); | |
echo $this->ztbFormElement($form->get('habilidades')->get($cont)->get('cod_habilidade_pai')); | |
echo $this->ztbFormElement($form->get('habilidades')->get($cont)->get('cod_idioma')); | |
echo $this->ztbFormElement($form->get('habilidades')->get($cont)->get('nom_habilidade')); | |
echo $this->ztbFormElement($form->get('habilidades')->get($cont)->get('ind_status')); | |
?> | |
</div> | |
<?php | |
$cont++; | |
endforeach; | |
?> | |
</div> | |
<hr/> | |
<?php | |
echo $this->formElement($form->get('submit')); | |
echo $this->form()->closeTag(); | |
?> | |
<?php | |
$controller = ucfirst($this -> parametros() -> getController(true)); | |
echo $this->headScript()->appendFile($this->basePath() . "/public_admin/js/validation/Admin/{$controller}Form.js"); | |
endif; | |
?> | |
</div> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment