Trim String In Automapper - asp.net-mvc

Mapper.CreateMap<DataViewModel, DataSource>()
My Source Here Contains String Values Coming from user interface. I want to trim all the string before i map it to my destination object. Couldn't find a solution for this. Anyone Knows how this is to be done

This can be done using the ForMember method, like so:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()));
If you want to trim more than one property you can chain the .ForMember() method like this:
Mapper.CreateMap<DataViewModel, DataSource>()
.ForMember(x => x.YourString, opt => opt.MapFrom(y => y.YourString.Trim()))
.ForMember(x => x.YourString1, opt => opt.MapFrom(y => y.YourString1.Trim()))
.ForMember(x => x.YourString2, opt => opt.MapFrom(y => y.YourString2.Trim()));
Whilst this would get the job done, I would suggest performing input sanitisation elsewhere in your application as it doesn't belong in the mapping.

You can also use AddTransform
CreateMap<DataViewModel, DataSource>()
.AddTransform<string>(s => string.IsNullOrWhiteSpace(s) ? "" : s.Trim());

Related

EntityFramework - Port WithRequired to EF Core

I'm porting an application from MVC5/EF6 to MVC6/EF7, but having an issue with this particular line:
modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithRequired(e => e.Client).WillCascadeOnDelete(false);
Apparently the WillCascadeOnDelete is transformed to the OnDelete with restrict as parameter, but I can't find any documentation on the "WithRequired" part which has disappeared too in EF7. Has 'WithOne' the same impact or am I completely wrong here :
modelBuilder.Entity<Client>().HasMany(c => c.Payments).WithOne(e => e.Client).OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);
You are correct in both cases. Here is a detailed post... http://www.mikee.se/posts/migrating_from_ef6_to_ef_core
Typically these mappings would change from this in EF6.
x.Entity<Company>()
.HasMany(c => c.StatementOfEarnings)
.WithRequired(e => e.Company)
.WillCascadeOnDelete(false);
To this in EF Core
x.Entity<Company>()
.HasMany(c => c.StatementOfEarnings)
.WithOne(e => e.Company)
.OnDelete(DeleteBehavior.Restrict);

How do I use the 'When' method in ASP.NET MVC Fluent Validation?

I am trying to develop a fluent valiation rule where if my TitleId FK property is null, then the TitleOther text field becomes mandatory. I have tried several combinations and orders of fluent expressions, all to no avail.
This is what I have so far, if someone can please help me get this one When part correct, I would be most grateful, and a little more educated.
context.RulesFor(p => p.TitleId).Required(p => p.Message("Title is required."));
context.RulesFor(p => p.TitleOther)
.Required(p => p.Message("Please provide your other title."))
.Length(0, 50, c => c.Message("Other title may not exceed 50 characters")
.When(p => context.RulesFor(p => p.TitleId). *[what here?]*
I haven't used FluentValidation much, but from what I understand from the docs you would need to do
.When(p => p.TitleId == null)
instead of .When(p => context.RulesFor(p => p.TitleId). *[what here?]*
I have used When in my project in Fluent Validation. I have used for 1 condition that is compare password as like below:
RuleFor(u => u.Password).NotEmpty().WithMessage("Please Enter Password"); // Normal checked for non empty
When(u => !string.IsNullOrEmpty(u.Password) && !string.IsNullOrEmpty(u.ComfirmPassword), () =>
{
RuleFor(u => u.ComfirmPassword).Equal(x => x.Password).WithMessage("Password does not match");
}); // For compare password
Hope it helps you.
Thanks.

Accepting single dynamic, but unused, element in route

I want to accept URLs for my objects that contain an element that is not used, but which would be nice for SEO reasons. For example, I want to accept an url like:
http://localhost:3000/people/USA/123-joe-schmoe
.. where the "/USA" bit has no significance at all, other than signaling to users and search engines that this person resides in the USA.
I've tried setting up my rule using globbing, as well as for example doing something like
match "people/:whatever/:id" => "people#show", :constraints => {:id => /\d+-.*/}, :as => "person"
But this results in an attempt to route to :controller => people, :action => "show", :whatever => #<Person id: 123 ...> which fails.
Is it possible to have the routing ignore the :whatever part and pass in my :id in stead?
You can try a widcard match instead
match "people/*/:id => "doctors#show", :constraints=> {:id=>/\d+=.*/}, :as=>"person"

AutoMapper - MapFrom gives me the following error: expression tree cannot contain assignment operator

I've been using AutoMapper successfully for a while now. However, when I use MapFrom() in the CreateMap calls, I get the above error.
I have upgraded to version 2, and it seems the registries have been broken.
CreateMap<EmailInbound, EmailMessageModel>()
.ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.Subject = (string.IsNullOrEmpty(src.Subject) ? "No Subject" : src.Subject)));
Any ideas what has broken here?
You are trying to do an assignment in lambda expression instead of returning a value. Here's the fixed version.
CreateMap<EmailInbound, EmailMessageModel>()
.ForMember(dest => dest.Subject, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.Subject)
? "No Subject"
: src.Subject));

Symfony form's i18n and add_empty

I have this widget:
$this->setWidget('slug', new sfWidgetFormDoctrineChoice(array('model'
=> 'MyTable', 'method' => 'myMethod', 'key_method' => 'myMethod',
'add_empty' => 'Select option')));
Ok, what should I do to translate the "Select option"?
I can't use the __() helper inside the form, and adding that string
into my XLIFF file doesn't translate it automatically.
If it can't be done, what workaround should I implement? I can't find
any way, and neither can't find any tip in the official documentation.
Thanks!
I think I have solved it:
$translated_text = $this->widgetSchema->getFormFormatter()->translate('String to translate');
You can use helper inside form, try this:
public function setup()
{
sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18n'));
$this->setWidget('slug', new sfWidgetFormDoctrineChoice(array('model'
=> 'MyTable', 'method' => 'myMethod', 'key_method' => 'myMethod',
'add_empty' => __('Select option')));
...
}

Resources