Jump to content

I can't change EAN13 field's validation


Recommended Posts

I need the product's EAN13 fields to be validated exactly as the "Reference" fields.

  1. First thing I've tried is changing the validate function and size from isEan13 to isReference in Product class:
    'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 64),

    It did not work.
     

  2. Then I've tried to change the isEan13() function return value to the same preg_match that isReference() has in Validate class, since isEan13 is being called from different files as well:
     

    public static function isEan13($ean13) {
    	error_log('Validating EAN');
    	return !$ean13 || preg_match(Tools::cleanNonUnicodeSupport('/^[^<>;={}]*$/u'), $ean13);
    }

    I've actually even added an error_log debug output, which is not in the log at all, as if the function is not triggered at all.

I've also reset the cache after each change. The result is always "This value is not valid." with 400 Bad Request.

I double checked whether I am correctly editing default classes/overrides. I even tried to manually edit those classes in admin/autoupgrade/latest/classes folder, since they are there as well.

What could be the issue?

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

  • 2 weeks later...
  • 2 weeks later...
  • 6 months later...

Solved finally.

Turned out there were actually two rounds of validation. Validation is isEan13() function in Validate.php didn't even run until the first validation round is passed. 

The first validation is defined in src/PrestaShopBundle/Form/Admin/Product/ProductOptions.php. There, I needed to remove the 'constraints' argument for EAN13's field:

->add('ean13', FormType\TextType::class, [
  'required' => false,
  'error_bubbling' => true,
  'label' => $this->translator->trans('EAN-13 or JAN barcode', [], 'Admin.Catalog.Feature'),
  /*'constraints' => [
  	new Assert\Regex('/^[0-9]{0,13}$/'),
  ],*/
  'empty_data' => '',
])

 

After that, I could enter anything to the EAN13 field but it did go through validation defined in Validate.php. Since I wanted to be able to enter anything, simple is_string() works fine for me:

public static function isEan13($ean13) {
	return is_string($ean13);
}

 

Works well now.

  • Like 1
Link to comment
Share on other sites

  • 2 years later...

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...