Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ZF][ZendFramework] Zastosowanie filtru Digits w formularzu
Forum PHP.pl > Forum > PHP > Frameworki
vaxjokr
Witajcie, jestem dość początkującą osobą w Zend Framework (wersja 1.12).

Otóż, mój problem polega na tym, że filtry typu: Alpha, Digist, Int, itd. nie pwodują żadnego filtrowania (przepuszczają wszystko co wpiszę w kontrolce). Natomiast filtr StringTrim działa poprawnie. Prosiłbym o pomoc w ustaleniu źródła błędu.

Poniżej przedstawiam strukturę mojego projektu oraz zawartość plików.

CODE
zadanie11
|-- application
| |-- Bootstrap.php
| |-- configs
| | `-- application.ini
| |-- controllers
| | |-- ErrorController.php
| | |-- IndexController.php
| |-- forms
|-- RegisterForm.php
|-- layouts
|-- scripts
|-- layout.phtml
|-- models
| |-- views
| |-- helpers
| |-- scripts
| |-- error
| |-- index
| |-- index.phtml
|-- next.phtml
|-- library
|-- public
| |-- .htaccess
| |-- index.php



application.ini
CODE
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.frontController.params.displayExceptions = 1

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1


indexController.php
CODE
<?php

class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->form = new forms_RegisterForm();
}

public function indexAction()
{
// action body
if ($this->_request->isPost())
{
$formData = $this->_request->getPost();
if ($this->form->isValid($formData))
{
return $this->_forward('next');
}
else
{
$this->form->populate($formData);
}
}
$this->view->form = $this->form;
}

public function nextAction()
{
// action body
$this->view->dane = $this->_request->getPost();
$this->view->title = "Poprawne";
}
}


RegisterForm.php
CODE
<?php
class forms_RegisterForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
}

public function init()
{
/* Form Elements & Other Definitions Here ... */
$this->setMethod('post');
$this->setName('register_formularz');

//Wprowadź imię
$this->addElement('text', 'imie', array(
'label' => 'Podaj imie:',
'required' => true,
'size' => '30',
'filters' => array('Digits'),
'validators' => array('NotEmpty')
));

// add the submit button
$this->addElement('submit', 'submit', array(
'label' => 'wybierz',
));
}
}



index.phtml
CODE
<div>
<?php echo $this->title?>
<?php echo $this->form ?>
</div>


next.phtml
CODE
<h1><?php echo $this->escape($this->title); ?></h1>
<div>
<?php foreach($this->dane as $x)
echo "<p>$x</p>"?>
</div>


index.php
CODE
<?php

// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/../application'),
get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);

// Umożliwia ładowanie automatyczne klas
require_once "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

// Ustawia bazowy url w kontrolerze
//Zend_Controller_Front::getInstance()->setBaseUrl("/~pai/zadanko11/public");

$application->bootstrap()
->run();
lucio1988
usuń $this->form->populate($formData);
populate uzywa się głównie podczas edycji - czyli pobierasz dane z bazy i wyświetlasz je w formularzu
podczas validacji formularz jest automatycznie wypelniany danymi. To co się u ciebie dzieje:
- pobierasz dane wysyłane POST
- validujesz formularz (działają wszystkie validatory i filtry)
- jeżeli jest bład podczas validacji nadpisujesz dane zmienione przez filtry metodą populate
vaxjokr
Próbowałem usunąć tą linijkę kodu, lecz dla ciągu np. "celina54kr" zwraca "celina54kr" zamiast "54" (dla filtru digist). Masz może jeszcze sugestie co może być nie tak? W razie czego mogę udostępnić cały projekt z plikami, możw wtedy by było łatwiej dojść do tego co nie działa.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.