UniqueEntity validation for OneToMany related entities - validation

I have the following validations defined for ProductType, ProductTypeAttributes (OneToMany):
Bike\ProductBundle\Entity\ProductType:
properties:
name:
- NotBlank: ~
productTypeAttributes:
- Valid: ~
Bike\ProductBundle\Entity\ProductTypeAttribute:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [attribute, productType]
errorPath: attribute
message: 'Attributes must be different for the same product type.'
There is a unique key for ProductTypeAttributes: attribute,productType.
I am using embedded forms (collection type) for ProductTypeAttributes with the possibility to add/remove items. The validation seems to work only for the already existent records in the db, happening only if I am adding a new related entity which will trigger the unique key violation for a record.
The problem is the validation doesn't work when adding two completely new related entities, with the same attribute/productType. In this case I get the "duplicate entry" exception.
So the validation checks only through db records using default findBy method, but not against newly added records themselves for duplicates.
Any way to overcome this?

Yes, but not through the default UniqueEntity validator. You'll have to manually create a second validator that works on the entire ProductType entity and validates that none of the new (unsaved) Attributes are the same.
Check out Symfony´s cookbook on how to make your own validator:
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

Related

How to check thrown errors during Sequelize validation execution

I have a sequelize model, which needs custom validation. That custom validation relies on some foreign keys being valid (valid as in valid uuid, not checked that they exist in db) on a record. There is also another validator appended that checks for mutual exclusivity of the foreign keys (only one can be present at the same time). After all this, I access the database in another validator to fetch data from another collection. I dont want this to happen if any of above validators fail because I dont want to access the database if I dont have to and I dont want to check if uuid is valid again because that is a job for one of previous validators.
The order is like so:
check if field contains valid type (uuid) - field type validator
check that field is not clashing with another field (mutually exclusive) - model-wide validator
fetch record from another collection to do further validation - model-wide validator
I want to check if previous validator has already thrown an error and not execute the next one if it has. Even better if I can check which one has thrown an error. Sequelize executes all validators even if error was already thrown. In documentation it says:
Any error messages collected are put in the validation result object
alongside the field validation errors, with keys named after the
failed validation method's key in the validate option object. Even
though there can only be one error message for each model validation
method at any one time, it is presented as a single string error in an
array, to maximize consistency with the field errors.
I tried several ways to access this "error" object but to no avail. Is there a way at all to know if error was already thrown by one of previous validators?

Is there a way how to recreate a field for a Dynamics CRM entity?

Our particular situation is that we have a DateOnly field and would like to change it to DateTime field supporting also the time portion. The operation is not allowed in UI and it's also stated in the documentation. Hence, I excepted deleting the field and recreating it with the new setting would work.
However when I try to create the field with the same name the Duplicate Field Name error is thrown. I've read the column actually still exists behind in the DB.
Of course, I could create a field with a new name but it would require changing all related workflows and code customizations.
Is there a way how to overcome this issue?
Deleting and recreating an attribute with the same name but different type should work - of course with the caveat that you have to remove all dependencies before deleting the attribute and recreate them with the new attribute.
The Duplicate Field Name error seems to indicate that the field still exists - perhaps the entity needs to be published after deleting.
You may also find the XrmToolbox tool Attribute Manager helpful.
It allows you to migrate an attribute and its data to a new attribute.

unique entity validation for add and edit forms

I use unique entity validation in username property of user entity,
when the user is going to be added that is ok,
but when the user is going to be edited, and username is not changed the unique entity validation impede this, because the same object already exist with that username,
how can I handle this?
PD:sorry for my poor english
Use validations groups. Link your UniqueEntity constraint to a validation group "new", then only link this group to your form when you are creating a User.

Different validation on a field depending on user selection

For example lets say I have the following validator:
Acme\BlogBundle\Entity\Person:
properties:
ID:
- NotBlank: ~
However the kind of validation for ID depends on the property IDType that is selected by a user. Which some IDS can be blank and some cannot. Not only that but other types of ID needs other types of validation. Is this possible to do? Or should I have one property for each ID?
The Callback validation constraint is very suitable in this case. It lets you define complex validation logic and add errors to any form field you see fit.

Custom Data Annotation Attribute using Metadata

My requirement is to create a custom data annotation attribute for my project. The requirement is to validate the min/ max length of a specific product from the database, which will be retrieved from the database using the ProductID. I have a dynamic page for each product where there are two fields called max length & min length. User inputs the values in these two fields which needs to be validated from the database. Product table contains all the products & one will be selected by passing a productId.
Please suggest some pointers to implement the above.
Thanks in advance.
This validation you can do only in the server side and not in client, so I see two options.
Remote Validation - You can use remote validation when you want to perform the validation and show the error message through ajax.
IValidatableObject - By implementing this interface in the class you can do both the validations at the same time and return all the validation error messages as a collection. By this way the validation will happen after the form is normally submitted.

Resources