Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to validate sub document field? #139

Open
sagarguhe opened this issue Jan 10, 2017 · 3 comments
Open

How to validate sub document field? #139

sagarguhe opened this issue Jan 10, 2017 · 3 comments

Comments

@sagarguhe
Copy link

sagarguhe commented Jan 10, 2017

How do I validate sub document field? for example if I have document like this:

{
  "name": "John",
  "email": "john.doe@example.com",
  "addresses": [
    {
      "street": "field required",
      "locality": "",
      "city": "",
      "state": "",
      "country": ""
    },
    {
      "street": "field required",
      "locality": "",
      "city": "",
      "state": "",
      "country": ""
    }
  ],
  "profile": {
    "profile_picture": "myavtar.jpg",
    "skills": "php,mongo,mysql",
    "years_exp": "field required"
  }
}

In the above document I need street from addresses and years_exp from profile must be filled by the user.

So how do i validate these fields?

@sokil
Copy link
Owner

sokil commented Jan 10, 2017

You need to implement Structure with validation rules, and validate this instance. Currently, embedded documents validates only on Document::set() or any method in Document that calls Structure::prepareToStore. Also it may be done manually before adding in any way to document.

Validating of embedded documents on call Document::isValid() is waiting to implement.

Samples at https://github.com/sokil/php-mongo/blob/master/README.md#validation-of-embedded-documents

@sagarguhe
Copy link
Author

I found a way to validate a profile.years_exp (<--- so you have to write exactly like that) but not able to validate addresses.n.street where n stands for index of an element.

@sokil
Copy link
Owner

sokil commented Jan 10, 2017

yes. If emdemmed document can be accessed directly, validation may be configured in document. To validate list of embedded documents you need to have ebbedded document class, extended from Structure. Currently this is the only way.

@sokil sokil added this to the Document schema milestone Feb 11, 2019
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

No branches or pull requests

2 participants