0

I'm working on a migration which includes importing some phone numbers that will be stored in a Phone Number field. Some of the phone numbers are malformed, so I'm trying to validate the phone number in my import script. But I can't figure out how to validate a single field. If I perform the full validation, I get the expected error:

$number = '123456789123456789123456789'; // some invalid phone number
$numberData = [
    'region' => 'DE',
    'number' => $number,
];
$element->setFieldValue('phone_number_field', $numberData);
$element->validate();
var_dump($element->getErrors('phone_number_field'));

// output: 
// (
//     [0] => The string supplied is too long to be a phone number.
// )

However, when I try to validate only the phone number field it's not reporting any errors:

// neither of those options produce any errors
$element->validate('phone_number_field');
$element->validate(['phone_number_field']);
var_dump($element->getErrors());
// no errors

In this case, I'm not getting any errors. But the validation errors still appear when Elements::saveElement tries to save the element.

Is there something wrong with how I try to validate a single field? Is there another syntax to use for custom fields? I also tried setting the element's scenario to Element::SCENARIO_LIVE, but that doesn't change anything.

Looking forward to any insights. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.