Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Saudações!
Estou montando uma classe que vai me ajudar a criar elementos HTML (os famosos HELPERS)
Um exemplo dela é: Seu eu chamar
$helper->makeAnchor('download.php','Clique aqui',array('class'=>'foo bar'));O helper me retorna
<a href="http://meusite.com.br/download.php" class="foo bar">Clique aqui</a>
Até aqui OK.
Eu estou tentando montar um makeForm e um makeList mas não estou conseguindo pensar num modo de passa todos os itens da lista/form, em uma função só.
Pensei em arrays, como:
$helper->makeForm(
'envia.php', 'post',
array(
'nome'=>array(
'type'=>'text',
'label'=>'Nome'),
'senha'=>array(
'type'=>'password',
'label'=>'Senha'),
'id'=>array(
'type'=>'hidden',
'value'=>1),
'submit'=>array(
'type'=>'submit',
'label'=>'Enviar')
),array(
'class'=>'form left',
'id'=>'contato'
)
)<form action="envia.php" method="post">
<label>Nome:<input name="nome" type="text" /></label>
<label>Senha:<input name="senha" type="password" /></label>
<input name="id" type="hidden" value="1" />
<button type="submit" name="submit">Enviar</button>
</form>
Agluma sugestão?Nesse caso, teria que ser chamada uma função por elemento e duas p/ o form certo?
Eu fui por esse caminho a princípio mas é praticamente é o mesmo que digitar os elementos em HTML, um a um.
Eu achei que a proposta que eu postei ficou meio complexa, e difícil de ler (eu tive que revisar umas 4x pra postar o tópico), por isso queria algo mais simples, porém direto.
Implementei o método como tinha pensado:
class helper{
public function makeForm($action='#', $method='', $attr=array(), $inputs=array()){
$form = '<form ';
//atributos formulario
$form .= 'action="'.$action.'" ';
$form .= 'method="'.$method.'" ';
foreach($attr as $atributo=>$valor){
$form .= $atributo.'="'.$valor.'" ';
}
$form .= '>'."\n";
//inputs
foreach($inputs as $nome=>$atributos){
$form .= $this->makeInput($nome, $atributos);
}
$form .= '</form>'."\n";
return $form;
}
public function makeInput($nome, $atributos){
switch($atributos['type']){
case 'password':
$element = $this->makePasswordInput($nome, $atributos);
break;
case 'textarea':
$element = $this->makeTextareaInput($nome, $atributos);
break;
case 'submit':
$element = $this->makeSubmitButton($nome, $atributos);
break;
case 'reset':
$element = $this->makeResetButton($nome, $atributos);
break;
default:
$element = $this->makeTextInput($nome, $atributos);
break;
}
return $element;
}
public function makeTextInput($nome, $atributos){
$input = '<label for="'.$nome.'">';
$input .= $atributos['label'];
$input .= '<input ';
$input .= 'name="'.$nome.'" ';
$input .= 'type="text" ';
$input .= '/>';
$input .= '</label>';
return $input;
}
public function makePasswordInput($nome, $atributos){
$input = '<label for="'.$nome.'">';
$input .= $atributos['label'];
$input .= '<input ';
$input .= 'name="'.$nome.'" ';
$input .= 'type="password" ';
$input .= '/>';
$input .= '</label>';
return $input;
}
public function makeTextareaInput($nome, $atributos){
$input = '<label for="'.$nome.'">';
$input .= $atributos['label'];
$input .= '<textarea ';
$input .= 'name="'.$nome.'" ';
$input .= '></textarea>';
$input .= '</label>';
return $input;
}
public function makeSubmitButton($nome, $atributos){
$input .= '<button ';
$input .= 'name="'.$nome.'" ';
$input .= 'type="submit" ';
$input .= '>'.$atributos['label'].'</button>';
return $input;
}
public function makeResetButton($nome, $atributos){
$input .= '<button ';
$input .= 'name="'.$nome.'" ';
$input .= 'type="reset" ';
$input .= '>'.$atributos['label'].'</button>';
return $input;
}
}
E pra chamá-lo, faria assim:
$helper = new helper();
echo $helper->makeForm(
'script.php',
'post',
array('class'=>'formulario'),
array(
'nome'=>array(
'type'=>'text',
'label'=>'Nome:',
),
'senha'=>array(
'type'=>'password',
'label'=>'Senha:',
),
'area'=>array(
'type'=>'textarea',
'label'=>'Texto:'
),
'submit'=>array(
'type'=>'submit',
'label'=>'Enviar'
),
'reset'=>array(
'type'=>'reset',
'label'=>'Limpar'
)
)
);
Deixo aqui minha contribuição =]
(desculpem, não consegui editar o post anterior)
Certo... 2 pequenas sugestões:
- Eu mudaria a ordem dos parâmetros do método makeForm e definiria valores padrão... Aí, caso seja post mesmo, não precisa colocar nada e caso não seja passado o action, seria colocado $_SERVER['PHP_SELF']. Olha só:
<?php
class helper {
public function makeForm($attr = array(), $inputs = array(), $action = null, $method = 'post') {
// aqui definimos. Caso não seja passado $action, ele pega como padrão a página atual
if ($action === null) {
$action = $_SERVER ['PHP_SELF'];
}
$form = '<form ';
//atributos formulario
$form .= 'action="' . $action . '" ';
$form .= 'method="' . $method . '" ';
foreach ( $attr as $atributo => $valor ) {
$form .= $atributo . '="' . $valor . '" ';
}
$form .= '>' . PHP_EOL;
//inputs
foreach ( $inputs as $nome => $atributos ) {
$form .= $this->makeInput ( $nome, $atributos ). PHP_EOL;
}
$form .= '</form>' . PHP_EOL;
return $form;
}
public function makeInput($nome, $atributos) {
switch ($atributos ['type']) {
case 'password' :
$element = $this->makePasswordInput ( $nome, $atributos );
break;
case 'textarea' :
$element = $this->makeTextareaInput ( $nome, $atributos );
break;
case 'submit' :
$element = $this->makeSubmitButton ( $nome, $atributos );
break;
case 'reset' :
$element = $this->makeResetButton ( $nome, $atributos );
break;
default :
$element = $this->makeTextInput ( $nome, $atributos );
break;
}
return $element;
}
public function makeTextInput($nome, $atributos) {
$input = '<label for="' . $nome . '">';
$input .= $atributos ['label'];
$input .= '<input ';
$input .= 'name="' . $nome . '" ';
$input .= 'type="text" ';
$input .= '/>';
$input .= '</label>';
return $input;
}
public function makePasswordInput($nome, $atributos) {
$input = '<label for="' . $nome . '">';
$input .= $atributos ['label'];
$input .= '<input ';
$input .= 'name="' . $nome . '" ';
$input .= 'type="password" ';
$input .= '/>';
$input .= '</label>';
return $input;
}
public function makeTextareaInput($nome, $atributos) {
$input = '<label for="' . $nome . '">';
$input .= $atributos ['label'];
$input .= '<textarea ';
$input .= 'name="' . $nome . '" ';
$input .= '></textarea>';
$input .= '</label>';
return $input;
}
// aqui não seria <input> ao invés de <button>?
public function makeSubmitButton($nome, $atributos) {
$input = '<button ';
$input .= 'name="' . $nome . '" ';
$input .= 'type="submit" ';
$input .= '>' . $atributos ['label'] . '</button>';
return $input;
}
// aqui não seria <input ao invés de button?
public function makeResetButton($nome, $atributos) {
$input = '<button ';
$input .= 'name="' . $nome . '" ';
$input .= 'type="reset" ';
$input .= '>' . $atributos ['label'] . '</button>';
return $input;
}
}
// usando...
$helper = new helper ();
// só estou separando para ficar mais fácil de visualizar
$atributos = array (
'class' => 'formulario' );
// inputs
$inputs = array (
'nome' => array (
'type' => 'text',
'label' => 'Nome:' ),
'senha' => array (
'type' => 'password',
'label' => 'Senha:' ),
'area' => array (
'type' => 'textarea',
'label' => 'Texto:' ),
'submit' => array (
'type' => 'submit',
'label' => 'Enviar' ),
'reset' => array (
'type' => 'reset',
'label' => 'Limpar' ) );
// enviando para a mesma página
echo $helper->makeForm ( $atributos, $inputs );
// mudando a página
echo $helper->makeForm ( $atributos, $inputs , 'script.php' );
// mudando a página e o método de envio
echo $helper->makeForm ( $atributos, $inputs , 'script.php', 'get' );
// mudando só o método de envio
echo $helper->makeForm ( $atributos, $inputs , null, 'get' );
Carlos Eduardo
Nice!
Sugestão aceita e anotada
Sobre o button/input, eu prefiro assim na hora de mexer c/ CSS
Obrigado.
você poderia usar a mesma ideia do helper do codeigniter ele separou as partes do form em varias funcoes