Constraint Violation Exception when trying to add node property

Avatar

Avatar

ariellea4070516

Avatar

ariellea4070516

ariellea4070516

21-02-2019

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>

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Gaurav-Behl

MVP

Total Posts

1.1K

Likes

226

Correct Answer

281

Avatar

Gaurav-Behl

MVP

Total Posts

1.1K

Likes

226

Correct Answer

281
Gaurav-Behl
MVP

21-02-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

Answers (5)

Answers (5)

Avatar

Avatar

сергейд53065179

Avatar

сергейд53065179

сергейд53065179

22-02-2019

Avatar

Avatar

ariellea4070516

Avatar

ariellea4070516

ariellea4070516

22-02-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!

Avatar

Avatar

Prince_Shivhare

Avatar

Prince_Shivhare

Prince_Shivhare

21-02-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.

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

21-02-2019

Or by form submission, do you mean component dialog?

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

21-02-2019

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