开发者

Symfony form - doesn't submit nor show the errors

I've built the following form based on the Symfony tutorial.

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 sfValidatorCh开发者_如何学JAVAoice(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 }

Although the errors are not displayed nor the database is updated. It's more like a refresh when I submit the form.

I am -very- newbie in Symfony and I can't find the error myself.

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)) ?>


The form is likely throwing an error that is not getting rendered. Try including this debug code in your BaseForm class (lib/form/BaseForm.class.php) and seeing what output you get when you call it in the view.

public function debug()
{
  if (sfConfig::get('sf_environment') != 'dev')
  {
    return;
  }
  foreach($this->getErrorSchema()->getErrors() as $key => $error)
  {
    echo '<p>' . $key . ': ' . $error . '</p>';
  }
}


After adding

$this->widgetSchema->setNameFormat('register[%s]');

to UsersForm.class.php

and

$this->form->bind(array_merge(
        $request->getParameter('register'),
        array('captcha' => $captcha)));

to actions.class.php; I managed to get the form to show the errors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜