1

How can I be sure that at least one of FatherName or AncestorName Is filled?

  • How can I manage that in MyEntity.cs since I shouldn't use [Required] annotation for both of them?

    [Required]
    public string FatherName { get; set; }
    
    [Required]
    public string AncestorName { get; set; }
    
  • How can I manage it in UI?

Now I have a class for required inputs in UI. On each '.required-input' elements change, jQuery will check the elements with '.required-input' class are filled or not, and then the submit button will be enabled/disabled.

Also all required elements have a * sign.

$('#create').prop("disabled", true);
$(document).on('change', '.required-input', function() {
    var requiredInputsAreFilled = true;
    $('.required-input').each(function() {
        if (!$(this).val()) {
            requiredInputsAreFilled = false;
            return false;
        }
    });
    if (requiredInputsAreFilled) {
        $('#create').prop("disabled", false);
    } else {
        $('#create').prop("disabled", true);
    }
}).change();
dda
  • 6,030
  • 2
  • 25
  • 34
Elnaz
  • 2,854
  • 3
  • 29
  • 41
  • But even if you manage to do that validation on client side, your server side will fail because you have required on both of them. – CodingYoshi Nov 18 '17 at 06:43
  • @CodingYoshi, OK, exactly I want to know how can I manage it in server-side, too. Is there any solution in entity-framework? – Elnaz Nov 18 '17 at 06:45
  • You need `custom validator` to do it. – Ali Soltani Nov 18 '17 at 06:47
  • 3
    Please see https://stackoverflow.com/questions/2712511/data-annotations-for-validation-at-least-one-required-field. – Ali Soltani Nov 18 '17 at 06:48
  • https://blogs.msdn.microsoft.com/simonince/2011/02/04/conditional-validation-in-asp-net-mvc-3/ – CodingYoshi Nov 18 '17 at 06:48
  • 1
    Possible duplicate of [Data Annotations for validation, at least one required field?](https://stackoverflow.com/questions/2712511/data-annotations-for-validation-at-least-one-required-field) – VDWWD Nov 18 '17 at 10:51

0 Answers0