question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How Can I valid parent from child and child from parent?

See original GitHub issue

Hello, First of all sorry for my poor english.

I want when RequiredDependentChild is different from null the RequiredDependentParent is required and when RequiredDependentParent is different from null the RequiredDependentChild is required.

This work with RequiredDependentParent different from null, so my RequiredDependentChild is required , thats ok

But I do not know how to do the inverse way.

I don’t want to put the RequiredDependentParent in ClassParent because in the real case there are many Properties.

How I can do that, would you help me please?

Thanks, this plugins is so good

My Models:

public class ClassParent
{       
    [Key]
    public int Id { get; set; }

    [Required]
    public string AlwaysRequired { get; set; }

    [RequiredIf("Child.RequiredDependentParent != null", ErrorMessage = "I am parent, You need fill me")]    
    public string RequiredDependentChild { get; set; }
          
    public ClassChild Child { get; set; }
}
public class ClassChild
{     
    [Key]
    public int Id { get; set; }
       
    [RequiredIf("")] //I don't know what to do here...
    public string RequiredDependentParent { get; set; }     
}

My fields

<div class="form-group">
            @Html.LabelFor(model => model.AlwaysRequired, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AlwaysRequired, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AlwaysRequired, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RequiredDependentChild, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RequiredDependentChild, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.RequiredDependentChild, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Child.RequiredDependentParent, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Child.RequiredDependentParent, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Child.RequiredDependentParent, "", new { @class = "text-danger" })
            </div>
        </div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jwaliszkocommented, Aug 6, 2017

Thank you for the sample.

Unfortunately out-of-the-box client-side support of back-references to the container (parent model) is not implemented. EA at client-side is pretty straightforward and designed to handle nested properties when explicitly provided to the view, without abilities to infer the context.

It’s a tricky stuff to achieve what you want. When using HTML helpers at the client-side, the more nested property, the more prefixed name. When reference to container is used in expression, what client-side script actually does, is looking for such a property within current context. It has no idea that it is a reference to a container.

In your case, the RequiredDependentChild property in the expression "Parent.RequiredDependentChild != null" makes client-side EA to be looking for the analogic input field within current model scope, i.e. Child.Parent.RequiredDependentChild. Child prefix corresponds with what HTML helper renders for nested property of your Child object.

The thing you could, do is providing the field EA is expecting to find, i.e.:

@Html.HiddenFor(m => m.Child.Parent.RequiredDependentChild) // hidden mock

Next, make the mocked field clone the behavior of the original (parent) one:

<script type="text/javascript">

    function Mimic(src, dest) {
        $(src).on(ea.settings.dependencyTriggers,
            function(e) {
                $(dest).val($(this).val());
                $(dest).trigger(e.type); // trigger the change for dependency validation
            });
    }

    $(document).ready(function () {
        Mimic('input[name="@Html.NameFor(m => m.RequiredDependentChild)"]',
              'input[name="@Html.NameFor(m => m.Child.Parent.RequiredDependentChild)"]');
    });

</script>

Please be aware that this a workaround for such specific cases like yours, when back-reference needs to be understood and properly handled by client-side EA logic.

0reactions
mauricioferrazscommented, Jun 2, 2017

I did not understand 100% of its explanation because my English rsrs, but the code about Mimic I understood and worked perfectly, tomorrow I will test in my real scenario and after that I give you a feed back.

Is it possible in the future to make this change to a new release without the need for this javascript code?

Thank you very much for your effort in helping me, I appreciated it. : D

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to validate child form from parent component in Vue
You need to set a value in the data of the parent component. Next you have to pass it as props to the...
Read more >
How to Validate a Child Object Based on Parent ...
Question: How do you validate a child object, when validation depends on one or more fields of a parent object? What about when...
Read more >
How to Prove a Parent-Child Relationship for Citizenship ...
To prove a mother-child relationship, it is enough to submit evidence of a biological relationship, preferably in the form of an acceptable birth...
Read more >
Validation: The Parenting Tool that Helps Kids Learn ...
Parents can try to validate their child anytime there is a strong emotional reaction to a situation or stimuli. Being present with your...
Read more >
Validation of a Parent - Child relationship
I'm working on a big, established project which uses a SQL database to store, process and validate the application's data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found