odolbeau / phone-number-bundle

Integrates libphonenumber into your Symfony application
MIT License
217 stars 43 forks source link

Make constraint repeatable #146

Closed AlexOstrovsky closed 11 months ago

AlexOstrovsky commented 1 year ago

Would it be possible to make PhoneNumber attribute repeatable?

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class PhoneNumber extends Constraint
{
}
Nek- commented 1 year ago

Hello, thank you for your suggestion. Can you provide a use case for this?

AlexOstrovsky commented 1 year ago

Hi @Nek- yes my use case is I have one class where I have $phoneNumber for multiple countries and I want to validate phoneNumber per country based on groups which represent countryIso. Currently I am extending the attribute from this bundle and just adding IS_REPEATABLE to it.

Nek- commented 1 year ago

@AlexOstrovsky can you provide some code sample please?

AlexOstrovsky commented 1 year ago

@Nek-

#[PhoneNumber(format: PhoneNumberFormat::E164, type: [\Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::MOBILE], defaultRegion: CountryIso::LATVIA->value, message: 'GSM.INVALID', groups: [CountryIso::LATVIA->value])]
#[PhoneNumber(format: PhoneNumberFormat::E164, type: [\Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::MOBILE], defaultRegion: CountryIso::ESTONIA->value, message: 'GSM.INVALID', groups: [CountryIso::ESTONIA->value])]
#[PhoneNumber(format: PhoneNumberFormat::E164, type: [\Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber::MOBILE], defaultRegion: CountryIso::DENMARK->value, message: 'GSM.INVALID', groups: [CountryIso::DENMARK->value])] 
public ?PhoneNumberObject $gsm = null;

did you mean this or something else?

AlexOstrovsky commented 1 year ago

@Nek- any update on this?

Nek- commented 1 year ago

Hello @AlexOstrovsky , wouldn't it be more convenient to be able to specify many countries as valid?

AlexOstrovsky commented 1 year ago

@Nek- thats also a possibility but I don't see any harm in making it repeatable

Nek- commented 1 year ago

TBH didn't test it and I do not understand how there's not 2 failing no matter what. If the validator is called 3 times, then it will necessarily fail 2 times....

AlexOstrovsky commented 1 year ago

Because each repeated attribute has diffrent group.

Nek- commented 1 year ago

Oh ok, from what I understand, you are using groups as a workaround because there's no possibility to specify many countries in the constraints, or am I missing something?

AlexOstrovsky commented 1 year ago

My use case is like this: I have one API endpoint lets call it /api/formSubmit. This API endpoint is universal for all countries. But not all countries have all fields on frontend and phone number validation is diffreent per country. So I am using the groups (which represent country) to define what constraints should be validated in which country.

AlexOstrovsky commented 1 year ago

@Nek- so whats the conclusion? 😸

Nek- commented 1 year ago

@AlexOstrovsky I guess both should be done. You can open a PR for sure.

AlexOstrovsky commented 11 months ago

@Nek- I opened the PR: #167