0

How to do trim in @bind

asked 2018-03-16 17:45:19 +0800

answer1 gravatar image answer1
1

I have to do trim on @bind('vm.state.email') . How to do trim in @bind?

delete flag offensive retag edit

Comments

Use a convertor.

chillworld ( 2018-03-17 02:23:02 +0800 )edit

@chillworld Thank You!:)

answer1 ( 2018-03-19 13:34:03 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-03-18 14:43:00 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

Oke, let's start to apologise for the short comment.
I looked at this on the phone, didn't have the time or resources to answer this correct so I pointed already to a possible solution.

Why doesn't this work.

Well, @bind is a shortcut for @load and @save together.
While @load just calls a getter, it's possible to do : @load('vm.state.email.trim()')
Look carefully at the trim(), yes the () is needed because otherwise he will call getTrim().
But @save() calls setters, trim function isn't a setter method, it doesn't take any parameters in, so that's the reason why it never can be done in this way.

Solution 1 : Do it in your setter.

You can add the trim method in your getter and setter method before returning/setting your variable.
If it's a basic business requirement of email, that's the place where you need to do it.
Like this, every usage of email will be correct through your whole application.

Solution 2 : Converter.

Zul :

="@bind('vm.state.email') @converter(vm.trimConverter)">

Java :

public class MyViewModel{
    private Converter trimConverter = new Converter() {
        // Implements the 2 methods and do your stuff.
        // Example in link provided.
    };

    public Converter getTrimConverter(){
        return myConverter;
    }
}

Solution 3 : Make your own custom component.

Custom components are underrated in ZK.
They are powerfull, easy to use and the advantage is refactoring is in 1 place in stead of multiple places.
But a custom components only pays off when you will actually use it more then once.

In this case, you can make a class who extends your component (I guess Textbox).
Override the getter (and/or setter) methods for value and add the trim() to it.
Very easy and your zul can stay clean that way.


So, depending on your needs, you have 3 possible solutions now.

Chill.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2018-03-16 17:45:19 +0800

Seen: 12 times

Last updated: Mar 18 '18

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More