Orchard custom forms with unobtrusive client-side validation - forms

I'm using Orchard's 'Custom Forms' module to build a form by adding a bunch of fields to a content type called 'Booking Form'. It works just fine - I can submit the form and view submissions in the admin pages.
I want to use client-side validation on the fields. I've succeeded in getting jQuery unobtrusive validation to work on my own manually-built forms (using #Html.EditorFor() a property on a viewmodel, and decorating the field [Required]). However, I can't get it to work in this context.
I am guessing this is because the property that's being used to render the input is not decorated with DataAnnotations attributes:
namespace Orchard.Core.Common.ViewModels {
public class TextFieldDriverViewModel {
public TextField Field { get; set; }
public string Text { get; set; }
public TextFieldSettings Settings { get; set; }
}
}
I think the 'Text' field would need something like [Required] before it for unobtrusive validation to work properly.
Is there any way to achieve unobtrusive validation for fields like this? Obviously decorating the property is undesirable, as then even fields NOT marked as required in the CMS would become required.

Related

What work is needed to pre-populate input form values using the URL/query strings?

I am creating a web portal. I have a button on the portal which redirects the user to a different website (owned by us but completely different website). I would like to pre-fill some of the input fields on a form with data passed from the portal (First name, last name and email).
I understand you can use query strings in the URL to populate fields on a form. I have tried using mywebsite.com/page/?FirstName=Jane but it is not populating the input fields. This method seems ideal but I assume there may be some work to do on the separate website to get this to work.
Both my portal and the other website is built with asp.net mvc.
Can anyone confirm if can get away with passing parameters without modifying the different website?
It's been a while since I've done mvc.
if you want to link with mywebsite.com/page/?FirstName=Jane
Assuming you have control over the page you're linking to, the page that you link to needs to
1. The model needs to have firstName in it
public class SomeModel
{
[Required]
[Display(Name = "First name")]
public string FirstName { get; set; }
}
The controller needs to populate it from that query string
public ActionResult Index(SomeModel model)
{
return View(model);
}
The view needs to use it
#model Site.Models.SomeModel
#Html.TextBoxFor(m => m.FirstName, new { #class = "form-control" })

Devexpress XAF. How apply autocomplete to field of string?

I have class
[DomainComponent]
[DefaultClassOptions]
public interface IEmployee
{
[XafDisplayName("Место рождения")]
String PlaceOfBirth { get; set; }
}
When the user try to fill property "PlaceOfBirth" in Web GUI, I want to help him with standart autocomplete (like http://jqueryui.com/autocomplete/ or same). Datasource of autocomplte is all value of "PlaceOfBirth" (distinct).
I already try many cases but not success. Is it possible?
PS: For now I use this advice https://documentation.devexpress.com/#Xaf/CustomDocument3116 but it not good for large collection :(
Look at DevExpress support center there are solutions available.
http://www.devexpress.com/Support/Center/Example/Details/E3325.
http://www.devexpress.com/Support/Center/Question/Details/K18561

What alternatives are there to Yii active form?

I wish to build a form that does not create/update any active records. An example of such a form would be a search filter using check boxes for the user to select which categories to apply to the search.
Everything I read about forms in Yii is centred around the CActiveForm class. What is the Yii way to build forms that don't use active records?
If you want to embrace Yii's convenient form handling, you should use CActiveForm. It does not require CActiveRecord. But it always requires a model for your form data - which is a good thing, because this will keep the validation rules out of your view files. Instead of a CActiveRecord you could also build a simple model class from CFormModel.
class SomeForm extends CFormModel
{
public $name;
public $email;
public function rules()
{
return array(
array('name,email','required'),
array('email','email'),
);
}
}
Check the docs of the CHtml class. While it contains all the active* method, there are plain form methods in there as well, such as checkBox() or checkBoxList() that sound like what you're looking for.

ASP MVC 2 Pattern for implementing CanExecute style commands

I have come from WPF (MVVM) background and trying to shift to MVC 2. Is there any pattern in MVC2 where you can use Commanding/Command buttons like <input> which you use to submit the form so that you can hide/disable when you try to Render the View.
In MVVM world, your commands could implement ICommand interface, and it had CanExecute method which was quite useful. I was wondering if there is anything similar in ASP MVC 2 ?
The only way I can think of, is to do it in the View, so that I can check the flag on ViewModel (CanSave) and depending on that show/hide the <input> tag.
Basically I want to have 2 version of the website running, one in Read-Only mode and the other Editing mode.
Let me know if you need any clarification.
ASP.NET MVC does not feature the notion of 'controls', as are found in classic ASP.NET and WPF. The foundational blocks of ASP.NET MVC are HTML elements, like <input>, <button> et cetera. Naturally, these don't offer the functionality you're looking for (i.e. Implementation of the ICommand Interface).
The scenario that you're looking at (i.e. two modes of your form) can be (and arguably should be) dealt with at the View level. You're already facing the right direction: have a 'CanSave' property on your Model, and use this in the View to determine what is generated.
Example:
<% if (Model.CanSave)
{ %>
<p>First Name: <%= Html.TextBox("firstname", Model.firstname) %> </p>
<% }
else
{ %>
<p>First Name: <%=Model.firstname %></p>
<% } %>
You'll probably want to check out the DisplayTemplates and EditorTemplates... very handy for this scenario. Brad Wilson does a good job here.
It will help you move to this:
<%= (Model.CanSave) ? Html.EditorFor(x => x.firstname) : Html.DisplayFor(x => x.firstname) %>
...which makes your View clean and nice.
If you can't get MVC to do this it's relatively worth it to hand-code something like this vb-style pseudocode. This involves...
Subclassing your controls.
Not as much of a pain as it sounds, but, it is a medium sized one. Therefore it is only appropriate for medium-sized to large apps. But worth it for them.
Interface BaseUIControl
Property Enabled as Boolean
Property Visible as Boolean
Property Name as String
Property EntireStateAsXML as string ' You can use this to do EVERYTHING!
Interface UserActionItem
Event Clicked(sender as UserActionItem ... don't pass anything from UI namespaces!)
Class MyButton (or link, etc.) Implement BaseUIControl, UserActionItem Inherits UI.Button
How does this help? You've basically replaced the missing functionality. Your Controller (or even application layer) can be aware of the UI components by interface only, so they won't have to see the UI types.
more...
You can leverage this philosophy to control everything. This has saved me thousands of hours of monkey code.
Interface TextControl
Property Value as text
Interface CheckControl
Property Checked as boolean
The above two are Pretty basic - you inherit MyCheckBox and MyTextBox from the UI versions and implement the appropriate.
Of course you could set up common code to loop thru all controls and auto-validate (or loop thru and get each one's XML to autobind the whole form).
Interface ValidationBase
Property Required as Boolean
If you have a text or numeric-only mask or restricitons built into 2 subclasses...
Interface ValidationNumeric
Property MinVal, MaxVal as double
Interface ValidationText
Property MinLen, MaxLen as double
No, it won't go to the database for you. But this sweeps a ton of crud under the rug.
You can even set these property values in the UI designer - yes, putting BL in bed with UI, BUT, if you only have one UI for the BL, actually works very well.
Now image a UI with a mix of things like listbox/multiselect, double-list picker controls, checked listbox, a groupbox of option buttons/checkboxes ...
Interface Selector
property Items as list (of string)
property SelectedItems as list (of string)
Use what works on the UI - your generic routines can care less what they look like!! The subclassed UI pieces will just implement them to set/get the right values.
In addition ... we added 'validationEquation', ActivatesEquation (gray/ungray), SetValueTriggerEquation (if true, set value to SetValueEquation, otherwise, leave alone), which allowed controls to be set to simple values from other items (basically getting the values from bound objects as if using reflection) via Pascal Gayane's Expression Evaluator (it reads .net types!)
You can also subclass the main form, have it recurse thru all it's subcontrols, put together the XML's for the whole screen, and serialize it like that. You can have your own classes implement these in the non-UI layers and use it to totally (de/)serialize the UI state, and use them to read the UI too, if they relate to a business object, to map to it.
It's unbelievable how much this simplifies a complex app. We have one with 1200+ data entry panels (... pages... ours is a thickclient app) that will fill out 250 different paper forms at 250K LOC. The form definitions contain the 'name' of each control and this is pulled from the XML generated from the screens. We probably saved 500K LOC as many of the screens have no code behind them or only trivial code; all the databinding, validation, etc. is handled by common routines that reference the interfaces.
Like I say, this only works for a big app. Spend at least 2-3 weeks developing 90% of the functionality, though; probably another month throughout the 2 years dev maturing it. I am guessing your apps is big if you're caring about ICommand and its conveniences. I would put the payback at 15-20 moderately complex pages.
If I'm understanding the question correctly, you could write a ControllerCommand class to encapsulate this. Something like this:
public class ControllerCommand
{
public string Action { get; set; }
public string Controller { get; set; }
public object RouteValues { get; set; }
public bool IsEnabled { get; set; }
}
Your Details viewmodel might use it like this:
public class DetailsModel
{
public guid Id { get; set;}
// some other viewmodel properties
public ControllerCommand Edit { get; set; }
}
You could write extension methods on HtmlHelper to replace the built-in ones:
public MvcHtmlString CommandLink(this HtmlHelper html, string linkText, ControllerCommand command, object htmlAttributes)
{
if (command.IsEnabled)
{
return html.ActionLink(linkText, command.Action, command.Controller, command.RouteValues, htmlAttributes);
}
else
{
return MvcHtmlString.Create(linkText);
// perhaps return <span class="disabled-command">linkText</span>
}
}
One of the ways I have found is to use Filter attributes which you can put in your Actions, but that only handles CanExecute on the server side.
For the GUI side, couldnt find better way than putting If statements to check if the user is Priviliged to run particular action (i.e. Edit/Delete buttons)

Make form submit with addition parameters. ASP.NET MVC

I would like to submit form by click on the button 'Submit'. All ok, but I have addition checkboxes without binding to model. This checkboxes were added dynamically. How to send form to client with these checkboxes and grab data on server.
[HttpPost]
public ActionResult Add(PremiumSlots PremiumSlots, FormCollection collection)
{
string chkbx = collection["yes-no"];
// "yes-no" name of checkbox (Should be same for all checkboxes)
// you will get comma separated values, perform split operation on string and use individualy
}