// use ObjectModel::validateField on validaterules
This commit is contained in:
+30
-9
@@ -893,7 +893,7 @@ abstract class ObjectModelCore
|
||||
* @param int $id_lang
|
||||
* @return bool|string
|
||||
*/
|
||||
public function validateField($field, $value, $id_lang = null)
|
||||
public function validateField($field, $value, $id_lang = null, $skip = array(), $human_errors = false)
|
||||
{
|
||||
$this->cacheFieldsRequiredDatabase();
|
||||
$data = $this->def['fields'][$field];
|
||||
@@ -901,9 +901,12 @@ abstract class ObjectModelCore
|
||||
// Check if field is required
|
||||
$required_fields = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array();
|
||||
if (!$id_lang || $id_lang == Configuration::get('PS_LANG_DEFAULT'))
|
||||
if (!empty($data['required']) || in_array($field, $required_fields))
|
||||
if (!in_array('required', $skip) && (!empty($data['required']) || in_array($field, $required_fields)))
|
||||
if (Tools::isEmpty($value))
|
||||
return 'Property '.get_class($this).'->'.$field.' is empty';
|
||||
if ($human_errors)
|
||||
return sprintf(Tools::displayError('The %s field is required.'), $this->displayFieldName($field, get_class($this)));
|
||||
else
|
||||
return 'Property '.get_class($this).'->'.$field.' is empty';
|
||||
|
||||
// Default value
|
||||
if (!$value && !empty($data['default']))
|
||||
@@ -913,11 +916,11 @@ abstract class ObjectModelCore
|
||||
}
|
||||
|
||||
// Check field values
|
||||
if (!empty($data['values']) && is_array($data['values']) && !in_array($value, $data['values']))
|
||||
return 'Property '.get_class($this).'->'.$field.' has bad value (allowed values are: '.implode(', ', $data['values']).')';
|
||||
if (!in_array('values', $skip) && !empty($data['values']) && is_array($data['values']) && !in_array($value, $data['values']))
|
||||
return 'Property '.get_class($this).'->'.$field.' has bad value (allowed values are: '.implode(', ', $data['values']).')';
|
||||
|
||||
// Check field size
|
||||
if (!empty($data['size']))
|
||||
if (!in_array('size', $skip) && !empty($data['size']))
|
||||
{
|
||||
$size = $data['size'];
|
||||
if (!is_array($data['size']))
|
||||
@@ -925,11 +928,24 @@ abstract class ObjectModelCore
|
||||
|
||||
$length = Tools::strlen($value);
|
||||
if ($length < $size['min'] || $length > $size['max'])
|
||||
return 'Property '.get_class($this).'->'.$field.' length ('.$length.') must be between '.$size['min'].' and '.$size['max'];
|
||||
{
|
||||
if ($human_errors)
|
||||
{
|
||||
if (isset($data['lang']) && $data['lang'])
|
||||
{
|
||||
$language = new Language((int)$id_lang);
|
||||
return sprintf(Tools::displayError('The field %1$s (%2$s) is too long (%3$d chars max, html chars including).'), $this->displayFieldName($field, get_class($this)), $language->name, $size['max']);
|
||||
}
|
||||
else
|
||||
return sprintf(Tools::displayError('The %1$s field is too long (%2$d chars max).'), $this->displayFieldName($field, get_class($this)), $size['max']);
|
||||
}
|
||||
else
|
||||
return 'Property '.get_class($this).'->'.$field.' length ('.$length.') must be between '.$size['min'].' and '.$size['max'];
|
||||
}
|
||||
}
|
||||
|
||||
// Check field validator
|
||||
if (!empty($data['validate']))
|
||||
if (!in_array('validate', $skip) && !empty($data['validate']))
|
||||
{
|
||||
if (!method_exists('Validate', $data['validate']))
|
||||
throw new PrestaShopException('Validation function not found. '.$data['validate']);
|
||||
@@ -948,7 +964,12 @@ abstract class ObjectModelCore
|
||||
$res = false;
|
||||
}
|
||||
if (!$res)
|
||||
return 'Property '.get_class($this).'->'.$field.' is not valid';
|
||||
{
|
||||
if ($human_errors)
|
||||
return sprintf(Tools::displayError('The %s field is invalid.'), $this->displayFieldName($field, get_class($this)));
|
||||
else
|
||||
return 'Property '.get_class($this).'->'.$field.' is not valid';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user