What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hello everyone, On the CheckoutAddressCollectionForm, I'm trying to change the defaults for the Sal

U01EDJAC1AB
U01EDJAC1AB Posts: 11 πŸ§‘πŸ»β€πŸš€ - Cadet

Hello everyone,

On the CheckoutAddressCollectionForm, I'm trying to change the defaults for the Salutation and the Country of the billing address. On the billing form (and only there), they are set to Mr. and Albania (the first country in the available countries array). Could someone point me to the FormBuilder option to change these?

Comments

  • U01K43ADW5N
    U01K43ADW5N Posts: 69 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited July 2021

    I had the same problem a few months ago, changing the order of the countries array for our store on the file config/Shared/stores.php did the trick

  • U01K43ADW5N
    U01K43ADW5N Posts: 69 πŸ§‘πŸ»β€πŸš€ - Cadet

    The first country on the array will be the default

  • U01K43ADW5N
    U01K43ADW5N Posts: 69 πŸ§‘πŸ»β€πŸš€ - Cadet

    Now, for the salutation you have to extend the method addSalutationField of the class CheckoutAddressForm

  • U01K43ADW5N
    U01K43ADW5N Posts: 69 πŸ§‘πŸ»β€πŸš€ - Cadet

    For example:

    /**
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
     * @param array $options
     *
     * @return \SprykerShop\Yves\CustomerPage\Form\AddressForm
     */
    public function addSalutationField(FormBuilderInterface $builder, array $options)
    {
        $builder->add(self::FIELD_SALUTATION, ChoiceType::class, [
            'choices' => array_flip([
                'Ms' => '[customer.salutation.ms](http://customer.salutation.ms)',
                'Mr' => '[customer.salutation.mr](http://customer.salutation.mr)',
            ]),
            'label' => 'profile.form.salutation',
            'required' => true,
            'trim' => true,
            'constraints' => [
                $this->createNotBlankConstraint($options),
            ],
        ]);
    
        return $this;
    }
    
  • U01EDJAC1AB
    U01EDJAC1AB Posts: 11 πŸ§‘πŸ»β€πŸš€ - Cadet

    Thank you, @U01K43ADW5N. It's funny, I did what you suggested and it seems that I ran into some kind of client side caching issue earlier.

    Now it does pre-select the first value with an implementation that mimics your code.

  • U01EDJAC1AB
    U01EDJAC1AB Posts: 11 πŸ§‘πŸ»β€πŸš€ - Cadet

    Here's my addSalutationField method:

    /**
         * @param \Symfony\Component\Form\FormBuilderInterface $builder
         * @param array $options
         *
         * @return $this
         */
        public function addSalutationField(FormBuilderInterface $builder, array $options)
        {
            $builder->add(self::FIELD_SALUTATION, ChoiceType::class, [
                'choices' => array_flip([
                    '' => 'global.select.placeholder.value',
                    'Mr' => '[customer.salutation.mr](http://customer.salutation.mr)',
                    'Ms' => '[customer.salutation.ms](http://customer.salutation.ms)',
                    'Mrs' => 'customer.salutation.mrs',
                    'Dr' => 'customer.salutation.dr',
                ]),
                'label' => 'profile.form.salutation',
                'required' => true,
                'trim' => true,
                'constraints' => [$this->createNotBlankConstraint($options)],
            ]);
    
            return $this;
        }
    

    And here the custom placeholder for the country selector (since I need an empty selector field, I could not use the store config):

    /**
         * @param \Symfony\Component\Form\FormBuilderInterface $builder
         * @param array $options
         *
         * @return $this
         */
        protected function addIso2CodeField(FormBuilderInterface $builder, array $options)
        {
            // Add blank preselect to address selector
            $choices = ['global.select.placeholder.value' => ''];
            $choices += array_flip($options[self::OPTION_COUNTRY_CHOICES]);
    
            $builder->add(self::FIELD_ISO_2_CODE, ChoiceType::class, [
                'label' => 'customer.address.country',
                'required' => true,
                'choices' => $choices,
                'constraints' => [$this->createNotBlankConstraint($options)],
            ]);
    
            return $this;
        }
    
  • U01EDJAC1AB
    U01EDJAC1AB Posts: 11 πŸ§‘πŸ»β€πŸš€ - Cadet

    Hi @UK7KBE2JW, I think that's what I was looking for. Thank you!

  • UK7KBE2JW
    UK7KBE2JW Posts: 463 πŸ§‘πŸ»β€πŸš€ - Cadet

    your welcome