Mentre sono costretto momentaneamente ad utilizzare Opera durante l’atteso aggiornamento a Firefox 3, scopro che finalmente comincia ad esserci della documentazione decente sulla maggiore novità della più recente incarnazione di symfony: il Form Framework.
Finora su form c’era qualcosa qua e la, ma niente di concreto. Ora è iniziata con quattro capitoli la pubblicazione dello pseudo libro “the symfony Forms in Action“. Grande notizia! Utilizzando questa documentazione, più prendendo spunto da uno snippet all’interno della documentazione, ho creato un completo form di login per il mio forum con symfony.
Il form si poggia su tre file, all’interno della directory lib/: lib/myLoginValidator.class.php, lib/sfWidgetFormSchemaFormatterDiv e lib/form/LoginForm.class.php. Vediamoli:
LoginForm.class.php
[source:php]
<?php
/**
* Login form.
*
* @package form
* @subpackage login
* @version SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
*/
class LoginForm extends sfForm
{
private function initValidators()
{
$this->usernameValidator = new sfValidatorAnd
(
array
(
new sfValidatorString
(
array
(
‘min_length’ => 4,
‘max_length’ => 50
),
array
(
‘min_length’ => ‘The username should be al least three characters long’,
‘max_length’ => ‘The username should be be fifty characters long al most’
)
),
new sfValidatorCallback
(
array
(
‘callback’ => array
(
‘myLoginValidator’, ‘execute’
),
‘arguments’ => array
(
)
),
array
(
‘invalid’ => ‘This username/password combination is unknown’
)
)
),
array
(
‘required’ => true
),
array
(
‘required’ => ‘The username is mandatory’
)
);
$this->passwordValidator = new sfValidatorString
(
array
(
‘required’ => true
),
array
(
‘required’ => ‘The password is mandatory’
)
);
$this->referrerValidator = new sfValidatorString
(
array
(
‘required’ => false
)
);
}
private function initWidgets()
{
$this->usernameWidget = new sfWidgetFormInput
(
array(),
array
(
‘id’ => ‘username’,
‘size’ => 25
)
);
$this->passwordWidget = new sfWidgetFormInputPassword
(
array(),
array
(
‘id’ => ‘password’,
‘size’ => 25
)
);
$this->referrerWidget = new sfWidgetFormInputHidden
(
array(),
array
(
‘id’ => ‘referrer’
)
);
}
public function configure()
{
$this->initValidators();
$this->initWidgets();
$this->setValidators
(
array
(
‘username’ => $this->usernameValidator,
‘password’ => $this->passwordValidator,
‘referrer’ => $this->referrerValidator
)
);
$this->setWidgets
(
array
(
‘username’ => $this->usernameWidget,
‘password’ => $this->passwordWidget,
‘referrer’ => $this->referrerWidget
)
);
$decorator = new sfWidgetFormSchemaFormatterDiv
(
$this->getWidgetSchema()
);
$this->getWidgetSchema()->addFormFormatter
(
‘div’, $decorator
);
$this->getWidgetSchema()->setFormFormatterName
(
‘div’
);
$this->getWidgetSchema()->setHelps
(
array
(
‘username’ => ‘Please enter your username’,
‘password’ => ‘Please enter your password’
)
);
}
}
[/source]
sfWidgetFormSchemaFormatterDiv
[source:php]
<?php
class sfWidgetFormSchemaFormatterDiv extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = ‘%field%
%error%
‘,
$helpFormat = ‘<span class=”help”>%help%</span>’,
$errorRowFormat = ‘<div>%errors%</div>’,
$errorListFormatInARow = ‘%errors%’,
$errorRowFormatInARow = ‘<div class=”clearer”></div><span class=”formError”>%error%</span><div class=”clearer”></div>’,
$namedErrorRowFormatInARow = ‘%name%: %error%<br />’,
$decoratorFormat = ‘<div id=”formContainer”>%content%</div>’;
}
[/source]
myLoginValidator
[source:php]
<?php
class myLoginValidator
{
public static function execute($validator, $value, $arguments)
{
$username = sfContext::getInstance()->getRequest()->getParameter(‘username’);
$password = sfContext::getInstance()->getRequest()->getParameter(‘password’);
$c = new Criteria();
$c->add(AuthorPeer::NAME, $username);
$c->add(AuthorPeer::PASSWORD, $password);
$user = AuthorPeer::doSelectOne($c);
if ($user)
{
sfContext::getInstance()->getUser()->setData(
$user->getID(),
$user->getGroupID(),
$user->getName()
);
}
else
{
throw new sfValidatorError($validator, ‘invalid’, array(‘value’ => $value, ‘invalid’ => $validator->getOption(‘invalid’)));
}
}
}
[/source]
L’uso è molto semplice, come vediamo ora nell’azione e nel template:
apps/frontend/modules/user/actions/actions.class.php
[source:php]
public function executeLogin($request)
{
$this->getUser()->clearData();
$this->getUser()->setLogged(false);
$this->form = new LoginForm();
if ($request->isMethod(‘post’))
{
$this->form->bind(
array (‘username’ => $request->getParameter(‘username’),
‘password’ => $request->getParameter(‘password’),
‘referrer’ => $request->getParameter(‘referrer’)
)
);
$this->form->setDefault(‘referrer’, $request->getParameter(‘referrer’));
if ($this->form->isValid())
{
$this->getUser()->setLogged(true);
$this->redirect($request->getParameter(‘referrer’));
}
else
{
$this->form->setDefault(‘referrer’, $request->getParameter(‘referrer’));
}
}
else
{
$this->form->setDefault(‘referrer’, $_SERVER['HTTP_REFERER']);
}
}
[/source]
apps/frontend/modules/user/templates/loginSuccess.php
[source:php]
<h1>Login</h1>
<div id=”formContainer”>
<?php if ($form->getErrorSchema()->getErrors()): ?>
<div id=”errors”>
<ul>
<?php foreach($form->getErrorSchema() as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form action=”<?php echo url_for(‘@user_login’) ?>” method=”post”>
<div class=”formElement”>
<?php echo $form['username']->renderLabel($form['username']->renderLabelName()) ?>
<?php echo $form['username']->renderRow($form->getWidgetSchema()->getHelp(‘username’)) ?>
</div>
<div class=”clearer”></div>
<div class=”formElement”>
<?php echo $form['password']->renderLabel($form['password']->renderLabelName()) ?>
<?php echo $form['password']->renderRow($form->getWidgetSchema()->getHelp(‘password’)) ?>
</div>
<div class=”clearer”></div>
<div class=”formElement”>
<?php echo $form['referrer']->render(array(‘value’ => $form->getDefault(‘referrer’))) ?>
</div>
<div class=”clearer”></div>
<div class=”formElement”>
<button type=”submit”>Login</button>
</div>
</form>
</div>
[/source]
Per oggi ho intenzione di lasciarvi con questa vagonata di codice da studiare, ed un consiglio: date un’occhiata alla documentazione, che come sempre è per symfony di altissimo livello. Nel caso qualcosa non fosse chiaro non esitate a chiedere, nei commenti. A presto!





Sono Daniel Londero un web developer che lavora ogni giorno con PHP ormai da 4 anni.
Da 2 anni a questa parte mi sono molto interessato a
Questo articolo è stato segnalato su ZicZac.it….
…
symfony 1.1: finalmente la documentazione…
Mentre sono costretto momentaneamente ad utilizzare Opera durante l’atteso aggiornamento a Firefox 3, scopro che finalmente comincia ad esserci della documentazione decente sulla maggiore novità della più recente incarnazione di symfony: il Form Fr…
symfony 1.1: finalmente la documentazione…
Mentre sono costretto momentaneamente ad utilizzare Opera durante l’atteso aggiornamento a Firefox 3, scopro che finalmente comincia ad esserci della documentazione decente sulla maggiore novità della più recente incarnazione di symfony: il Form Fr…