How can you ensure that the base fieldtype validation (required etc) and error messages are still applied when validating $value via the validate($value) function??
Validate method in fieldtype class:
public function validate($value)
{
if(is_array($value) && $value['type'] != '')
{
$linkModel = $this->_valuetToModel($value);
$validated = $linkModel->validate();
return $validated ? true : $linkModel->getAllErrors();
}
return true;
}
Validate method of custom model that the fieldtype populates and returns to the template:
public function validate($attributes = null, $clearErrors = true)
{
switch($this->type)
{
case('email'):
if( !filter_var($this->value, FILTER_VALIDATE_EMAIL) )
{
$this->addError('value', Craft::t('Please enter a valid email address.'));
}
break;
case('tel'):
$regexp = '/^[0-9+\(\)#\.\s\/ext-]+$/';
if(!filter_var($this->value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $regexp))))
{
$this->addError('value', Craft::t('Please enter a valid telephone.'));
}
break;
case('custom'):
if(!filter_var($this->value, FILTER_VALIDATE_URL))
{
$this->addError('value', Craft::t('Please enter a valid url.'));
}
break;
case('entry'):
if($this->value == '')
{
$this->addError('value', Craft::t('Please select an entry.'));
}
break;
case('asset'):
if($this->value == '')
{
$this->addError('value', Craft::t('Please select an asset.'));
}
break;
case('category'):
if($this->value == '')
{
$this->addError('value', Craft::t('Please select a category.'));
}
break;
// default:
// Craft::dd(parent::validate($attributes, null));
// $this->addError('value', Craft::t('Please select a sdfasdfsd.'));
// break;
}
return !$this->hasErrors();
}