I would like to customize input name to show in my view when happen an error in validation of the form
$this->add(array(
'name' => 'generica_descricao', // I WOULD LIKE TO CALL HIM DESCRIÇÃO
//'custom_name' => 'Descrição',
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
'isEmpty' => 'O campo não pode ser vazio'
),
),
),
));
and when i call getMessage() as the code above
if (!$form->isValid()) {
$resultado = new Resultado(Resultado::FLAG_WARNING, $form->getMessages());
$resultado->setaRetornoLayoutErro($this->getServiceLocator());
return $resultado->getJson();
}
they will return
array('Descrição' => 'O campo não pode ser vazio');
then i will can give this array to my view and show dialog with the correctly messages, can anybody help how do that in zend?
I found a best way, create a new method that extends the default form getting the name from the label in the form.
abstract class GenericForm extends Form {
public function getMessagesTranslated($elementName = null) {
$mensagensOriginais = $this->getMessages($elementName);
foreach ($mensagensOriginais as $chave => $mensagens) {
$label = TranslateUtil::translate($this->get($chave)->getLabel());
$mensagensOriginais[$label] = $mensagensOriginais[$chave];
unset($mensagensOriginais[$chave]);
}
return mensagensOriginais;
}
I customize my filter with a method that translates the field name, is not what i want but it works for now:
generic filter
abstract function convertErrorsArrayKeyToFriendlyNames($erros);
specific filter:
public function convertErrorsArrayKeyToFriendlyNames($erros) {
foreach ($erros as $chave => $valor) {
if ($chave == 'generica_descricao') {
$erros['Descrição'] = $erros[$chave];
unset($erros[$chave]);
} else if ($chave == 'generica_ordem') {
$erros['Ordem'] = $erros[$chave];
unset($erros[$chave]);
} else if ($chave == 'generica_ativo') {
$erros['Ativo'] = $erros[$chave];
unset($erros[$chave]);
}
}
return $erros;
}
and in the controller
if (!$form->isValid()) {
$erros = $filtro->convertErrorsArrayKeyToFriendlyNames($form->getMessages());
$resultado = new Resultado(Resultado::FLAG_WARNING, $erros);
$resultado->setaRetornoLayoutErro($this->getServiceLocator());
return $resultado->getJson();
}