Jump to content

Help to make birthdate field required in 1.7.2


mr_absinthe

Recommended Posts

hi

Open this file classes/form/CustomerFormatter.php

In Prestashop 1.7.2.0, line 193 just add "->setRequired(true)" as the following screenshot: http://prntscr.com/gcdtvm 

On other Prestashop 1.7.x version the line may be different but you just find the birthday field and set it required as my screenshot above

If you want to keep the changes for future updates, you should override the class instead of making direct changes as my guide

 

Good luck

 

Trong

  • Like 7
Link to comment
Share on other sites

:)  Works as expected! Thank you! Would you be so kind as to share the class override please? I have also removed mr & mrs from the same file thus it would be nice to have that all in one override file.

 

you're welcome.

what is your Prestashop version? I'll help make an override file and send it to you

Link to comment
Share on other sites

Thank you very much, I have v 1.7.2. Do you think it's possible to include the removal of mr & mrs in that file as well? I have edited two files as advised here: https://www.prestashop.com/forums/topic/623679-help-to-remove-social-title-mr-mrs-in-v172/?p=2596119

 

I just tried to make an override file but seems Prestashop 1.7 have problem, it ignore the override.

so just keep the changes in your core files

Link to comment
Share on other sites

I just tried to make an override file but seems Prestashop 1.7 have problem, it ignore the override.

so just keep the changes in your core files

That's the reason why experienced developers are suggesting TO DON'T USE Prestashop 1.7. IN PRODUCTION. It is still in development, full of bugs and also a version without any coding standard, cause it is half smarty half symfony and both not compatible together. PS 1.7. is for developers to play and learn, but not a version for sellers.

Best is to sort it out and wait for next major version, which should be 100% symfony framework instead of a hybrid non-working version.

Link to comment
Share on other sites

I just tried to make an override file but seems Prestashop 1.7 have problem, it ignore the override.

so just keep the changes in your core files

Thank you anyway for trying.

 

That's the reason why experienced developers are suggesting TO DON'T USE Prestashop 1.7. IN PRODUCTION. It is still in development, full of bugs and also a version without any coding standard, cause it is half smarty half symfony and both not compatible together. PS 1.7. is for developers to play and learn, but not a version for sellers.

Best is to sort it out and wait for next major version, which should be 100% symfony framework instead of a hybrid non-working version.

And that will be what version in your opinion? And what else could be used? Older version...?

Link to comment
Share on other sites

  • 10 months later...
On 24.8.2017 at 9:11 AM, ets-soft said:

... file classes/form/CustomerFormatter.php

...line 193 just add "->setRequired(true)" as the following screenshot: http://prntscr.com/gcdtvm 

...

 

Thank you for this fast and easy solution!

for the updates an override would be cool. i have prestashop 1.7.4.1 at the moment.

 

i have read the documentation for the overrides but i can't understand it :D

I know  i have to write the new code that replaces the old code, but how do i define exactly what code is to be replaced?

 

and one thing that would be really cool: is it possible to define in that override that prestashop checks the age of the customer on registration? so only over 18 can register?

Link to comment
Share on other sites

On 8/17/2017 at 2:45 PM, mr_absinthe said:

Does anybody know how to make birthday field in registration form required (not optional) in v 1.7.2? There is no such option in settings and none of those guides I found work in this version.

 

Please find the answer in below post. I commented for mobile number required. You can follow same for birth date also.

https://www.prestashop.com/forums/topic/867885-how-to-make-mobile-number-mandatory-prestashop-17/

Link to comment
Share on other sites

  • 1 year later...
  • 1 month later...
On 3/30/2020 at 1:59 PM, Puppo said:

Works perfect! Thank you.

I someone can show how to make a override with this fix it would be great!

Create override/classes/form/CustomerFormatter.php

<?php
use Symfony\Component\Translation\TranslatorInterface;

class CustomerFormatter extends CustomerFormatterCore
{
    private $translator;
    private $language;

    //translator and language are private in parent class, override need
    public function __construct(
      TranslatorInterface $translator,
      Language $language
    ) {
      $this->translator = $translator;
      $this->language = $language;
        
      parent::__construct($translator,$language);
    }
    
    public function getFormat()
    {
      $format = parent::getFormat();
      
      //override/add all customisations for fields
      $format['birthday'] = (new FormField())
        ->setName('birthday')
        ->setType('text')
        ->setLabel(
        $this->translator->trans(
          'Birthdate',
          [],
          'Shop.Forms.Labels'
        )
      )
        ->addAvailableValue('placeholder', Tools::getDateFormat())
        ->addAvailableValue(
        'comment',
        $this->translator->trans('(E.g.: %date_format%)', array('%date_format%' => Tools::formatDateStr('31 May 1970')), 'Shop.Forms.Help')
      )
        ->setRequired(true);

      //As addConstraints method is private we need to call the logic here. We don't need to iterate over all the fields again, just the changed ones.
      $constraints = Customer::$definition['fields'];
      $field = $format['birthday'];

      if (!empty($constraints[$field->getName()]['validate'])) {
        $field->addConstraint(
          $constraints[$field->getName()]['validate']
        );
      }

      return $format;
    }
}

 

If you need a value check like $ask_for_birthdate just override it like the __construct method:

private $ask_for_birthdate = true;

public function setAskForBirthdate($ask_for_birthdate)
{
  $this->ask_for_birthdate = $ask_for_birthdate;

  return parent::setAskForBirthdate($ask_for_birthdate);
}

 

Edited by ByteEmotions
code format (see edit history)
  • Like 1
Link to comment
Share on other sites

On 7/22/2018 at 4:34 PM, backamblock said:

 

Thank you for this fast and easy solution!

for the updates an override would be cool. i have prestashop 1.7.4.1 at the moment.

 

i have read the documentation for the overrides but i can't understand it :D

I know  i have to write the new code that replaces the old code, but how do i define exactly what code is to be replaced?

 

and one thing that would be really cool: is it possible to define in that override that prestashop checks the age of the customer on registration? so only over 18 can register?

See my last post for override the birthday field.

For the age check of 18 years I have override the Validate.php. Create override/classes/Validate.php

<?php

// User has to have a minimum age of 18 years

class Validate extends ValidateCore
{

 public static function isBirthDate($date, $format = 'Y-m-d')
    {
        if (empty($date) || $date == '0000-00-00') {
            return false;
        }

        $d = DateTime::createFromFormat($format, $date);
        if (!empty(DateTime::getLastErrors()['warning_count']) || false === $d) {
            return false;
        }
        
        //adding 18 years to the birth date
        $d->add(new DateInterval("P18Y"));

        return $d->getTimestamp() < time();
    }
}

After that change the text of the error message (field validation) to something like: "Format has to be: 1970/20/04 (minimum age is 18 years)"

  • Like 2
Link to comment
Share on other sites

  • 5 weeks later...

Dear @ByteEmotions

I had a lot of trouble with my site in the last few weeks so I moved this task backwards. Thx for the reminder. I will come back for shure because we need this function and I don't like to lose it after each update.

Link to comment
Share on other sites

  • 2 years later...

Hi All,

I am using version 1.7.8.6

I changed classes/form/CustomerFormatter.php line 212 set required true to require birthday.

        if ($this->ask_for_birthdate) {
            $format['birthday'] = (new FormField())
                ->setName('birthday')
                ->setType('text')
                ->setLabel(
                    $this->translator->trans(
                        'Birthdate',
                        [],
                        'Shop.Forms.Labels'
                    )
                )
                ->setRequired(true)
                ->addAvailableValue('placeholder', Tools::getDateFormat())

 

1. should i create a new CustomerFormatter.php and put it in the override/classes/form instead of modifying the root/classes/form/?

 

2.  I added the below code, however, if the age is younger than 18 it says "format should be 05/31/1970." If its 18 and older user can proceed.

How can i change the message to show you must be 18 years or older to proceed?

3. I like to add another condition if greater than 18 but younger than 60.

 

<?php
//User age verification 18 years and older
class Validate extends ValidateCore
{

public static function isBirthDate($date, $format = 'Y-m-d')
    {
        if (empty($date) || $date == '0000-00-00') {
            return true;
        }

        $d = DateTime::createFromFormat($format, $date);
        if (!empty(DateTime::getLastErrors()['warning_count']) || false === $d) {
            return false;
        }
        
        $d->add(new DateInterval("P18Y"));
        return $d->setTime(0, 0, 0)->getTimestamp() <= time();
    }
}

 

Thanks

-K

Edited by kyaj323 (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...