Validator
in namespace DotVVM.Framework.Controls
Displays the asterisk or validation message for a specified field.
Usage & Scenarios
Displays the asterisk or validation message for a specified field.
Look at the Validation tutorial to see how the validation works.
Sample 1: Basic Validator
The Value
property specifies which property will be validated. In the viewmodel, the Text
property has the [Required]
attribute.
The content inside the Validator
control will be displayed when the field is not valid.
<p>
<dot:TextBox Text="{value: Text}" />
<dot:Validator Value="{value: Text}">This field is requied!</dot:Validator>
</p>
<dot:Button Text="SEND" Click="{command: Send()}" />
using System.ComponentModel.DataAnnotations;
namespace DotvvmWeb.Views.Docs.Controls.builtin.ValidationMessage.sample1
{
public class ViewModel
{
[Required]
public string Text { get; set; }
public void Send()
{
// do the job
}
}
}
Sample 2: CSS Classes for Invalid Values
The Validator
has a property InvalidCssClass
. If the field is not valid, the Validator control will get this CSS class.
You can also apply the Validator properties to other elements.
<p>
<dot:TextBox Text="{value: Text}" />
<dot:Validator Value="{value: Text}"
InvalidCssClass="validation-error">
*
</dot:Validator>
</p>
<div Validator.InvalidCssClass="validation-error-row"
Validator.Value="{value: Text2}"
style="padding: 10px; width: 200px">
<dot:TextBox Text="{value: Text2}" />
</div>
<dot:Button Text="SEND" Click="{command: Send()}" />
using System.ComponentModel.DataAnnotations;
namespace DotvvmWeb.Views.Docs.Controls.builtin.ValidationMessage.sample2
{
public class ViewModel
{
[Required]
public string Text { get; set; }
[Required]
public string Text2 { get; set; }
public void Send()
{
// do the job
}
}
}
Sample 3: Error Message Texts
There are the ShowErrorMessageText
and SetToolTipText
properties. If they are set, the error message generated by the [Required]
attribute
will be displayed in the Validator
control or set as a tool tip text.
<p>
<dot:TextBox Text="{value: Text}" />
<dot:Validator Value="{value: Text}"
ShowErrorMessageText="true"/>
</p>
<div Validator.Value="{value: Text2}"
Validator.SetToolTipText="true">
<dot:TextBox Text="{value: Text2}" />
</div>
<dot:Button Text="SEND" Click="{command: Send()}" />
using System.ComponentModel.DataAnnotations;
namespace DotvvmWeb.Views.Docs.Controls.builtin.ValidationMessage.sample3
{
public class ViewModel
{
[Required]
public string Text { get; set; }
[Required]
public string Text2 { get; set; }
public void Send()
{
// do the job
}
}
}
Properties
Name | Type | Description | Notes | Default Value | |
---|---|---|---|---|---|
Attributes | Dictionary<String,Object> |
attribute
static value
|
null | ||
ClientIDMode | ClientIDMode | Gets or sets the client ID generation algorithm. |
attribute
static value
|
Static | |
DataContext | Object | Gets or sets a data context for the control and its children. All value and command bindings are evaluated in context of this value. |
attribute
static value
bindable
|
null | |
HideWhenValid | Boolean | Gets or sets whether the control should be hidden even for valid values. |
attribute
static value
bindable
|
False | |
ID | String | Gets or sets the unique control ID. |
attribute
static value
|
null | |
InnerText | String | Gets or sets the inner text of the HTML element. |
attribute
static value
bindable
|
null | |
InvalidCssClass | String | Gets or sets the name of CSS class which is applied to the control when it is not valid. |
attribute
static value
|
null | |
SetToolTipText | Boolean | Gets or sets whether the title attribute should be set to the error message. |
attribute
static value
|
False | |
ShowErrorMessageText | Boolean | Gets or sets whether the error message text should be displayed. |
attribute
static value
|
False | |
Value | Object | Gets or sets a binding that points to the validated value. |
attribute
bindable
|
null | |
Visible | Boolean | Gets or sets whether the control is visible. |
attribute
bindable
|
True |
HTML produced by the control
If the Validator
control is used as a stand-alone control, it renders a <span>
.
<span data-bind="..."></span>
If the properties of Validator
are used on another element, it only renders a data-bind
attribute.
data-bind="..."