ccc

Zend Framework: Formularios

FormularioController.php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\Formularios;
use Application\Model\Entity\Procesa;

class FormularioController extends AbstractActionController {
    public function indexAction()
    {
        return new ViewModel();
    }
    public function formularioAction()   {
        $form = new Formularios("form");
        return new ViewModel(
array(
"titulo"=>"Formularios en ZF2",
"form"=>$form,
"url"=>$this->getRequest()->getBaseUrl()
)
);
    }

    public function recibeAction() {
        $data = $this->request->getPost();
        $procesa = new Procesa($data);
        $datos = $procesa->getData();
        return new ViewModel(
array(
"datos"=>$datos
)
);
    }

}


module\Application\src\Application\Form\Formularios.php:
namespace Application\Form;
use Zend\Captcha\AdapterInterface as CaptchaAdapter;
use Zend\Form\Element;
use Zend\Form\Form;
use Zend\Captcha;
use Zend\Form\Factory;

class Formularios extends Form {
    public function __construct($name = null)  {
        parent::__construct($name);
     
         $factory = new Factory();
$nombre = $factory->createElement(array(
            'type' => 'Zend\Form\Element\Text',
            'name' => 'nombre',
            'options' => array(
                'label' => 'Escribe tu nombre completo:',
            ),
            'attributes' => array(
                'class' => 'classLSG'
            ),
         ));
        $this->add($nombre);


        $email = $factory->createElement(array(
            'type' => 'Zend\Form\Element\Email',
            'name' => 'email',
            'options' => array(
                'label' => 'Correo',
            ),
            'attributes' => array(
                'class' => 'classLSG'
            ),
        ));
        $this->add($email);

        $this->add(array(
            'name' => 'send',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Enviar',
                'title' => 'Enviar'
            ),
        ));
     }

}



module\Application\src\Application\Model\Entity\Procesa.php:
namespace Application\Model\Entity;
class Procesa {
    private $nombre;
    private $correo;
    
    public function __construct($datos=array()) {
        $this->nombre = $datos["nombre"];
        $this->correo = $datos["email"];
    }

    public function getData()  {
        $array = array(
"nombre" => $this->nombre,
"email" => $this->correo
);
        return $array;
    }
}

module\Application\view\application\formulario\fomulario.phtml:
<h1><?php echo $this->titulo?></h1>
<?php
$form = $this->form;
$form->prepare();

$form->setAttributes(array(
    'action' => $this->url.'/application/formulario/recibe',
    'method' => 'post'
));

$formLabel = $this->plugin('formLabel');
echo $this->form()->openTag($form);
?>
<div class="form_element">
<?php
    $nombre = $form->get('nombre');
echo $formLabel->openTag().$nombre->getOption('label')." ";
    echo $this->formInput($nombre);
echo $formLabel->closeTag();
?>
</div>

<div class="form_element">
<?php
    $email = $form->get('email');
    echo $formLabel->openTag().$email->getOption('label')." ";
    echo $this->formInput($email);
    echo $formLabel->closeTag();
?>
</div>
<?php 
echo $this->formElement($form->get('send'));
echo $this->form()->closeTag();
?>



module\Application\view\application\formulario\recibe.phtml:
<h1>Recibo los datos</h1>
<ul>
    <li>Nombre : <?php echo $this->datos["nombre"]?></li>
    <li>E-Mail : <?php echo $this->datos["email"]?></li>
</ul>


Para ejecutarlo:
http://localhost/zend/zendproyecto8LSG/public/application/formulario/formulario

No hay comentarios:

Publicar un comentario