1

I have a symfony form and i need to add a couple of fields(widgets) dynamicly via jquery. Let´s say i have an User form and I want the user to be able to set multiple emails addresses,

The "email" is just a simple widget and not a form so using embeded forms ,like most of the tutorials avaliable in the net, doesn´t make sense.

What is the best approach to this?

1
  • I would prefer a way to put all my code inside the form classes and not create form elements manually. Commented Feb 12, 2011 at 12:17

1 Answer 1

1

The suggested way would be to use the embedded form, but I see how that could not be useful to you (given the form inputs are added via Javascript).

One solution I suggest (while eager to see how other people would solve this) is to remove the email field from the widget schema and only add it to your validation schema (with custom callback).

Not tested but something along the lines as a guide.

class YourForm extends sfForm {

  public function configure() {
    $this->setValidators(array(
      'email' => new sfValidatorCallback(array(
        'callback' => array($this, 'my_email_validator')
      ))
    ));
  }

  public function my_email_validator($validator, $value) {
    $email_validator = new sfValidatorEmail();
    try {
      foreach($value as $email) { // multiple fields with the same name
        $email_validator->clean($email); // throws exception when not valid
      }
    }
    catch(sfValidatorError $e) {
      // $email is invalid - notify user or whatever
      // you could also rethrow a sfValidatorError so that the form->isValid
      // would return false
    }
  }
}

And you would have in your template the field (manually yes).

<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />
<input type="text" name="email[]" />

I would still like to see others suggestion, because this looks hackish to me as well.

Sign up to request clarification or add additional context in comments.

5 Comments

What about a similar aproach to tihs tutorial tech.cibul.org/embedded-forms-with-symfony-1-4-and-jquery but without using embeded forms, instead add normal widgets and validators to the form? Can this be done? I dont want to create a form just for a simple email field to use embeded forms.
I looked over the article you provided, and while he achieves it through embedded forms I find it overly complicated. The advantage with the solution provided in that article is that you don't have to handle the validation manually. As with your question of achieving that solution without embedded forms, I cannot see it possible.
Since nobody give other ideas i have implemented what you said but i have a problem. Because of the form name format i have to name my inputs like this: user[email][]. The problem is that in the doSave() method i get "Array" as the value for the email field and not a real array with the values.
You'll have to override the updateObject method so save the array as a relation.
I solved it. In the validator i need to add this line return (array)$value; after the foreach loop. This way symfony returns the correct values as array for use in updateObject().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.