简体   繁体   中英

How to pre populate a field according to previous values in a form?

(I'm using Symfony2) I have a form that asks for the name and the firstnname and also the email address.

The email address is always on the same pattern : firstname.name@mycompany.com

For example, I would like that when the user for example enters 'John' for the first name and 'Adams' for the last name, the email address be set to : 'john.adams@mycompany.com' by default (but it should not be disabled because it can change sometimes a little, if the user has complex names etc. ...).

I have never done such a thing and I'm stuck with this problem I don't manage to solve although maybe it is simple.

Any help would be much appreciated
Thanks a lot

Im think there is many ways :

Solution 1 : In the entity

public function setEmail($email)
{
        $email = $this->email.firstName.'.'.$this->lastName.'@domain.com'
        $this->email = $email;
}

Solution 2 : In the controller

in the createAction and in the updateAction

$email = $entity->getEmail().'.'.$entity->getLastName().'@domain.com';
$entity->setEmail($email);

Solution 3 : you can do it directly in javascript (with jquery it's easy) you put event on Keyup to generate the email field.

$('#form_firstName').live('keyup',function() {
       var email = $('#form_firstName').val()+'.'+$('#form_lastName').val().'@domain.com';
    $('#form_email').val(email);
});
$('#form_lastName').live('keyup',function() {
       var email = $('#form_firstName').val()+'.'+$('#form_lastName').val().'@domain.com';
    $('#form_email').val(email);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM