Я построил следующую форму на основе учебника 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)) ?>