void IFormItem.AddControl <TValue>(AntInputComponentBase <TValue> control) { if (control.FieldIdentifier.Model == null) { throw new InvalidOperationException($"Please use @bind-Value in the control with generic type `{typeof(TValue)}`."); } this._control = control; CurrentEditContext.OnValidationStateChanged += (s, e) => { control.ValidationMessages = CurrentEditContext.GetValidationMessages(control.FieldIdentifier).ToArray(); this._isValid = !control.ValidationMessages.Any(); StateHasChanged(); }; _formValidationMessages = builder => { var i = 0; builder.OpenComponent <FormValidationMessage <TValue> >(i++); builder.AddAttribute(i++, "Control", control); builder.CloseComponent(); }; _propertyReflector = PropertyReflector.Create(control.ValueExpression); if (_propertyReflector.RequiredAttributes.Any()) { _labelCls = $"{_prefixCls}-required"; } }
void IFormItem.AddControl <TValue>(AntInputComponentBase <TValue> control) { this._control = control; CurrentEditContext.OnValidationStateChanged += (s, e) => { control.ValidationMessages = CurrentEditContext.GetValidationMessages(control.FieldIdentifier).ToArray(); this._isValid = !control.ValidationMessages.Any(); StateHasChanged(); }; RenderFragment <FormItem> formValidation = form => { return(builder => { var i = 0; builder.OpenComponent <FormValidationMessage <TValue> >(i++); builder.AddAttribute(i++, "Control", control); builder.CloseComponent(); }); }; _formValidationMessages = formValidation(this); if (control.FieldIdentifier.TryGetValidateProperty(out var propertyInfo)) { var requiredAttribute = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true); if (requiredAttribute.Length > 0) { _labelCls = $"{_prefixCls}-required"; } } }
void IFormItem.AddControl <TValue>(AntInputComponentBase <TValue> control) { if (_control != null) { return; } if (control.FieldIdentifier.Model == null) { throw new InvalidOperationException($"Please use @bind-Value (or @bind-Values for selected components) in the control with generic type `{typeof(TValue)}`."); } _fieldIdentifier = control.FieldIdentifier; this._control = control; if (Form.ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex)) { _fieldPropertyInfo = _fieldIdentifier.Model.GetType().GetProperty(_fieldIdentifier.FieldName); } _validationStateChangedHandler = (s, e) => { control.ValidationMessages = CurrentEditContext.GetValidationMessages(control.FieldIdentifier).Distinct().ToArray(); this._isValid = !control.ValidationMessages.Any(); StateHasChanged(); }; CurrentEditContext.OnValidationStateChanged += _validationStateChangedHandler; _formValidationMessages = builder => { var i = 0; builder.OpenComponent <FormValidationMessage <TValue> >(i++); builder.AddAttribute(i++, "Control", control); builder.CloseComponent(); }; if (control.ValueExpression is not null) { _propertyReflector = PropertyReflector.Create(control.ValueExpression); } else { _propertyReflector = PropertyReflector.Create(control.ValuesExpression); } if (_propertyReflector.RequiredAttribute != null) { _labelCls = $"{_prefixCls}-required"; } if (_propertyReflector.DisplayName != null) { Label ??= _propertyReflector.DisplayName; } }
void IFormItem.AddControl <TValue>(AntInputComponentBase <TValue> control) { if (control.FieldIdentifier.Model == null) { throw new InvalidOperationException($"Please use @bind-Value in the control with generic type `{typeof(TValue)}`."); } this._control = control; CurrentEditContext.OnValidationStateChanged += (s, e) => { control.ValidationMessages = CurrentEditContext.GetValidationMessages(control.FieldIdentifier).ToArray(); this._isValid = !control.ValidationMessages.Any(); StateHasChanged(); }; _formValidationMessages = builder => { var i = 0; builder.OpenComponent <FormValidationMessage <TValue> >(i++); builder.AddAttribute(i++, "Control", control); builder.CloseComponent(); }; if (control.FieldIdentifier.TryGetValidateProperty(out var propertyInfo)) { var requiredAttribute = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true); if (requiredAttribute.Length > 0) { _labelCls = $"{_prefixCls}-required"; } } else { throw new InvalidOperationException($"Unable to match property `{control.FieldIdentifier.FieldName}` in Model `{control.FieldIdentifier.Model.GetType()}`, please confirm that the Model contains this property."); } }
void IFormItem.AddControl <TValue>(AntInputComponentBase <TValue> control) { this._control = control; _formValidation = form => { return(builder => { var i = 0; builder.OpenComponent <FormValidationMessage <TValue> >(i++); builder.AddAttribute(i++, "For", control.ValueExpression); builder.AddAttribute(i++, "OnStateChange", EventCallback.Factory.Create <bool>(form, valid => form._isValid = valid)); builder.CloseComponent(); }); }; if (control.FieldIdentifier.TryGetValidateProperty(out var propertyInfo)) { var requiredAttribute = propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true); if (requiredAttribute.Length > 0) { _labelCls = $"{_prefixCls}-required"; } } }