Vamos a usar 5 campos para el ejemplo(Crean una tabla con los campos siguientes, a la tabla la llamaremos "posts").
1. id
2. nombre
3. historia
4. fecha
5. email
SQL
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL auto_increment,
`nombre` varchar(100) NOT NULL,
`historia` tinytext NOT NULL,
`fecha` varchar(15) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;
Vamos a nuestra carpeta de la aplicación y creamos el modelo relacionado a la tabla. Usamos el metodo initialize para validar los datos (mas info acá)
PHP models/posts.php
<?php
class Posts extends ActiveRecord{
public function initialize(){
$this->validates_email_in('email');
}
}
Creamos nuestro controller.
PHP controllers/acciones_controller.php
<?php
class AccionesController extends ApplicationController{
public $models = array('posts');
public function publicar(){
$this->data['title'] = "Publica tu historia";
if($this->has_post('posts')){
$post = new Posts($this->post('posts'));
//insertamos la fecha en formato unix
$post->fecha = time();
/*recibimos el campo historia y le aplicamos el filter "striptags" usamos
* autocarga del helper en boot ini [modules] libs = filter
*/
$post->historia = $this->post('posts.historia', 'striptags');
/*
* Ayuda general
print "<pre>";
print_r($post);
print "</pre>";
*/
//Insertamos los datos recibidos(devuelve true en caso de éxito)
if(!$post->save()){
//Enviar mensaje de error falla la operacion
Flash::error('No se guardo los datos :( ');
$this->posts = $this->post('posts'); //reenviamos los datos al form
}
else{
Flash::success('Se guardo con exito');
}
}
}
}
Nuestra vista:
PHP views/acciones/publicar.phtml
<?php echo View::content(); ?>
<?php echo form_tag("acciones/publicar"); ?>
Tu Nombre: <?php echo text_field_tag(array('posts.nombre')); ?>
Tu historia: <?php echo text_field_tag(array('posts.historia')); ?>
Tu Email: <?php echo text_field_tag(array('posts.email')); ?>
<?php echo submit_tag("Enviar") ?>
<?php echo end_form_tag() ?>
Probamos nuestro formulario:
http://kumbia/acciones/publicar
Los datos se hacen pesistentes si hay algun campo que no es validado, al usar el filtro "filter" el campo historia lo guardara eliminando los tags html.
Por cierto estoy usando el template global.
PHTML views/templates/default.phtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-type' content='text/html; charset=<?php echo APP_CHARSET ?>' />
<title><?php echo $data['title']; ?></title>
<?php echo stylesheet_link_tag('style') ?>
<?php echo stylesheet_link_tag('exception') ?>
<?php echo stylesheet_link_tags() ?>
</head>
<body>
<div id='content'>
<?php View::content(); ?>
</div>
</body>
</html>
Y eso es todo, espero que vean lo realmente fácil que es trabajar con formularios seguros en Kumbiaphp.
El original esta sacádo de la propia wiki de Kumbiaphp(Como hacer un crud paso a paso).

1 comentarios: