Disable some [Required] field validators in a view - asp.net

ASP.NET MVC Core if that makes any difference
Is there a simple way to disable some field validators in a model or view under certain cases?
Most information on the web looks to be from the ASP.NET Forms era.
I could not find too many things to try out but, this looks to not do the trick.
<label asp-for="Files.PromoImage" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Files.PromoImage" class="form-control" />
#if (Model.Content.NewArticle)
{
<span asp-validation-for="Files.PromoImage" class="text-danger"></span>
}
</div>
<div class="col-md-8">
<div asp-validation-summary="ValidationSummary.All" class="text-danger"></div>
</div>

<input asp-for="Files.PromoImage"/> will generate validation span and data-required attribute if the PromoImage property is marked with the [Required] attribute. You can check the generated html in the browser.
The easiest way to achieve what you want is to remove the [Required] attribute in the model and have something like this in the view:
#if (Model.Content.NewArticle)
{
<input class="form-control" data-val="true"
data-val-required="The PromoImage field is required." name="Files.PromoImage"
placeholder="Promo Image" type="text" value="">
}
else
{
<input asp-for="Files.PromoImage" class="form-control"/>
}
Then again on the server, in your post action method, you have to do a manual validation for the PromoImage property.
Not so trivial but more elegant approach is to extend the MVC and jQuery validation with your own validation attribute and jQuery validator, e.g. [RequiredIf]. The API is slightly different in ASP.NET Core 1.0, but here is an example: Custom validation

Related

Asp.Net Validation add custom class

I am trying to add a class to my div if there is an error in the ModelState. Is there any way to achieve this?
This is how the Html looks like:
<div class="form-group">
<label class="control-label" asp-for="Vorname">Vorname</label>
<input type="text" class="form-control" asp-for="Vorname">
<span class="form-control-feedback" asp-validation-for="Vorname"></span>
</div>
Now I would like to add the class has-danger to the div arround it if an error occured on the Vorname property. Like this:
<div class="form-group has-danger">
<label class="control-label" asp-for="Vorname">Vorname</label>
<input type="text" class="form-control" asp-for="Vorname">
<span class="form-control-feedback" asp-validation-for="Vorname"></span>
</div>
Edit:
If your form is submitted to the server with no prior on-the-client JavaScript validation (old school! but it works), then you’ve got yourself the easiest of all fixed. You can just add these CSS classes whenever the page loads in the following fashion:
$(document).ready(function() {
$('.input-validation-error').parents('.form-group').addClass('has-danger');
});

Plone Search: get method to pass via URL to another site

I am using Plone 4.3 and the diazo bootstrap theme and want to use the site-search-form to pass the searchterms including two search-options to another site (catalog) via get method.
To achieve this I have modified the plone.searchbox template and changed some content of it:
<div id="portal-searchbox"
i18n:domain="plone"
tal:define="navigation_root_url view/navigation_root_url;
search_input_id view/search_input_id;">
<form id="searchGadget_form" method="get" enctype="application/x-www-form-urlencoded" accept-charset="utf-8" action="http://www.thecatalogadress.net/opensearch">
<div class="LSBox">
<input name="LOCATION"
type="hidden"
value="HAGENBIB" />
<input name="SG1.SG.HAGENBIB:SGHagenvk"
type="hidden"
value="on" />
<input name="QUERY_alAL"
type="text"
size="18"
value=""
title="Finden"
class="searchField" />
<input class="searchButton"
type="submit"
value="search"
i18n:attributes="value label_search;" />
<div class="LSResult" id="LSResult"><div class="LSShadow" id="LSShadow"></div></div>
</div>
</form>
</div>
By now the search term is passed, but the two input-options are not introduced in the URL thus the query in the catalog doesn't work.
The result I need as URL is http://www.thecatalogadress.net/opensearch?LOCATION=HAGENBIB&SG1.SG.HAGENBIB:SGHagenvk=on&QUERY_alAL=test
As I am new to plone I wanted to ask for a hint, where to look at or what to change in order to add the two input-options to the URL.
I think you're probably forgetting to add the proper ZCML directives for the override on the template take place.
Take a look at this tutorial on Overriding Viewlets.

Setting text input's value from ViewBag? ASP.NET MVC5

I am passing a variable ViewBag.crimeRef from my controller and am using it to display a message which works fine:
#if(ViewBag.crimeRef != null)
{
<div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
Sorry, there are no matching results for the crime reference <strong>#ViewBag.crimeRef</strong>. Please try searching again.
</div>
}
I figured I could use the same methodology to populate the input's value too, however I'm getting a CS1002: ; expected error:
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference"
#if(ViewBag.crimeRef != null) { value="#ViewBag.crimeRef" }>
Is there anyway I can populate the input's value using the ViewBag variable?
Many thanks
Answer 1 :-
<input type="text" class="form-control" maxlength="11" id="crimeRef"
name="crimeRef" placeholder="Crime reference"
value="#(ViewBag.crimeRef ?? String.Empty)" >
Answer 2 :-
I m including Nick's answer here because this is the accepted answer, so it will help other stackoverflow users to get answer of their question at one place.
Conditional statements are actually built into Razor as of MVC4 :
Conditional HTML Attributes using Razor MVC3
So simply using this:
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference"
value="#(ViewBag.crimeRef)">
Will only render the value attribute if ViewBag.crimeRef isn't null - perfect!
Conditional statements are actually built into Razor as of MVC4: Conditional HTML Attributes using Razor MVC3
So simply using this:
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference"
value="#(ViewBag.crimeRef)" }>
Will only render the value attribute if ViewBag.crimeRef isn't null - perfect!
Thanks to Andrei for the pointer!
The way you are currently trying to do this won't work, value isn't a server-side variable therefore you can't reference it like you are. You can, however, do something a lot simpler
value='#(ViewBag.crimeRef ?? "")'
Remove #ViewBag.crimeRef from quote and try this
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference" value=#(String.IsNullOrEmpty(ViewBag.crimeRef)?ViewBag.crimeRef:String.Empty) />
<input type="text" class="form-control" maxlength="11" id="crimeRef" value="#((ViewBag.crimeRef==null)?0:ViewBag.crimeRef)" name="crimeRef" placeholder="Crime reference">
In Controller action
ViewBag.crimeRef = null; or ViewBag.crimeRef = 1;

Registration form [FOSUserBundle]

I built my own registration form and when I try to register users it doesn't react. Everything with FOSUserBundle works good. Also when I use the default FOSUserBundle registration form it works and it saves all the data in my table. Should I use only the default form from FOSUserBundle or what?
Here is my form that I made:
<form action="{{ path('fos_user_registration_register') }}" method="post">
<p>
<input type="text" name="name"/>
</p>
<p>
<input type="text" name="surname" />
</p>
<p>
<input type="text" name="email" />
</p>
<p>
<input type="password" name="password"/>
</p>
<p>
<input type="submit" name="submit" value="Sign up"/>
</p>
</form>
Read documentation how you can override registration controller if you need extend functionality. Or you can just override standard form to add additional fields.
You need to pass the registration form from the controller ( if you are overriding the controller ) to the view in order to have the csrf token available. Otherwise the form will not validate.
In order to overwrite FOSUserBundle's standard registration form use bundle inheritance.
Please read FOSUserBundle's documentation chapter Overriding Default FOSUserBundle Forms.
If you just want to add "name" and "surname" to your user entity, add the properties and the mapping information and afterwards add the fields to the overriden form class. then override the registration form template provided by FOSUserBundle if you need further changes in there.

Is Possible to Set a Widget Constraint Dynamically?

I'm designing dynamic forms for grails and I was wondering if it was at possible to set the widget constraint dynamically?
Setting the widget constraint is not the way to attack this. I suppose the simplest method would be to choose the tag you'd like to display based upon some condition using an "if" tag in your view or template. For instance:
Say you have a Book
class Book {
String name
}
If you use Grails generate-view for this class, Grails will produce a template called _form that looks like the following:
<div class="fieldcontain ${hasErrors(bean: bookInstance, field: 'name', 'error')} ">
<label for="name">
<g:message code="book.name.label" default="Name" />
</label>
<g:textField name="name" value="${bookInstance?.name}"/>
</div>
You can test your condition in this form and change the view:
<div class="fieldcontain ${hasErrors(bean: bookInstance, field: 'name', 'error')} ">
<label for="name">
<g:message code="book.name.label" default="Name" />
</label>
<g:if test="$yourCondition">
<g:textField name="name" value="${bookInstance?.name}"/>
</g:if>
<g:else>
<g:textArea name="name" value="${bookInstance?.name}"/>
</g:else>
</div>
Now the view will display a textField or textArea field based upon $yourCondition.

Resources