Sitecore WFFM MVC Custom Cross Control validation - validation

Is there a way in Sitecore WFFM MVC to implement a custom validation on a field which is conditionally required based on the selection of another field
"DynamicValidationBase":- This is a attributerized validation where this validation is applied on the field, which does have the access to the form details(other controls on the form).
"FormCustomValidator":- This never gets triggered as this is a "MVC Form".
Ex:
<style>
div{padding:10px 0;}
ul{
list-style:none;
}
</style>
<div>
<label for="Email">Email:</label>
<input type="text" name="Email"/>
</div>
<div>
<label for="Phone">Phone:</label>
<input type="text" name="Phone"/>
</div>
<div>
<label for="IPrefer">I Prefer:</label>
<ul>
<li>
<input type="radio" id="rdo_email" checked="checked" name="rdoPreferType"/>
<label for="rdo_email">Email</label>
</li>
<li>
<input type="radio" id="rdo_phone" name="rdoPreferType"/>
<label for="rdo_phone">Phone</label>
</li>
</ul>
</div>

RequiredIfPopulatedAttribute -
First, a custom attribute is needed to decorate the custom field. This feeds necessary data from the form to the client side to build the validation and validation error message.
Custom Field Class -
Next, create a custom field that inherits from the desired field but add the attribute above to the Value property.
Custom Field View -
Add any customizations to the presentation in this mvc view. Ensure that the model is the custom class above. Save this .cshtml file with the other WFFM views.
Javascript -
Add the following js validators:
$scw.validator.addMethod()
$scw.validator.unobtrusive.adapters.add()
Sitecore updates -
Create a FieldType referencing the custom class in the MVC Type field.
Reference this new custom FieldType as the Type field in the Form Designer.
The form field’s Parameters field contains a list of values that feed the custom field’s properties.
Above is an outline of the steps to accomplish this. For illustrations, please see: https://soyouwannasitecore.wordpress.com/2016/10/27/sitecore-wffm-required-if-outlined/

Related

How can I get items from a List element for dropdown menu for Spring forms?

I want to use a list element for drop-down select menu but it implemented everywhere with model. I use Spring I want the user to select items from a static list.
There are some use like
<form:select path="...." items=$(......)>
but it needs a model for items as I understood. Also I don't want to post anything I just need the value of the select menu and I will do everything with JS. How can I achieve this?
My List element:
private static String[] lang = {"en","fr","tr","es","de"};
I tried to use List like that but didn't work. I mean every form elements gone.
<form:form commandName="TranslateService">
<div class="form-group col-sm-6">
<label class="control-label" for="first-name">Target Languages<span class="required">*</span>
</label>
<div>
<form:select path="lang" items="${lang}"></form:select>
</div>
</div>
</form:form>
it's a hard way to solve your question. instead of this you can create a model for form.

Autocomplete datalist on Thymeleaf

I'm building a view with Thymeleaf templates, which contains a form that provides a way to bind the value of my inputs with the attributes passed in the model. The backend was developed using Spring 4.
The following snippet includes an autocomplete datalist with the data of the namelist object, which was added to the model as an attribute. Said namelist is an ArrayList of a class with the fields int id and String name.
<form th:action="#{/}" method="POST" th:object="${example}">
<div class="form-group">
<input list="names" class="form-control" id="nameinput" th:field="${example.num.id}"> </input>
<datalist id="names">
<option th:each="row : ${namelist}" th:value="${row.id}" th:label="${row.name}">
</datalist>
<button>submit</button>
</div>
</form>
The value of the selected option is already bound to example.num.id, which is the expected and desired behaviour. However, when loading the view on a web browser (tested on latest Firefox and Chrome), it is represented like this:
As you can see, the id's are showing. However, I'm trying to emulate the behaviour of a <select>; the value should not be shown, just the text or label.
Is there a better way to achieve this? Am I not using the datalist properly?

Modifying a model property of textarea in a View in ASP.NET Core

I have a View in ASP.NET Core application where I have a form:
<div class="form-group">
<label asp-for="#Model.Property" class="col-md-2 control-label">Description</label>
<div class="col-md-10">
<textarea asp-for="#Model.Property" class="form-control" rows="10" data-val-maxlength-max="1000"></textarea>
<span asp-validation-for="#Model.Property" class="text-danger" />
</div>
</div>
I want to make textarea empty and Not to have the value from Model. I don't see any value property on this textarea.
Is it possible to have textarea mapped to #Model.Property but Not display it?
I'll be using this textarea for POST only and I don't want to display anything for GET. But I want to have other properties fetched so that's why I need the model in GET.
I also tried to change the Model property in controller before sending, but this model is a part of a DBSet and if I modify in controller then the DBSet gets affected.
Javascript is another option but I want to avoid that.
I had a look at How to overwrite one model property in ASP.Net core but this is not convincing.
Thank you.

MVC3 and validating a mixture of Razor and jQuery plugins

I am using MVC3 and my view has a mixture of razor and raw html in my view e.g.
<div class="editor-field">
#Html.EditorFor(model => model.PAGE)
#Html.ValidationMessageFor(model => model.PAGE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PERIODICAL)
</div>
div class="editor-field">
<div class="editor-field">
<input type="text" id="periodicallist" name="tiperiodical" />
</div>
</div>
My JavaScript applies the tokenInput control plugin to the textbox and its all fine, but I wonder how I can validate this (i.e. for empty textbox/tokenInput), it needs to filled with at least 1 token. At the moment it's confusing having these two forms of creating a view, letting Razor constuct elements direct from the model, and then adding more complexity/cusomization by usin jQuery plugins etc.
Can anyone advise how I can validate jQuery plugins on posting back to the controller?
Cheers
If you are using unobtrusive validation then you all have to do is add the necessary HTML5 data attributes to the input elements from javascript and microsoft's unobtrusive javascript library will take care of the rest.
For ex. if you have dynamically injected a textbox into the form through javascript and you need to perform required validation then all you have to add the data-val and data-val-required attributes as below.
<input data-val="true"
data-val-required="No. of joinees is required"
name="NoOfJoinees"
type="text" />
EDIT:
To display the validation error message you have to inject a span near to the field,
<span class="field-validation-valid"
data-valmsg-for="NoOfJoinees"
data-valmsg-replace="true">
</span>

ASP.NET MVC3 Unobtrusive Validation and Hand Coded Element Question

I understand with built in function to render UI controls like
#Html.TextBoxFor(model => model.CustomerId)
#Html.ValidationMessageFor(model => model.CustomerId)
The rendered Html will look like
<input data-val="true" data-val-number="The field CustomerId must be a number." data-val-required="The CustomerId field is required." id="CustomerId" name="CustomerId" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="CustomerId" data-valmsg-replace="true"></span>
All the attributes inside input elements are used to support unobtrusive validation feature.
But for some reason I cannot render by using Html helper, but I have to hand code Html markup like
<select id="MyId" name="MyId" />
<%=Html.ValidationMessageFor(model => model.MyId)%>
In this case how can I still make unobtrusive validation word without hard code all these attributes in select element?
Thanks
Hardy
Unobtrusive validation relies completely on this attributes to determine which validation it will apply to the element, you will have to hard code them in your html is you dont want to use the helper, just make sure the name attribute of you select tag is equal to the property name it binds to.

Resources