{"id":14692221,"date":"2021-07-23T06:52:40","date_gmt":"2021-07-23T06:52:40","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/custom-validation-for-file-uploader\/"},"modified":"2021-07-23T06:54:13","modified_gmt":"2021-07-23T06:54:13","slug":"custom-validation-for-file-uploader","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/custom-validation-for-file-uploader\/","title":{"rendered":"Custom Validation for file uploader"},"content":{"rendered":"<p>Hello.<\/p>\n<p>I would like to switch required fields depending on the value of a radio button.<\/p>\n<p>I prepare fields like this:<\/p>\n<pre><code>\n[radio my-data-size use_label_element default:1 &quot;Less than 2MB&quot; &quot;More than 2MB&quot;]\n\n[file my-data filetypes:pdf limit:2mb]\n\n\n\n<\/code><\/pre>\n<p>And I add the code bellow:<\/p>\n<pre><code>\nfunction wpcf7_validate_customize($result,$tags){\n\n  foreach( $tags as $tag ){\n\t\n    $type = $tag[&#039;type&#039;];\n    $name = $tag[&#039;name&#039;];\n\n    switch($type){\n      case radio:\n      case &#039;radio*&#039;:\n        if($name === &#039;my-data-size&#039;){\n          if($_POST[$name] === &#039;Less than 2MB&#039;){\n            if (is_null($_POST[&#039;my-data&#039;])) {\n              $result-&gt;invalidate( &#039;my-data&#039;,&#039;You need to upload a file&#039; );\n            }\n          } elseif($_POST[$name] === &#039;More than 2MB&#039;) {\n            if ($_POST[&#039;my-data-url&#039;] === &quot;&quot;) {\n              $result-&gt;invalidate( &#039;my-data-url&#039;,&#039;You need to note a download URL&#039; );\n            }\n          }\n        }\n        break;\n    }\n\n  }\n  return $result;\n}\n\nadd_filter(&#039;wpcf7_validate&#039;, &#039;wpcf7_validate_customize&#039;, 11, 2);\n<\/code><\/pre>\n<p>I would like to rule as below:<\/p>\n<p>&#8211; When you chose &#8220;Less than 2MB&#8221;, then &#8220;my-data&#8221; is &#8220;required&#8221;.<br \/>\n&#8211; When you chose &#8220;More than 2MB&#8221;, then &#8220;my-data-url&#8221; is &#8220;required&#8221;.<\/p>\n<p>But<br \/>\nWhen I upload a file and submit a form, &#8220;You need to upload a file&#8221; is shown.<\/p>\n<p>Could you give me some advice?<\/p>\n","protected":false},"template":"","class_list":["post-14692221","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14692221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":1,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14692221\/revisions"}],"predecessor-version":[{"id":14692228,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14692221\/revisions\/14692228"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=14692221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}