Nibble

Archive for February, 2016

UI-O-Matic and setting default values 4

I’ve just released verison 1.7.0 which adds the ability to set default values by using the event model.

You can now attach to the event UIOMatic.Controllers.PetaPocoObjectController.ScaffoldingObject

Here is an example:

Say we have the following poco (with all of the UI-O-Matic stuff in place).

1 [UIOMatic("TestWithDate", "icon-users", "icon-user", RenderType = UIOMaticRenderType.List, 2 SortColumn = "TheDate", SortOrder = "desc")] 3 [TableName("TestWithDate")] 4 public class TestWithDate : IUIOMaticModel 5 { 6 public TestWithDate() 7 { 8 9 } 10 11 12 [UIOMaticIgnoreField] 13 [PrimaryKeyColumn(AutoIncrement = true)] 14 public int Id { get; set; } 15 16 [UIOMaticField("Firstname", "Enter your firstname")] 17 public string FirstName { get; set; } 18 19 [UIOMaticField("Lastname", "Enter your lastname")] 20 public string LastName { get; set; } 21 22 [UIOMaticField("TheDate", "Select a date")] 23 public DateTime TheDate { get; set; } 24 25 public IEnumerable<Exception> Validate() 26 { 27 28 return new List<Exception>(); 29 } 30 }

And we wish to set TheDate property to the current date time, you can now do that in the following way:

1 public class Startup : ApplicationEventHandler 2 { 3 protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 4 { 5 UIOMatic.Controllers.PetaPocoObjectController.ScaffoldingObject += PetaPocoObjectController_ScaffoldingObject; 6 } 7 8 private void PetaPocoObjectController_ScaffoldingObject(object sender, UIOMatic.ObjectEventArgs e) 9 { 10 if (e.Object.GetType() == typeof(TestWithDate)) 11 ((TestWithDate)e.Object).TheDate = DateTime.Now; 12 } 13 }

So by using the event model, subscribe to the ScaffoldingObject event, and the in the eventhandler check the type of object (since this is executed for each type), and then simply cast to the correct type and set the property(or multiple) you would like to have a default value for.

UI-O-Matic and one to many relations 1

Just had this question on the forum and makes sense to share this in a blog post so here it is

How can you handle one to many relations with UI-O-Matic? Think order and orderlines

If you take a look at the example project we have the following 2 classes

Dog

1 [UIOMatic(Dogs, icon-users, icon-user, RenderType = UIOMaticRenderType.List)] 2 [TableName(Dogs)] 3 public class Dog : IUIOMaticModel 4 { 5 public Dog() { } 6 7 [UIOMaticIgnoreField] 8 [PrimaryKeyColumn(AutoIncrement = true)] 9 public int Id { get; set; } 10 11 [UIOMaticNameField] 12 public string Name { get; set; } 13 14 [UIOMaticField(Is castrated, Has the dog been castrated)] 15 public bool IsCastrated { get; set; } 16 17 18 [UIOMaticField(Owner, Select the owner of the dog, View = dropdown, 19 Config = {’typeName’: ‘Example.Models.Person, Example’, ‘valueColumn’: ‘Id’, ’sortColumn’: ‘FirstName’, ‘textTemplate’ : ‘FirstName + \” \”+ LastName ‘})] 20 [UIOMaticIgnoreFromListView] 21 public int OwnerId { get; set; } 22 23 public override string ToString() 24 { 25 return Name; 26 } 27 28 public IEnumerable<Exception> Validate() 29 { 30 var exs = new List<Exception>(); 31 32 if (string.IsNullOrEmpty(Name)) 33 exs.Add(new Exception(Please provide a value for name)); 34 35 if (OwnerId == 0) 36 exs.Add(new Exception(Please select an owner)); 37 38 39 return exs; 40 } 41 }

As you see the dog has an OwnerId property that is linked to the Person class  (in the way described here http://www.nibble.be/?p=491 ) So we get a nice dropdown where we can select a Person (instead of having to type in the id)

Person

1 [UIOMatic(People, icon-users, icon-user,SortColumn=FirstName)] 2 [TableName(People)] 3 public class Person : IUIOMaticModel 4 { 5 public Person() { } 6 7 [UIOMaticIgnoreField] 8 [PrimaryKeyColumn(AutoIncrement = true)] 9 public int Id { get; set; } 10 11 [UIOMaticField(Firstname,Enter your firstname)] 12 public string FirstName { get; set; } 13 14 [UIOMaticField(Lastname, Enter your lastname)] 15 public string LastName { get; set; } 16 17 [UIOMaticField(Picture, Please select a picture,View =file)] 18 public string Picture { get; set; } 19 20 [Ignore] 21 [UIOMaticField(Dogs, “”, View =list, 22 Config = {’typeName’: ‘Example.Models.Dog, Example’, ‘foreignKeyColumn’ : ‘OwnerId’, ‘canEdit’ : false})] 23 public IEnumerable<Dog> Dogs { get; set; } 24 25 public override string ToString() 26 { 27 return FirstName + + LastName; 28 } 29 30 public IEnumerable<Exception> Validate() 31 { 32 var exs = new List<Exception>(); 33 34 if (string.IsNullOrEmpty(FirstName)) 35 exs.Add(new Exception(Please provide a value for first name)); 36 37 if (string.IsNullOrEmpty(LastName)) 38 exs.Add(new Exception(Please provide a value for last name)); 39 40 41 return exs; 42 } 43 }

On the person class you’ll see a Dogs property (that is set to Ignore since it isn’t an actuall column) making use of the list field view type (more on that in the docs)

The result

We get a nice overview off all dogs associated with the selected person

Screen Shot 2016-02-19 at 13.54.05