Constraint Violation Exception when trying to add node property | Community
Skip to main content
ariellea4070516
New Participant
February 21, 2019
Solved

Constraint Violation Exception when trying to add node property

  • February 21, 2019
  • 6 replies
  • 2394 views

Hi, I have created a WCMUsePojo to handle form submissions that serve to update and/or create user profile properties found under

/home/users/../userID/profile

But am getting a Constraint Violation exception

javax.jcr.nodetype.ConstraintViolationException: No matching property definition: nodePropertyValue = thisissomestring

Even though this node is of type unstructured. I have posted my code below, any insight would be appreciated!

Thank you!

Session session = getRequest().getResourceResolver().adaptTo(Session.class);
UserManager userManager = getRequest().getResourceResolver().adaptTo(UserManager.class);
String sessionUserId = session.getUserID();
authorizablePath = userManager.getAuthorizable(sessionUserId).getPath();

String nodePropertyValue= getRequest().getParameter("nodePropertyValue");

Resource resource = getRequest().getResourceResolver().getResource(authorizablePath);
Node node = resource.adaptTo(Node.class);
Node profile = node.getNode("profile");

profile.setProperty("nodePropertyName", nodePropertyValue);
session.save();

The form is structured as such

<form method="POST" action="FormSubmission.java">

  <input type="text" name="nodePropertyValue" id="nodePropertyValue" />

  <button type="submit">Submit</button>

</form>

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Gaurav-Behl

"No matching property definition: nodePropertyValue = thisissomestring"

means that your code is trying to add "nodePropertyValue" to a node <authorizablePath> where it's not allowed.

Not sure why it says the property name is not "nodePropertyName" but "nodePropertyValue = thisissomestring". You should run the server in debug mode or dump logs to find more clues.

you're correct that "nt:unstructured" should've allowed it but I'm not sure if you as a logged-in user can modify your own (session-user's profile) profile at runtime? Did you try with "admin" user? Could you check if this user has "write" permissions (rep:write + jcr:write) to create the property on own profile (although that would've thrown a different exception in logs)

You may try with a service user and ensure that you have appropriate permissions on the /home/users... path

P.S. As Scott mentioned, you should use better options for this use case in production-ready code

6 replies

сергейд53065179
New Participant
February 23, 2019
ariellea4070516
New Participant
February 22, 2019

Thanks Gaurav, I was not planning on using the WCMUsePojo in production ready code but simply experimenting in a quick fashion to understand what logic should be used to process form entries into node property updates. Also, "nodePropertyName" and "Value" were my fillers to help explain what was previously there "email" and "example@gmail.com." I have moved my code into an AEM core bundle and will utilize JSP to return my param values.

Thanks!

Prince_Shivhare
New Participant
February 22, 2019

agree with above comments: please use service user, as you are trying to do some changes with node at runtime. ResourceResolverFactory rrFactory; Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "getResourceResolver"); ResourceResolver resourceResolver = rrFactory.getServiceResourceResolver(param); also if you are working on normal page form then you can sling models. search for helpx articles.

Gaurav-Behl
Gaurav-BehlAccepted solution
New Participant
February 22, 2019

"No matching property definition: nodePropertyValue = thisissomestring"

means that your code is trying to add "nodePropertyValue" to a node <authorizablePath> where it's not allowed.

Not sure why it says the property name is not "nodePropertyName" but "nodePropertyValue = thisissomestring". You should run the server in debug mode or dump logs to find more clues.

you're correct that "nt:unstructured" should've allowed it but I'm not sure if you as a logged-in user can modify your own (session-user's profile) profile at runtime? Did you try with "admin" user? Could you check if this user has "write" permissions (rep:write + jcr:write) to create the property on own profile (although that would've thrown a different exception in logs)

You may try with a service user and ensure that you have appropriate permissions on the /home/users... path

P.S. As Scott mentioned, you should use better options for this use case in production-ready code

smacdonald2008
New Participant
February 22, 2019

Or by form submission, do you mean component dialog?

smacdonald2008
New Participant
February 22, 2019

Using a WCMUsePojo not best choice to handle a form submission. Where did you get that idea?