Skip to content

Add support for rich text fields and custom validation messages#164

Open
redjam13 wants to merge 5 commits intocontentful:masterfrom
redjam13:master
Open

Add support for rich text fields and custom validation messages#164
redjam13 wants to merge 5 commits intocontentful:masterfrom
redjam13:master

Conversation

@redjam13
Copy link

I took my first foray into using this SDK yesterday but quickly ran into issues. Namely:

  1. Cannot update any content type which contains a rich text field.

The NodesValidation::jsonSerialize method is hardcoded to return [], however an object is expected as evidenced by the response:

"details": {
      "errors": [
        {
          "name": "type",
          "details": "The type of \"validations[1]\" is incorrect, expected type: Object",
          "path": [
            "fields",
            1,
            "validations",
            1
          ],
          "type": "Object",
          "value": []
        }

I can see that the justification for this change was that the empty nodes object is always present when retrieving rich text content types. My proposed solution here is instead just to ignore the empty nodes object from validations when mapping the response.

  1. Rich text validations (nodes, enabledMarks and enabledNodeTypes) all suffer from the above problem - and presumably even if the intention of the existing code worked correctly, we would be overwriting any defined validations by hardcoding this to be empty?

I've made a first pass at implementing the correct validations for a rich text field.

  1. Custom validation messages on fields are overwritten (emptied) when updating a content type

  2. Fields which have a custom validation message - where the validation key comes after message alphanumerically e.g. size - will throw an exception (MessageValidation class not found)

The BaseField::mapValidation() method assumes the first key will correspond to size, range, etc (see array_keys($data)[0]) however this is not true where a message is included.

Ostensibly these last two bugs have always existed but have somehow gone unnoticed; I can only assume adding custom validation messages isn't a commonly-used feature 😄


I'm not yet completely au fait with the code here and I'm not precious about the implementation details of these fixes so I am of course happy for any of this to be adapted by others if necessary - or to make further changes upon feedback :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant