{"id":334,"date":"2021-06-26T12:07:29","date_gmt":"2021-06-26T15:07:29","guid":{"rendered":"http:\/\/hackerzone.com.br\/blog\/?p=334"},"modified":"2022-08-13T00:19:46","modified_gmt":"2022-08-13T03:19:46","slug":"como-traduzir-o-validator-csrf-laminas-framework","status":"publish","type":"post","link":"http:\/\/hackerzone.com.br\/blog\/como-traduzir-o-validator-csrf-laminas-framework\/","title":{"rendered":"Como traduzir o validator Csrf &#8211; Laminas Framework"},"content":{"rendered":"<p>Se voc\u00ea esta acostumado a utilizar os formul\u00e1rios do Laminas Form (antigo Zend Form), voc\u00ea j\u00e1 deve ter tentado utilizar o elemento Csrf:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">Laminas\\Form\\Element\\Csrf<\/pre>\n<p>Se voc\u00ea foi inocentemente utilizar ele nos validadores com <strong>InputFilterProviderInterface<\/strong> atrav\u00e9s do <strong>getInputFilterSpecification<\/strong> para traduzi-lo deve ter se deparado com o seguinte problema, toda vez que voc\u00ea tenta salvar o formul\u00e1rio o erro de tempo de inatividade \u00e9 apresentado: <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nThe form submitted did not originate from the expected site\r\n<\/pre>\n<p><strong>Prov\u00e1vel forma <span style=\"color:#EA141F;\">errada<\/span> que voc\u00ea utilizou<\/strong><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npublic function getInputFilterSpecification() {\r\n return array(\r\n  'security' =&gt; [\r\n            'required' =&gt; true,\r\n            'validators' =&gt; array(\r\n                array(\r\n                    'name' =&gt; 'notSame',\r\n                    'options' =&gt; array(\r\n                        'messages' =&gt; array(\r\n                            'isEmpty' =&gt; 'O formul\u00e1rio ficou inativo por muito tempo.'\r\n                        )\r\n                    )\r\n                )\r\n            )\r\n        ];\r\n );\r\n}\r\n<\/pre>\n<p>Por\u00e9m o elemento do tipo CSRF j\u00e1 \u00e9 validado por padr\u00e3o e quando voc\u00ea tenta especificar ele dentro do <strong>getInputFilterSpecification<\/strong> ele \u00e9 validado novamente, opa! Ent\u00e3o o elemento acaba sendo validado 2 vezes, por\u00e9m quando ele \u00e9 validado pela primeira vez ele apaga o hash de valida\u00e7\u00e3o e quando ocorre a segunda valida\u00e7\u00e3o \u00e9 retornado erro, pois ele compreende que n\u00e3o existe mais o hash daquele formul\u00e1rio.<\/p>\n<p>Ent\u00e3o como eu posso traduzir esse elemento? Simples, passe todos os par\u00e2metros diretamente na chamada do elemento:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n        $csrf = new \\Laminas\\Form\\Element\\Csrf('security');\r\n        $csrf-&gt;setOptions(['csrf_options' =&gt; [\r\n            'timeout' =&gt; 900,\r\n            'messageTemplates' =&gt; ['notSame' =&gt; 'O formul\u00e1rio esta inativo por muito tempo, tente enviar novamente.'],\r\n            ]]);\r\n        $this-&gt;add($csrf);\r\n<\/pre>\n<p>Estou postando essa curiosidade, pois quando eu passei por essa situa\u00e7\u00e3o pesquisei muito e nada encontrei, ent\u00e3o fucei at\u00e9 encontrar essa solu\u00e7\u00e3o, espero que possa ajudar mais algu\u00e9m.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Se voc\u00ea esta acostumado a utilizar os formul\u00e1rios do Laminas Form (antigo Zend Form), voc\u00ea j\u00e1 deve ter tentado utilizar o elemento Csrf: Se voc\u00ea foi inocentemente utilizar ele nos validadores com InputFilterProviderInterface atrav\u00e9s do getInputFilterSpecification para traduzi-lo deve ter se deparado com o seguinte problema, toda vez que voc\u00ea tenta salvar o formul\u00e1rio o [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,13],"tags":[150,149,151,111,18],"class_list":["post-334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-zend-framework-3","tag-csrf","tag-laminas-framework","tag-zend-element-csrf","tag-zend-form","tag-zend-framework-3","cat-12-id","cat-13-id","has_thumb"],"_links":{"self":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":4,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":340,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions\/340"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/media\/459"}],"wp:attachment":[{"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hackerzone.com.br\/blog\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}