Jump to content

[solved] Date of birth validation


Recommended Posts

if you want to set up birthday as a required field, go to the classes/Customer.php file

 

you've got there object definition with all fields, there is birthday field definition:

'birthday' =>	  array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),

 

add to this required=>true param

Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...

I have still problem with birthday required. I edit Customer.php and add required=>true. The field is now required but when i select date of birth on the page with authentification, system give me error "Birthday is required" and I can't save it. I use prestashop 1.5.4.1. Thanks for reply

 

Customer.php - 'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate', 'required' => true)

Link to comment
Share on other sites

  • 1 month later...

Some how i solved this issue by removing the above parameters that is

 

This

'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate', 'required' => true param ),

to This

'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate' ),

Again I mean to default and Made changes in classes/validate.php As

 

This

public static function isBirthDate($date)
    {
        if (empty($date) || $date == '0000-00-00')
            return true;

to This

public static function isBirthDate($date)
    {
        if (empty($date) || $date == '0000-00-00')
            return false;

This atlest gave an error as invalid date of birth but only when you fill all the required fields

 

Hope so i have explained you better, Might be helpfull to you.

 

Regards

 

Amod

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

  • 5 weeks later...

The version from amodkarmarkar worked for me!!!

 

THANKS A LOT!!!!!

 

using Prestashop 1.5.6.0

 

Another question...

Is it possible now to add the red "*" to the Birthday field?

 

you can do it in authentication.tpl file located in your theme directory (themes/YOUR_THEME/authentication.tpl)

<sup>*</sup>
Link to comment
Share on other sites

I tried that, but I can't find the right place for it...

Where do I have to place the <sup>*</sup> ?
 

<p class="select">
<span>{l s='Date of Birth'}</span>
<select id="days" name="days">
<option value="">-</option>
{foreach from=$days item=day}
<option value="{$day}" {if ($sl_day == $day)} selected="selected"{/if}>{$day}  </option>
{/foreach}
</select>
Link to comment
Share on other sites

here:

<p class="select">

<span>{l s='Date of Birth'} <sup>*</sup></span>

<select id="days" name="days">

<option value="">-</option>

{foreach from=$days item=day}

<option value="{$day}" {if ($sl_day == $day)} selected="selected"{/if}>{$day}  </option>

{/foreach}

</select>

Link to comment
Share on other sites

  • 3 months later...
Hello Everyone, I have found root cause of the date of birthday required error.

 

Here is complete flow of action.

 

function processSubmitAccount() in AuthController which is taking care of post data and validate them.

Then If we marked birthday field as required field then we need to update ObjectModel.php file. Because validation is checked based on parameters set in classes\Customer.php file and in Customer.php file there is birthday field on which are setting validation required=>true but there is no such fields in authentication.tpl file and in ObjectModel.php file there is function called validateController() which is checking for each required field data and produces error array.

 

So we have to update it for birthday field. In validateController() function you can find below code:

 



if (isset($data['required']) && $data['required'] && empty($value) && $value !== '0')
{
if (!$this->id || $field != 'passwd')
$errors[$field] = '<b>'.self::displayFieldName($field, get_class($this), $htmlentities).'</b> '.Tools::displayError('is required.');
}


 

Replace it with below code:

 



if(isset($data['required']) && $data['required'] && $field ==  'birthday')
{
         if (!@checkdate(Tools::getValue('months'), Tools::getValue('days'), Tools::getValue('years')) && !(Tools::getValue('months') == '' && Tools::getValue('days') == '' && Tools::getValue('years') == ''))
         {
               $errors[$field] = '<b>'.self::displayFieldName($field, get_class($this), $htmlentities).'</b> '.Tools::displayError('is required.');
         }
}
elseif (isset($data['required']) && $data['required'] && empty($value) && $value !== '0')
{
        if (!$this->id || $field != 'passwd')
               $errors[$field] = '<b>'.self::displayFieldName($field, get_class($this), $htmlentities).'</b> '.Tools::displayError('is required.');
}


 

Let me know if it works or not. I have checked with all the conditions and it works well.

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

 

Hello Everyone, I have found root cause of the date of birthday required error.

 

 

Thanks a lot, very helpful.

Adapted code for Presta 1.6:

      if(isset($data['required']) && $data['required'] && $field ==  'birthday')
      {
        if (!@checkdate(Tools::getValue('months'), Tools::getValue('days'), Tools::getValue('years')) && !(Tools::getValue('months') == '-' && Tools::getValue('days') == '-' && Tools::getValue('years') == '-'))
        {
          $errors[$field] = '<b>'.self::displayFieldName($field, get_class($this), $htmlentities).'</b> '.Tools::displayError('is required.');
        }
      }
      elseif (isset($data['required']) && $data['required'] && empty($value) && $value !== '0')
      {
        if (!$this->id || $field != 'passwd')        
        {
          $errors[$field] = '<b>'.self::displayFieldName($field, get_class($this), $htmlentities).'</b> '.Tools::displayError('is required.');          
        }
      }
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

 

you can do it in authentication.tpl file located in your theme directory (themes/YOUR_THEME/authentication.tpl)

<sup>*</sup>

hi vekia,

 

its not working for me.i added the required true in validate file but after then also there have issue.

when i select 31 feb then its showing birthday is required and birth date is invalid but if i select right date like 25feb then also it showing error birthday is required. 

 

Do you have nay solution for this.

Link to comment
Share on other sites

hi vekia,

 

its not working for me.i added the required true in validate file but after then also there have issue.

when i select 31 Feb then its showing birthday is required and birth date is invalid but if i select right date like 25 Feb then also it showing error birthday is required. 

 

Do you have nay solution for this.

Link to comment
Share on other sites

×
×
  • Create New...