# event für kundendaten änderung?

**URL:** <https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662>\
**Category:** Programmierung\
**Created:** [25. Januar 2018 um 12:51 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662 "2018-01-25T12:51:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wontfix](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@wontfix](https://forum.shopware.com/u/wontfix)\
**Post date:** [25. Januar 2018 um 12:51 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662/1 "2018-01-25T12:51:07Z")

</div>

wir wollen alle kundenadressen vor speichern nach unserem regeln prüfen.

gibts es ein generelles backend event für “kunde hat neue addressdaten eingegeben”?

bzw. wo überall müssen wir events/hooks anhängen um alle address änderungen zu prüfen?

---

<div class="post-metadata">

**Author:** ![wontfix](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@wontfix](https://forum.shopware.com/u/wontfix)\
**Post date:** [26. Januar 2018 um 12:31 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662/2 "2018-01-26T12:31:35Z")

</div>

ok then, down the rabbit hole we go…

> note: this is on shopware 5.2.27

we should first findout where all the client-address changes land on submit. with luck we can find one common place, and save duplicating code.

- `/checkout/confirm` submit lands in `engine/Shopware/Controllers/Frontend/Register.php::saveRegisterAction`
- `/account` submit lands in `engine/Shopware/Controllers/Frontend/Register.php::saveRegisterAction`
- `/address/edit/id/128625` submit lands in `engine/Shopware/Controllers/Frontend/Address.php::editAction`
- `/address/create` submit lands in `engine/Shopware/Controllers/Frontend/Address.php::createAction`

> sorry: above looks horrible, but this forum doesnt seem to have tables…

i hope thats all the places where we get new address data from the client. lets see if we can find a place they share, and can event/hook to:

- `$form->isValid()` is used by `editAction` and `createAction`, but not by `saveRegisterAction`. dammit!
- sidenote, i wonder why `saveRegisterAction` has `$registerService = $this->get('shopware_account.register_service');` yet never uses that `$registerService`

nope, doesnt look like they share anything we could event/hook to, dammit!

as there are no events emitted, we can only hook to all these three functions. well then, lets see how we can get the client address data to our hooks, and give our error messages back to the client…

```
public function beforeSaveRegisterAction (\Enlight_Hook_HookArgs $args)
{
    // yay, we can get the client data
    $data = $args->getSubject()->Request()->getPost();
    var_dump($data);

    // but we cannot set errors from here, cause parent overwrites them...
    die();
}

```

so thanks to this code in `saveRegisterAction`:

```
$errors = [
    'personal' => $this->getFormErrors($customerForm),
    'billing' => $this->getFormErrors($billingForm),
    'shipping' => [],
];

```

the only possible function we can hook to in here is `getFormErrors`. but hey, the first code in `getFormErrors` is our old friend `$form->isValid()`. yay! we need to checkout how to extend that…

so the `$form` is a `Symfony\Component\Form\FormInterface` and according to composer.json its from symfony version „2.8.17“. so lets follow a new rabbit hole…

- [Forms (Symfony 2.8 Docs)](https://symfony.com/doc/2.8/forms.html#form-validation)
- [Validation (Symfony 2.8 Docs)](https://symfony.com/doc/2.8/validation.html)
- [https://symfony.com/doc/2.8/validation/custom\_constraint.html](https://symfony.com/doc/2.8/validation/custom%5C_constraint.html)

also found these when searching „validator“ in shopware forum:

- [[Shopware52] Alternative zu ...ValidateStep1\_FilterStart und ValidateStep2\_FilterStart - #14 von mfischer1620 - Programmierung - Shopware Community Forum](https://forum.shopware.com/discussion/comment/210369)
- [Address Management Guide](https://developers.shopware.com/developers-guide/address-management-guide/#extending-the-address-form)

ok, so in theory we simply create a symfony validator constraint and stick it in shopware logic?

almost there!

```
class FooAddressCheck extends Plugin
{
    public static function getSubscribedEvents ()
    {
        return [
            'Shopware_Form_Builder' => 'onFormBuild',
        ];
    }

    public function onFormBuild (\Enlight_Event_EventArgs $event)
    {
        if ($event->getReference() !== \Shopware\Bundle\AccountBundle\Form\Account\AddressFormType::class) {
            return;
        }

        $builder = $event->getBuilder();

        $builder->add('zipcode', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
            'constraints' => [
                new \Symfony\Component\Validator\Constraints\Email(['message' => 'PLZ muss eine E-Mailadresse sein :P'])
            ]
        ]);
    }
}

```

above test works nicely (at least for client register), but i cant for the life of me get my own validator constraint to work…

gotta do some more poking next week.

---

<div class="post-metadata">

**Author:** ![wontfix](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@wontfix](https://forum.shopware.com/u/wontfix)\
**Post date:** [29. Januar 2018 um 07:47 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662/3 "2018-01-29T07:47:14Z")

</div>

YAY! it works. i was loading the constraint validator, not the constraint in my plugin \*doh\*…

heres a working example:

```
namespace FooAddressCheck;

use Shopware\Components\Plugin;
use FooAddressCheck\Validator\Constraints\ZipIsNotIsland;

class FooAddressCheck extends Plugin
{
    public static function getSubscribedEvents ()
    {
        return [
            'Shopware_Form_Builder' => 'onFormBuild',
        ];
    }

    public function onFormBuild (\Enlight_Event_EventArgs $event)
    {
        if ($event->getReference() !== \Shopware\Bundle\AccountBundle\Form\Account\AddressFormType::class) {
            return;
        }

        $builder = $event->getBuilder();

        $builder->add('zipcode', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
            'constraints' => [
                new ZipIsNotIsland()
            ]
        ]);
    }
}

namespace FooAddressCheck\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class ZipIsNotIsland extends Constraint
{
    public $message = "what {{ string }} ever";
}

namespace FooAddressCheck\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ZipIsNotIslandValidator extends ConstraintValidator
{
    public function validate ($value, Constraint $constraint)
    {
        if ($value != "12345") { // TODO: compare to actual list from plugin options
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ string }}', $value)
                ->addViolation()
            ;
        }
    }
}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![wontfix](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@wontfix](https://forum.shopware.com/u/wontfix)\
**Post date:** [1. Februar 2018 um 08:43 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662/4 "2018-02-01T08:43:45Z")

</div>

hmm, ich nehme an mit symfony validator contstraint kann man aber nicht die komplette adresse (stasse +&nbsp;plz +&nbsp;ort) prüfen, oder?

---

<div class="post-metadata">

**Author:** ![wontfix](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@wontfix](https://forum.shopware.com/u/wontfix)\
**Post date:** [1. Februar 2018 um 09:09 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662/5 "2018-02-01T09:09:28Z")

</div>

we actually can! in the validator:

```
$data = $this->context->getRoot()->getData();
$city = $data->getCity();
$street = $data->getStreet();
$zipcode = $data->getZipcode();
```

---

<div class="post-metadata">

**Author:** ![wontfix](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@wontfix](https://forum.shopware.com/u/wontfix)\
**Post date:** [5. Februar 2018 um 12:26 UTC](https://forum.shopware.com/t/event-fur-kundendaten-anderung/50662/6 "2018-02-05T12:26:28Z")

</div>

FWIW, i also found [http://symfony.com/doc/2.8/reference/constraints/Callback.html](http://symfony.com/doc/2.8/reference/constraints/Callback.html)

“The purpose of the Callback constraint is to create completely custom validation rules and to assign any validation errors to specific fields on your object”

sounds like a perfect fit to check complete address (eg. multiple fields: street + zip + city).

but looks like i’m too stupid to implement it, so had to rage quit… if anyone figures out how to use that from a shopware plugin, please do post!

here my fails:

- $event-\>getBuilder() in shopware event Shopware\_Form\_Builder returns a Form, not a FormBuilder…
- addValidator() is gone [validation - symfony2 2.3 missing FormBuilder::addValidator() how to adapt to FormBuilderInterface? - Stack Overflow](https://stackoverflow.com/questions/19006195/symfony2-2-3-missing-formbuilderaddvalidator-how-to-adapt-to-formbuilderinte)
- addEventListener() does not pass ExecutionContextInterface to the callback…
