file code.txt validation

Home Forums Quform WordPress file code.txt validation

This topic is: resolved

This topic contains 8 replies, has 3 voices, and was last updated by  Icemanmx 3 months, 4 weeks ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #26292

    Icemanmx
    Participant

    Hello
    I want to make that i can make a code.txt file instead of array of pass codes if that can be done.

    add_filter('quform_element_valid_1_37', function ($valid, $value, $element) {
    $codes = array('PASS', 'pass');

    if (!in_array($value, $codes)) {
    $element->addError('Código no válido');
    $valid = false;
    }

    return $valid;
    }, 10, 3);

    Thanks in advance
    Icemanmx

    #26310

    Icemanmx
    Participant

    Or even can use a api for codes….

    • This reply was modified 4 months, 1 week ago by  Icemanmx.
    #26345

    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #26353

    Icemanmx
    Participant

    Thanks again,,,
    Will try it later today..

    Thanks

    #26355

    Icemanmx
    Participant

    Hello
    I did not function for me.

    add_filter('quform_element_valid_1_37', function ($valid, $value, $element) {
     /  $codes = file(WP_CONTENT_DIR . '/surveyhdapg/codes.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
       if (!in_array($value, $codes)) {
           $element->addError('Código no válido');
           $valid = false;
        }
       return $valid;
    }, 10, 3);
    
    

    I have all the files under plugin folder /surveyhdapg/codes.txt in that text file i have one word on each row:

    test
    test1
    TEST1

    But i get the error Código no válido

    I also tried to put 666 permission on the txt file, but normally 644 should be okay..

    Best Regards
    Icemanmx

    #26365

    katw
    Participant

    Hi @icemanmx

    Saw your post and thought I could help.

    You said your code.txt file is in the plugin folder – yes?

    If yes, the code you are using is looking for file in WP Content folder. Also you have a leading slash at start of the line which shouldn’t be there.

    Try:

    add_filter('quform_element_valid_1_37', function ($valid, $value, $element) {
       $codes = file(WP_PLUGIN_DIR . '/surveyhdapg/codes.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
       if (!in_array($value, $codes)) {
           $element->addError('Código no válido');
           $valid = false;
        }
       return $valid;
    }, 10, 3);
    #26387

    Icemanmx
    Participant

    Hello
    I already try that without the trailing slash, but he don´t read the txt file what i put in it… Maybe need right permissions, but i try 666 and 644 and did not help…

    Best Regards
    Icemanmx

    • This reply was modified 3 months, 4 weeks ago by  Icemanmx.
    #26389

    katw
    Participant

    The SLASH at start of $codes line shouldn’t be there so take it out. I didn’t suggest removing a trailing slash, just the one at start of the first line.

    Did you see the directory path change in my code?

    WP_PLUGIN_DIR not WP_CONTENT_DIR

    You could add a debug statement to see what path is being created and then test the path manually to see if you can access the code.txt file from that path.

    You might also see from doing this if the text file contains the correct contents. Sometimes when file security is set wrong the contents shows an error message not the file itself which gives you heads-up you need to fix file attributes.

    add_filter('quform_element_valid_1_37', function ($valid, $value, $element) {
    
       $pathcheck = WP_PLUGIN_DIR . '/surveyhdapg/codes.txt';
       echo '<script language="javascript">';
       echo 'alert("Path:' . $pathcheck .'")';
       echo '</script>';
    
       $codes = file(WP_PLUGIN_DIR . '/surveyhdapg/codes.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
       if (!in_array($value, $codes)) {
           $element->addError('Código no válido');
           $valid = false;
        }
       return $valid;
    }, 10, 3);

    I assume you have the correct field reference 1_37 too? You will know soon enough if the alert box appears.

    • This reply was modified 3 months, 4 weeks ago by  katw. Reason: added hint
    • This reply was modified 3 months, 4 weeks ago by  katw. Reason: Slash clarification
    #26392

    Icemanmx
    Participant

    Thanks katw

    This function perfect now…

    Best Regards
    Icemanmx

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

Be inspired. © 2018 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy