Validation not working

Validation not working

nsaknsak Posts: 36Questions: 6Answers: 0

hi,

In my table I have two text fields where the user sets 2 numbers, greater_than and lower_than. Obviously there has to be greater_than<lower_than, so I have the following code:

$('input', editor.field('manage.lower_than').node()).change(function(){
    if( (editor.field('manage.lower_than').val() <= editor.field('manage.greater_than').val()) &&     editor.field('manage.lower_than').val() > 0) ){
                editor.field('manage.lower_than').error("error");
            }
            if( editor.field('manage.lower_than').val() > editor.field('manage.greater_than').val() ){
                editor.field('manage.lower_than').error("");
            }
        });

However, when I try to test it, it doesn't work. Specifically, if I give, as an example, greater_than=23, lower_than=12 the error message pops-up. So far so good, but when I correct it to lower_than=125, the error remains. Also, if greater_than=67 and lower_than=9, no error message pops-up. So, it seems to me that it only compares the first digit of the numbers.
Any ideas on what I have done wrong??

Moreover, besides that problem, the user, despite the error message that may pop-up, can still submit the data and create the new entry. So, no validation takes place. Do I have to validate it through php? If so, how to??

Thanks a lot in advance!

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    So, no validation takes place. Do I have to validate it through php?

    Ideally yes. Server-side validation has to be done, and should always be done in preference to client-side validation since it is trivial to bypass client-side validation.

    The Editor PHP manual discusses validators and writing custom validators with closure methods.

    However, there is still a place for client-side validation (although personally I tend not to double the amount of validation code and just use server-side validation). The reason your form is still submitting is because it isn't being stopped (the fact that a field is in error doesn't stop it). You can use the preSubmit event to validate data - example.

    As to why the message isn't clearing - I'm not sure. It looks like it should! Can you link to the page?

    Allan

  • nsaknsak Posts: 36Questions: 6Answers: 0
    edited November 2014

    Unfortunately, there is no page uploaded yet. So, all I can do is describe what I see. If I add an alert to test if the values greater_than and lower_than are submitted correctly, I don't see any problem. Also, for both of those fields I have used the Validator::numeric, so they both are numbers. Honestly, I can't imagine what the problem is. Could there be something wrong with the libraries I use?
    Do you think that copy-pasting some part of the code could be useful? If yes, which one?

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    My own suggestion would be to drop the client-side validation entirely. You have to do it server-side anyway, so why do it twice? :-). It would save an XHR call to the server, but that might save 200mS of time for an infrequent operation?

    On the server-side you could likely want to provide a closure function that would validate each number individually (that it is a number and that it is greater / equal to / less than, as appropriate).

    Perhaps start with that, since it will be needed regardless, and then if you want to look into doing client-side validation as well, let me know and we can look into it.

    Allan

  • nsaknsak Posts: 36Questions: 6Answers: 0

    Ok, so I perform server side validation. However, the following code isn't working:

    Field::inst( 'manage.greater_than' )
            ->validator( 'Validator::maxNum', array(
                "max"=>"manage.lower_than",
                "message"=>"Please enter a valid value"
            ) ),
        Field::inst( 'manage.lower_than' )
            ->validator( 'Validate::minNum', array(
                "min"=>"manage.greater_than",
                "message"=>"Please enter a valid value"
            ) )
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    That won't work because "manage.greater_than" is a string not a numeric value. The min (and max) properties are integers.

    As I noted above you would need to use a closure to do it. Something like:

    Field::inst( 'manage.lower_than' )
      ->validator( function ( $data, $row, $opts ) {
        if ( $data > $row['manage.greater_than'] ) {
          return "The lower than number can't be greater than the greater than number!";
        }
        return true;
      } )
    

    Allan

  • nsaknsak Posts: 36Questions: 6Answers: 0

    I am afraid that doesn't work either. When I submit the new entry I get a "An error has occurred - Please contact the system administrator". Here is the php script:

    $data = Editor::inst( $db, 'manage')
    ->field(
        ...
        Field::inst( 'manage.greater_than' )
            ->validator( function ( $val, $data, $opts ) {
                if ( $val > $data['manage.lower_than'] ) {
                    return "Enter a valid value";
                }
                return true;
            } ),
        Field::inst( 'manage.lower_than' )
            ->validator( function ( $val, $data, $opts ) {
                if ( $val < $data['manage.greater_than'] ) {
                    return "Enter a valid value";
                }
                return true;
            } ),
    ...
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    What is the error? Likely a syntax error that I missed...

    Allan

  • nsaknsak Posts: 36Questions: 6Answers: 0

    I am not sure...I will look into it.

    Thanks a lot for your time Allan!

  • darren@imagesoft.co.ukdarren@imagesoft.co.uk Posts: 1Questions: 0Answers: 0

    From a similar problem of my own it would seem that the $data array is two dimensional. If you replace $data['manage.lower_than'] with $data['manage']['lower_than'] that should do the trick.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Oops - I missed that. Good spotting! Not sure if @nsak is still working on that as the post was from November last year, but that does look like the issue.

    Allan

This discussion has been closed.