Textfield validator probelm



  • Hi

    I created a textfield and assign the text to the field. In that , the validator not validate the text properly. how can i validate the text.

    code snippet:

    TextField{
        id:pt
        z:5
        x: 278
        y: 43
        text: ""
        width: 200
        height: 38
       validator: IntValidator {bottom: 000; top: 999;}
    
        Component.onCompleted:
        {
              pt.text="hi"
        }
    }
    

    In that above code, integer validator was used in the textfield and i will pass the string to the textfield. It display the string without validation. If i enter the string in the textfield, i validate perfectly. How can i solve the problem?


  • Moderators

    @eswar I'm not a QML expert but I guess these validators are used for user input.
    Why do you try to set values which shouldn't be accepted in your code?



  • @eswar I tried your code and it might be considered a bug, although as @jsulm pointed out, validators are expected to work on user input, which it does. Not sure why you will set an inappropriate value programmatically. Anyway, a workaround could be working on the onTextChanged signal, to see if the current value is indeed acceptable:

    TextField{
        id:pt
        ...
        validator: IntValidator {bottom: 000; top: 999;}
        onTextChanged: 
            if (!pt.acceptableInput) {
                console.log("unacceptable input, changed to default value");
                text = 0
            }
        Component.onCompleted:
        {
         ...
    


  • Hi @jsulm & @Pablo-J-Rogina

    Thanks for you comment

    My requirement is like to get text input from user as well as update the text based on the some external factors in the textfield. so that, i tested the textfield with different data. so that, i found that the validator allows the inappropriate data to the textfield. It didn't satisfy my requirements. In my project, all the textfield similar to the above requirements.

    Any other solution to solve this issue?



  • @eswar with due respect, have you looked at the code snippet I posted? You may want to check if the value set to the textfield is acceptable, whenever the value was entered by the user or programmatically, and if value is not acceptable, set some default value. I guess that's an appropriate workaround along with using validators



  • @Pablo-J.-Rogina

    Hi

    Its working fine.. Thanks for the solution..



  • @eswar great! so if your problem is solved, please use the Topic Tools button to mark as Solved and upvote the answer(s) that helped you to solve the issue. Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.