Remove/Add asterisk for requiredif when dependent property changes value

MariaMadalina

I have the following scenario: Multiple requiredif DataAnnotation attributes. I made a custom label helper that renders a "*" aside for the properties that are decorated if requiredif attribute. Now on the clientside I want to be able to hide/show if the dependent property changes value.

For a more precise example I have a model

public class Document {
public bool IsFake {get; set; }

[RequiredIf("IsFake",false,ValueComparison.IsEqual)]
public string Number{ get; set; }
}

Based on the label helper that I made I have the corresponding label for Number with a red * in the UI. When I change on the client side from the is fake to the is not fake radio button I want to hide the *.

I want to do be able to make this changes automatic and not make a script that for the known fields does that, as I have multiple cases like this.

I was thinking maybe I could write a javascript code that attaches dynamically a change event to the dependent property input and a handler that would show/hide the required mark.

MariaMadalina

This is pretty much the solution which I came up with. It is the best I could do, but still needs some customization meaning that the span that contains the "*" it's custom for my pages' DOM.

            <div class="editor-label">
                <label ui-documentLabel" for="Number">Number<span class="span-required">*</span></label>
            </div>
            <div class="editor-field">
                <input class="k-textbox ui-textbox" data-val="true" data-val-requiredif="The field Number is required!" data-val-requiredif-dependentproperty="IsFake" data-val-requiredif-dependentvalue="False" data-val-requiredif-operator="IsEqual" id="Number" name="Number" value="" type="text">
                <span class="field-validation-valid" data-valmsg-for="Number" data-valmsg-replace="true"></span>
            </div>

and the javascript

  var clietsidevalidation = function () { };

 clietsidevalidation.is = function (value1, operator, value2) {
//function that verifies that the comparison between value1 and value2 is true or not
};

 clietsidevalidation.handleRequirefIf = function (container) {



$('input', container).filter(function () {
    var attr = $(this).attr('data-val-requiredif');
    return (typeof attr !== 'undefined' && attr !== false);
}).each(function (index, item) {

    var params = new Array();
    params["operator"] = $(this).attr('data-val-requiredif-operator');
    params["dependentvalue"] = $(this).attr('data-val-requiredif-dependentvalue');
    params["dependentproperty"] = $(this).attr('data-val-requiredif-dependentproperty');

    var dependentProperty = clietsidevalidation.getName(this, params["dependentproperty"]);
    var dependentTestValue = params["dependentvalue"];
    var operator = params["operator"];
    var dependentPropertyElement = document.getElementsByName(dependentProperty);
    params["element"] = this;


    $(dependentPropertyElement).on('change', { params: params }, function (e) {
        var input = e.data.params.element;
        var inputName = $(input).attr("name");
        var $span = $('label[for=' + inputName + '] span', '.editor-label');

        var dependentProperty = this;
        var dependentTestValue = e.data.params["dependentvalue"];
        var operator = e.data.params["operator"];
        var dependentPropertyElement = $(this);

        var dependentValue = null;

        if (dependentPropertyElement.length > 1) {
            for (var index = 0; index != dependentPropertyElement.length; index++)
                if (dependentPropertyElement[index]["checked"]) {
                    dependentValue = dependentPropertyElement[index].value;
                    break;
                }

            if (dependentValue == null)
                dependentValue = false
        }
        else
            dependentValue = dependentPropertyElement[0].value;

        if (clietsidevalidation.is(dependentValue, operator, dependentTestValue) == false) {

            $span.addClass('hidden');

            var $form = $span.closest("form");
            // get validator object
            var $validator = $form.validate();

            var $errors = $form.find("span.field-validation-error[data-valmsg-for='" + inputName + "']");
            // trick unobtrusive to think the elements were succesfully validated
            // this removes the validation messages
            //custom form our solution as the validation messages are differently configured DOM
            $errors.each(function () { $validator.settings.success($('label',this)); })

            // clear errors from validation
            $validator.resetForm();
        }
        else {
            $span.removeClass('hidden')
        }
    });

});
 };

This is inspired by another post which I can't find right now, but when I do I will post a link.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ember computed property doesn't update when dependent property changes

From Dev

View is not getting notified when value of static Property Changes

From Dev

Data annotation to check dependent property is value

From Dev

UI not updating when a property changes

From Dev

MVVM Update Property A when Property B changes

From Dev

angularjs manual bootstrapping does not update property value when input changes through method call

From Dev

Can't update the static bound property's value when text box's text changes

From Dev

How often does as spring property get updated when value in application.properties changes?

From Dev

C++ object doesn't get value when connected QML property changes

From Dev

output text color changes when value changes

From Dev

Check if an object property changes its value

From Dev

Do function after a property value changes?

From Dev

Preserving Value When Orientation Changes

From Dev

$watch not called when model property changes

From Dev

Get notified when static property changes in wpf

From Dev

UserControl Fire Event when a property changes

From Dev

Change CSS property of element when time changes

From Dev

UserControl Fire Event when a property changes

From Dev

AngularJS : watching a particular property in an array of objects for changes in the property's value

From Dev

Field dependent property

From Dev

WPF binding update notification for nested property when parent property changes

From Dev

Dependency Property on a user control not updating the property when bound data changes

From Dev

Add row with value when value changes

From Dev

NullReferenceException when set value to a property

From Dev

jquery: get value and id of textbox when it changes

From Java

How to detect when an @Input() value changes in Angular?

From Dev

Update scope value when service data changes

From Dev

Prevent buttons moving position when value changes

From Dev

Angular js - Highlight dom when value changes

Related Related

  1. 1

    Ember computed property doesn't update when dependent property changes

  2. 2

    View is not getting notified when value of static Property Changes

  3. 3

    Data annotation to check dependent property is value

  4. 4

    UI not updating when a property changes

  5. 5

    MVVM Update Property A when Property B changes

  6. 6

    angularjs manual bootstrapping does not update property value when input changes through method call

  7. 7

    Can't update the static bound property's value when text box's text changes

  8. 8

    How often does as spring property get updated when value in application.properties changes?

  9. 9

    C++ object doesn't get value when connected QML property changes

  10. 10

    output text color changes when value changes

  11. 11

    Check if an object property changes its value

  12. 12

    Do function after a property value changes?

  13. 13

    Preserving Value When Orientation Changes

  14. 14

    $watch not called when model property changes

  15. 15

    Get notified when static property changes in wpf

  16. 16

    UserControl Fire Event when a property changes

  17. 17

    Change CSS property of element when time changes

  18. 18

    UserControl Fire Event when a property changes

  19. 19

    AngularJS : watching a particular property in an array of objects for changes in the property's value

  20. 20

    Field dependent property

  21. 21

    WPF binding update notification for nested property when parent property changes

  22. 22

    Dependency Property on a user control not updating the property when bound data changes

  23. 23

    Add row with value when value changes

  24. 24

    NullReferenceException when set value to a property

  25. 25

    jquery: get value and id of textbox when it changes

  26. 26

    How to detect when an @Input() value changes in Angular?

  27. 27

    Update scope value when service data changes

  28. 28

    Prevent buttons moving position when value changes

  29. 29

    Angular js - Highlight dom when value changes

HotTag

Archive