开发者

How can I toggle which fields are required after a form has been submitted in Symfony?

The system I'm working this is based around users and charity accounts, everyone has a user account but charities have开发者_JS百科 a user account and a charity record tied to the user account with a user id that contains some extra fields.

I need one registration form that allows people to sign up as either an individual (with just a user account) or a charity (user account + charity record) so I've embedded the charity form into my user registration form and added a radio button question at the top to find out which type of account the user is signing up for.

What I need is that when the user indicates they want a charity account for the charity fields to become required and when they indicate they want to be an individual for them to not be (I hide them with Javascript when they select this).

I've tried running this when the user selects charity but it doesn't seem to work:

$this->register_form->getEmbeddedForm('charity')->getValidator("name")->setOption("required", true)

Can anyone let me know what the correct way to do this is?


First of all it's probably better to keep this logic within the form class instead of doing it in the action. Secondly when embedding a form it copies the validatorschema to the parent form, that's why changing the option on the embedded form itself has no effect.

Another way of accomplishing what you are trying to do could be this:

class myForm extends BaseForm {

  public function configure() {
    // set up widgets, validators etc
  }

  protected function doBind($values) {
    is (!empty($values['charity']['charityCheckbox']) {
      $this->validatorSchema['charity']['name']->setOption('required', true);
    }
    parent::doBind($values);
  }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜