• Resolved ngwp

    (@ngwp)


    Hello.

    I would like to switch required fields depending on the value of a radio button.

    I prepare fields like this:

    
    [radio my-data-size use_label_element default:1 "Less than 2MB" "More than 2MB"]
    
    [file my-data filetypes:pdf limit:2mb]
    
    [text my-data-url]
    
    

    And I add the code bellow:

    
    function wpcf7_validate_customize($result,$tags){
    
      foreach( $tags as $tag ){
    	
        $type = $tag['type'];
        $name = $tag['name'];
    
        switch($type){
          case radio:
          case 'radio*':
            if($name === 'my-data-size'){
              if($_POST[$name] === 'Less than 2MB'){
                if (is_null($_POST['my-data'])) {
                  $result->invalidate( 'my-data','You need to upload a file' );
                }
              } elseif($_POST[$name] === 'More than 2MB') {
                if ($_POST['my-data-url'] === "") {
                  $result->invalidate( 'my-data-url','You need to note a download URL' );
                }
              }
            }
            break;
        }
    
      }
      return $result;
    }
    
    add_filter('wpcf7_validate', 'wpcf7_validate_customize', 11, 2);
    

    I would like to rule as below:

    – When you chose “Less than 2MB”, then “my-data” is “required”.
    – When you chose “More than 2MB”, then “my-data-url” is “required”.

    But
    When I upload a file and submit a form, “You need to upload a file” is shown.

    Could you give me some advice?

    • This topic was modified 3 years, 4 months ago by ngwp.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Validation for file uploader’ is closed to new replies.