我根据Symfony教程构建了以下表单。
UsersForm.class.php:
class UsersForm extends BaseUsersForm
{
public function configure()
{
$this->useFields(array('username', 'password', 'email', 'tara', 'gen'));
$this->widgetSchema['password'] = new sfWidgetFormInputPassword();
$this->widgetSchema['email'] = new sfWidgetFormInputText();
$this->validatorSchema['email'] = new sfValidatorAnd(array(
$this->validatorSchema['email'],
new sfValidatorEmail(),
));
$this->widgetSchema['tara'] = new sfWidgetFormChoice(array(
'choices' => Doctrine_Core::getTable('Users')->getTara(),
'expanded' => false,
'multiple' => false,
));
$this->validatorSchema['tara'] = new sfValidatorChoice(array(
'choices' => array_keys(Doctrine_Core::getTable('Users')->getTara()),
));
$this->widgetSchema['gen']= new sfWidgetFormChoice(array(
'choices' => Doctrine_Core::getTable('Users')->getGen(),
'expanded' => false,
'multiple' => false,
));
$this->validatorSchema['gen'] = new sfValidatorChoice(array(
'choices' => array_keys(Doctrine_Core::getTable('Users')->getGen()),
));
$this->widgetSchema->setLabels(array(
'username' => 'Utilizator',
'password' => 'Parolă',
'email' => 'Adresă de email',
'tara' => 'Ţara',
'gen' => 'Gen',
));
$this->setValidators(array(
'username' => new sfValidatorString(array('min_length' => 4),
array('required' => 'Câmp obligatoriu',
'min_length' => 'Minim %min_length% caractere.',)),
'password' => new sfValidatorString(array('min_length' => 4),
array('required' => 'Câmp obligatoriu',
'min_length' => 'Minim %min_length% caractere.',)),
));
$this->widgetSchema['captcha'] = new sfWidgetFormReCaptcha(array(
//'public_key' => sfConfig::get('app_recaptcha_public_key')
'public_key' => '/*...*/'
));
$this->validatorSchema['captcha'] = new sfValidatorReCaptcha(array(
//'private_key' => sfConfig::get('app_recaptcha_private_key')
'private_key' => '/*...*/'
));
}
}
的actions.class.php:
public function executeCreate(sfWebRequest $request)
{
$this->setLayout(false);
$this->form = new UsersForm();
$this->processForm($request, $this->form);
}
public function executeRegister(sfWebRequest $request)
{
$this->setLayout('register');
$this->form = new UsersForm();
$this->setTemplate('register');
}
protected function processForm(sfWebRequest $request, sfForm $form)
{
$captcha = array(
'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'),
);
$form->bind(array_merge(
$request->getParameter($form->getName()),
array('captcha' => $captcha)));
if ($form->isValid())
{
$user = $form->save();
$this->redirect('/');
}
}
的routing.yml:
register_create:
url: /register.:sf_format
class: sfDoctrineRoute
options: { model: Users, type: object }
param: { module: home, action: register, sf_format: html }
requirements: { sf_method: post }
register_new:
url: /register.:sf_format
class: sfDoctrineRoute
options: { model: Users, type: object }
param: { module: home, action: register, sf_format: html }
虽然未显示错误,也未更新数据库。当我提交表单时,它更像是刷新。
我是Symfony的新手,我自己找不到错误。
LE
_form.php这个:
<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>
<?php echo form_tag_for($form, '@register') ?>
<table id="register_form">
<tfoot>
<tr>
<td colspan="2">
<input type="submit" value="Înregistrează-te" />
</td>
</tr>
</tfoot>
<tbody>
<?php echo $form ?>
</tbody>
</table>
</form>
registerSuccess.php:
<?php include_partial('form', array('form' => $form)) ?>
答案 0 :(得分:1)
表单可能会抛出未呈现的错误。尝试在BaseForm
类(lib / form / BaseForm.class.php)中包含此调试代码,并查看在视图中调用它时获得的输出。
public function debug()
{
if (sfConfig::get('sf_environment') != 'dev')
{
return;
}
foreach($this->getErrorSchema()->getErrors() as $key => $error)
{
echo '<p>' . $key . ': ' . $error . '</p>';
}
}
答案 1 :(得分:0)
添加
后$this->widgetSchema->setNameFormat('register[%s]');
到UsersForm.class.php
和
$this->form->bind(array_merge(
$request->getParameter('register'),
array('captcha' => $captcha)));
to actions.class.php;我设法得到表格来显示错误。