2014-10-14 16:10:01 -07:00
using Orchard.DynamicForms.Elements ;
using Orchard.Layouts.Framework.Display ;
using Orchard.Layouts.Framework.Drivers ;
2015-06-29 14:01:58 +03:00
using Orchard.Layouts.Helpers ;
2015-12-03 22:47:15 +01:00
using Orchard.Layouts.Services ;
2014-10-14 16:10:01 -07:00
using Orchard.Tokens ;
using DescribeContext = Orchard . Forms . Services . DescribeContext ;
namespace Orchard.DynamicForms.Drivers {
2015-02-19 22:14:55 +01:00
public class TextFieldElementDriver : FormsElementDriver < TextField > {
2014-10-14 16:10:01 -07:00
private readonly ITokenizer _tokenizer ;
2014-12-01 23:04:52 -08:00
2015-12-03 22:47:15 +01:00
public TextFieldElementDriver ( IFormsBasedElementServices formsServices , ITokenizer tokenizer ) : base ( formsServices ) {
2014-10-14 16:10:01 -07:00
_tokenizer = tokenizer ;
}
protected override EditorResult OnBuildEditor ( TextField element , ElementEditorContext context ) {
var autoLabelEditor = BuildForm ( context , "AutoLabel" ) ;
var textFieldEditor = BuildForm ( context , "TextField" ) ;
var textFieldValidation = BuildForm ( context , "TextFieldValidation" , "Validation:10" ) ;
return Editor ( context , autoLabelEditor , textFieldEditor , textFieldValidation ) ;
}
protected override void DescribeForm ( DescribeContext context ) {
context . Form ( "TextField" , factory = > {
var shape = ( dynamic ) factory ;
var form = shape . Fieldset (
Id : "TextField" ,
_Value : shape . Textbox (
Id : "Value" ,
Name : "Value" ,
Title : "Value" ,
2016-09-22 21:03:46 +01:00
Classes : new [ ] { "text" , "medium" } ,
2014-10-14 16:10:01 -07:00
Description : T ( "The value of this text field." ) ) ) ;
return form ;
} ) ;
context . Form ( "TextFieldValidation" , factory = > {
var shape = ( dynamic ) factory ;
var form = shape . Fieldset (
Id : "TextFieldValidation" ,
_IsRequired : shape . Checkbox (
Id : "IsRequired" ,
Name : "IsRequired" ,
Title : "Required" ,
Value : "true" ,
2014-11-21 17:47:32 -08:00
Description : T ( "Check to make this text field a required field." ) ) ,
2014-10-14 16:10:01 -07:00
_MinimumLength : shape . Textbox (
Id : "MinimumLength" ,
Name : "MinimumLength" ,
Title : "Minimum Length" ,
2015-06-29 14:01:58 +03:00
Classes : new [ ] { "text" , "medium" } ,
2014-10-14 16:10:01 -07:00
Description : T ( "The minimum length required." ) ) ,
_MaximumLength : shape . Textbox (
Id : "MaximumLength" ,
Name : "MaximumLength" ,
Title : "Maximum Length" ,
2015-06-29 14:01:58 +03:00
Classes : new [ ] { "text" , "medium" } ,
2014-10-14 16:10:01 -07:00
Description : T ( "The maximum length allowed." ) ) ,
2016-10-13 21:10:57 +02:00
_ValidationExpression : shape . Textbox (
Id : "ValidationExpression" ,
Name : "ValidationExpression" ,
Title : "Validation Expression" ,
Classes : new [ ] { "text" , "large" } ,
Description : T ( "The regular expression the text must match with." ) ) ,
2014-10-14 16:10:01 -07:00
_CustomValidationMessage : shape . Textbox (
Id : "CustomValidationMessage" ,
Name : "CustomValidationMessage" ,
Title : "Custom Validation Message" ,
Classes : new [ ] { "text" , "large" , "tokenized" } ,
Description : T ( "Optionally provide a custom validation message." ) ) ,
_ShowValidationMessage : shape . Checkbox (
Id : "ShowValidationMessage" ,
Name : "ShowValidationMessage" ,
Title : "Show Validation Message" ,
Value : "true" ,
Description : T ( "Autogenerate a validation message when a validation error occurs for the current field. Alternatively, to control the placement of the validation message you can use the ValidationMessage element instead." ) ) ) ;
return form ;
} ) ;
}
2015-04-04 17:22:17 +02:00
protected override void OnDisplaying ( TextField element , ElementDisplayingContext context ) {
2016-10-27 22:15:47 +02:00
var tokenData = context . GetTokenData ( ) ;
context . ElementShape . ProcessedName = _tokenizer . Replace ( element . Name , tokenData ) ;
context . ElementShape . ProcessedLabel = _tokenizer . Replace ( element . Label , tokenData , new ReplaceOptions { Encoding = ReplaceOptions . NoEncode } ) ;
// Allow the initial value to be tokenized.
// If a value was posted, use that value instead (without tokenizing it).
context . ElementShape . ProcessedValue = element . PostedValue ! = null ? element . PostedValue : _tokenizer . Replace ( element . RuntimeValue , tokenData , new ReplaceOptions { Encoding = ReplaceOptions . NoEncode } ) ;
2014-10-14 16:10:01 -07:00
}
}
}