This is a migrated thread and some comments may be shown as answers.

Automatically trim() all input fields?

17 Answers 888 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sunita
Top achievements
Rank 1
Sunita asked on 04 May 2011, 01:31 PM
Is there an elegant way to automatically trim the spaces from any user-input String fields before being saved to the database using the ORM.

17 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 05 May 2011, 09:19 AM
Hi Sunita,

One method I could think of is to extend the code generation templates (that's T4 templates) and have setters for properties of type string perform some "trimming magic" before actually setting the value of the backing field.

I am sorry I do not have an example of this, but I am sure it can be done this way.

Regards

Henrik
0
Sunita
Top achievements
Rank 1
answered on 05 May 2011, 12:59 PM

You mean something like this in the main class right?

[Telerik.OpenAccess.FieldAlias("someString")] 
       public string SomeString 
       
           get { return someString; } 
           set { this.someString= value.Trim(); } 
       }

This would need to be done for all the string values in all the tables. I was hoping for some magic setting  that would apply for all string values.
0
IT-Als
Top achievements
Rank 1
answered on 05 May 2011, 01:51 PM
Hi Sunita,

Just to clarify... are you using the "Classic Approach/ObjectScope" or the "Visual Designer/Context" approach?

I was referring to the T4 templates in the Visual Designer/Context approach...  The idea is much as you illustrated, but built in to the code generation template instead.

/Henrik
0
Sunita
Top achievements
Rank 1
answered on 05 May 2011, 02:23 PM
Yes we are using the "Classic Approach/ObjectScope".
0
IT-Als
Top achievements
Rank 1
answered on 05 May 2011, 02:31 PM
Hi Sunita,

Then there's no easy way to accomplish the trimming as far as I am aware of... other that hooking up to the life cycle events...

But that not easy either...  :-(  Sorry

Regards

Henrik

0
Sunita
Top achievements
Rank 1
answered on 06 May 2011, 12:52 PM
Hi Henrik,

When you say hooking up to the life cycle events, it still needs to be done for every property that is a string in every class right?

Sunita
0
IT-Als
Top achievements
Rank 1
answered on 06 May 2011, 01:46 PM
Hi Sunita,

Yes, I am afraid so....  unless some other Teleriker has the magic bullet... Anyone?

Regards

Henrik
0
Damyan Bogoev
Telerik team
answered on 09 May 2011, 04:57 PM
Hi Henrik,

I am afraid that there is not an out-of-the-box approach you could use in order to achieve that goal.
Actually you could implement triggers that should handle the trimming of the user-input string data on insert and update.
I am sorry for the inconvenience caused.

Regards,
Damyan Bogoev
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
Sunita
Top achievements
Rank 1
answered on 11 May 2011, 08:36 PM

Henrik,

As you suggested I am trying to hook up to the life cycle event (tracking_changing) .--> which says
 

Changing The changing event occurs when there is a change to an property that is just to be done. You can use this event to check the value that is being passed to certain property and change it if neccessary.


But I am not able to change the NewValue. How do I do that? An example would be great.

Thanks,
Sunita
0
IT-Als
Top achievements
Rank 1
answered on 12 May 2011, 06:25 AM
Hi Sunita,

Hooking into these events is not very effective, since you'll have to do it for each and every property of type string.

In the link I posted there's an example. In this example you gain access to the persistent object through the event args. With the persistent object at hand you can modify the property in question.

However, as Damyan already said, using the classic approach there's not really an effective / easy way of doing this.

If I should do it I would change the property setters (of type string) to do the trimming logic.

Regards

Henrik
0
Sunita
Top achievements
Rank 1
answered on 12 May 2011, 01:06 PM
Hi Henrik,

I tried this..
static void Tracking_Changing(object sender, ChangeEventArgs e)
       {
           if (e.NewValue is string)
           {
               if (e.NewValue != null)
               {
                   string passedvalue = e.NewValue.ToString();                  
                   if (e.OldValue != e.NewValue) 
                   {
                       log.InfoFormat("Setting property to {0}", passedvalue);
                        e.NewValue= e.NewValue.ToString().Trim(); // something like this --this does not work ---
                        
                   }
                   else
                   {
                       log.InfoFormat("Not setting property to {0}", passedvalue);
                   }                 
                     
               }
           }
       }

If that doesn't work then I might have to change the property setters.
Thanks,
Sunita
0
IT-Als
Top achievements
Rank 1
answered on 12 May 2011, 01:57 PM
Hi Sunita,

I am not sure you can actually change the value of e.NewValue... it is merely in the event args for logging purposes for example.

However a reference to the persistent object is also in the event args...  If you cast this, you have it at hand...  Cumbersome I know..
0
Damyan Bogoev
Telerik team
answered on 13 May 2011, 04:44 PM
Hi Sunita,

I am afraid that you are unable to modify the ChangeEventArgs.NewValue.
Actually you could modify the following template [OpenAccess_ORM_Directory\sdk\IDEIntegrations\templates\PCClassGeneration\cs\templates\classgen\fields\field\access\default.vm] in order to generate the correct code for trimming the new string values.
Hope that helps.

Best wishes,
Damyan Bogoev
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
IT-Als
Top achievements
Rank 1
answered on 16 May 2011, 08:15 AM
Hi Damyan,

Sunita is using the classic approach, thus no class generation.

Regards

Henrik
0
Damyan Bogoev
Telerik team
answered on 16 May 2011, 04:51 PM
Hello Henrik,

The [OpenAccess_ORM_Directory\sdk\IDEIntegrations\templates\PCClassGeneration\cs\templates\classgen\fields\field\access\default.vm] template is used from the class reverse mapping wizard to generate the class’s properties.

Kind regards,
Damyan Bogoev
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
Sunita
Top achievements
Rank 1
answered on 16 May 2011, 05:02 PM
Hi Damyan,

But this would be useful only for any newly generated classes/ new string fields right? I would still need to make to change manually for all the existing class / properties.

-Sunita
0
Damyan Bogoev
Telerik team
answered on 19 May 2011, 05:30 PM
Hi Henrik,

I am afraid that there is no other approach you could use in order to trim all the string values except those that I and Henrik proposed already.
I am sorry for the inconvenience caused.

Greetings,
Damyan Bogoev
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
Tags
General Discussions
Asked by
Sunita
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Sunita
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or