I'm not able to create store views with code/url like en-sa because of the backend validation:
The store code may contain only letters (a-z), numbers (0-9) or underscore (_), and the first character must be a letter.
What is the best solution/workaround for allowing dashes in store views without affecting Magento integrity, modules and third party plugins? Is it ok to change it directly in database table store?
Please help.
Solved! Go to Solution.
Override storecodevalidator function
Defined below into global di.xml
<preference for="Magento\Store\Model\Validation\StoreCodeValidator"
type="Package\Module\Model\Validation\StoreCodeValidator"/>
Override isValid($value) function
public function isValid($value)
{
$validator = $this->regexValidatorFactory->create(['pattern' => '/^[a-z]+[a-z0-9_\-]*$/i']);
$validator->setMessage(
__(
'The store code may contain only letters (a-z), numbers (0-9) or underscore (_),'
. ' and the first character must be a letter.'
),
\Zend_Validate_Regex::NOT_MATCH
);
$result = $validator->isValid($value);
$this->_messages = $validator->getMessages();
return $result;
}
Then after run setup:di:compile
That's all !!
did you find a solution ?
Override storecodevalidator function
Defined below into global di.xml
<preference for="Magento\Store\Model\Validation\StoreCodeValidator"
type="Package\Module\Model\Validation\StoreCodeValidator"/>
Override isValid($value) function
public function isValid($value)
{
$validator = $this->regexValidatorFactory->create(['pattern' => '/^[a-z]+[a-z0-9_\-]*$/i']);
$validator->setMessage(
__(
'The store code may contain only letters (a-z), numbers (0-9) or underscore (_),'
. ' and the first character must be a letter.'
),
\Zend_Validate_Regex::NOT_MATCH
);
$result = $validator->isValid($value);
$this->_messages = $validator->getMessages();
return $result;
}
Then after run setup:di:compile
That's all !!
Google itself recommends that you use hyphens - instead of underscores _ in your URLs.
here is a reference from google web master central
Still not sure why Magento not allowing hyphens for store code.
You can try the given solution to allow hyphens to store code