Customize property path in ConstraintViolation message - bean-validation

I would like to customize the property path of the ConstraintViolation error message.
Example:
class A{
#Valid
private B b;
}
class B{
#NotNull
private String s;
}
Validate A with B and a null s String.
property path: "B.s" message: "must not be null"
Is there a possibility to change the property path? I want to remove the sub bean "B"?

Related

I have one entity, i need to add property of entity type

**public class Ticket : BaseEntity
{
public TicketType TicketType { get; set; }
}
public class TicketType : AuxiliaryInfoBaseEntity
{
public string Description { get; set; }
}**
In Ticket Entity i need one property of TicketType and type of column should be byte in Ticket.
Resulting table will look like.Table-Ticket:column TicketType(tinyint,not null).
When i trying to migrate, i am getting this exception.
System.InvalidOperationException: 'The property 'Ticket.TicketType' is of type 'TicketType' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
Please help me with some soluntion thanks.

How to set property calue of bean as a value getting returned from method of another class?

I am working on one of the task in Hybris where i need to fetch the value of attribute from one class "a" to class "b" when my bean for class b is called. Below is what i tried. "i am on one page where i selected 10 products and on calling multi select event listener i got number of products in class a." but when bean is initialised for class b there i am getting null instead of value i need from class a.
Class A{
private string test;
public void setTest(String test)
{
this.test=test} //value is setting up on an event like
multi-select of products
public String getTest()
{
return test;} . //value is coming here
}
Class B{
private String attribute;
public void setAttribute(String attribute) //getting null
{
this.attribute=attribute}
public String getAttribute()
{
return attribute;}
}
<bean id="classB" class"B">
<property name="attribute">
<bean factory-bean="A" factory-method="getTest"></bean>
</bean>
You can define two separate beans for class A and class B as there is no dependency in both the classes as I understood from your problem statement.
After that, inject class A as property in class B will solve your issue.

Asp.Net Mvc - Setting Required attribute error message from property

I pass in a string through a constructor that i want to use as my error message in my model class like this:
public class ContactForm
{
public string NameError { get; set; }
public ContactForm(string nameError)
{
NameError = nameError;
}
[Required(ErrorMessage = NameError)]
public string Name { get; set; }
//More properties
}
Here i get the exception:
An object reference is required for the non-static field, method, or property 'UmbracoSport.Models.ContactForm.NameError.get' D:\Umbraco\Websites\UmbracoSport\UmbracoSport\Models\Custom\ContactForm.cs 19 34 UmbracoSport
If i make NameError static i get this error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type D:\Umbraco\Websites\UmbracoSport\UmbracoSport\Models\Custom\ContactForm.cs 19 34 UmbracoSport
Is this in any way possible ?

BeanValidation is not evaluating Constraints on Type-level

BeanValidation works on normal Validations which are placed at FIELD-level:
#Email
#Column(unique = true)
private String email;
However, placing a custom constraint on TYPE-Level:
#Entity
#UniqueEmail
public class UserAccount {
#Email
#Column(unique = true)
private String email;
...
}
with the annotation being:
#Constraint(validatedBy = {UniqueEmailValidator.class})
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface UniqueEmail { ... }
will not work. The UniqueEmailValidator is never called unless I programatically trigger it by calling .validate() on an injected javax.validation.Validator
Is this expected behaviour or can that be changed in a way that the BeanValidation will also evaluate custom constraints on type level.

DataMember attribute not found in field properties

I set to member of class attibute DataMember.
[DataMember]
internal protected string _FirstName="";
[DataMember]
public string FirstName { get { return _FirstName; }
internal protected set { _FirstName=(value!=null?value:""); } }
Next I want to search class members which have this attribute. But when I type:
Type.GetType("classType").GetProperty("FirstName").Attributes
I get null.
Any idea why this attribute was not found by reflection ?
You need to call GetCustomAttributes, not use the Attributes property.
var attributes = Type.GetType("ClassType").GetProperty("FirstName")
.GetCustomAttributes(typeof(DataMemberAttribute), true);

Resources