{"id":6849,"date":"2014-05-03T05:12:43","date_gmt":"2014-05-03T05:12:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/03\/laravel-form-validation-string-length-error-messages-cause-exception-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:45:32","modified_gmt":"2022-08-30T15:45:32","slug":"laravel-form-validation-string-length-error-messages-cause-exception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/03\/laravel-form-validation-string-length-error-messages-cause-exception-collection-of-common-programming-errors\/","title":{"rendered":"Laravel: Form validation string-length error messages cause exception-Collection of common programming errors"},"content":{"rendered":"<p>When trying to implement Laravel&#8217;s length based validation<\/p>\n<pre><code>'password' =&gt; array(\n    'required',\n    'alpha_dash',\n    'Min:7'\n)\n<\/code><\/pre>\n<p>and outputting error messages in my view<\/p>\n<pre><code>{{\n    $errors-&gt;first(\n        'password',\n        ':message'\n    )\n}}\n<\/code><\/pre>\n<p>I get<\/p>\n<pre><code>Unhandled Exception\nMessage:\nArray to string conversion\nLocation:\n_avalog\\laravel\\messages.php on line 188\nStack Trace:\n#0 _avalog\\laravel\\laravel.php(42): Laravel\\Error::native(8, 'Array to string...', '_avalog...', 188)\n#1 [internal function]: Laravel\\{closure}(8, 'Array to string...', '_avalog...', 188, Array)\n#2 _avalog\\laravel\\messages.php(188): str_replace(':message', Array, 'get('password', 'get()\n<\/code><\/pre>\n<p>Debugging, it appears to be true. If I <code>print_r( $validation );<\/code><\/p>\n<pre><code>Laravel\\Validator Object (\n    [attributes] =&gt; Array (\n        [username] =&gt; fred\n        [email] =&gt;\n        [password] =&gt; asd\n        [csrf_token] =&gt; DWg3CUfqtMZkIRfyZXNEqygvWUHsGS9SQMue2V4S\n    )\n    [errors] =&gt; Laravel\\Messages Object (\n        [messages] =&gt; Array (\n            [email] =&gt; Array (\n                [0] =&gt; The email field is required.\n            )\n            [password] =&gt; Array ( \n                [0] =&gt; Array (\n                    [numeric] =&gt; The password must be at least 7.\n                    [file] =&gt; The password must be at least 7 kilobytes.\n                    [string] =&gt; The password must be at least 7 characters.\n                )\n            )\n    )\n    [format] =&gt; :message\n)\n<\/code><\/pre>\n<p>You can see that messages does in fact contain an array for password which appears to be dependent upon input type, even though I&#8217;ve specified in the rule it is alphadash<\/p>\n<pre><code>[password] =&gt; Array (\n    [0] =&gt; Array (\n        [numeric] =&gt; The password must be at least 7.\n        [file] =&gt; The password must be at least 7 kilobytes.\n        [string] =&gt; The password must be at least 7 characters.\n    )\n)\n<\/code><\/pre>\n<p>Whereas the rest, do not<\/p>\n<pre><code>[email] =&gt; Array (\n    [0] =&gt; The email format is invalid.\n)\n<\/code><\/pre>\n<p>Looking at <code>messages.php<\/code> in Laravel framework, it has nothing to handle such array-based messaging so I assume I am doing something wrong before it gets there, but I don&#8217;t know what.<\/p>\n<p>Thanks for your help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When trying to implement Laravel&#8217;s length based validation &#8216;password&#8217; =&gt; array( &#8216;required&#8217;, &#8216;alpha_dash&#8217;, &#8216;Min:7&#8217; ) and outputting error messages in my view {{ $errors-&gt;first( &#8216;password&#8217;, &#8216;:message&#8217; ) }} I get Unhandled Exception Message: Array to string conversion Location: _avalog\\laravel\\messages.php on line 188 Stack Trace: #0 _avalog\\laravel\\laravel.php(42): Laravel\\Error::native(8, &#8216;Array to string&#8230;&#8217;, &#8216;_avalog&#8230;&#8217;, 188) #1 [internal function]: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1],"tags":[],"class_list":["post-6849","post","type-post","status-publish","format-standard","hentry","category-laravel","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6849"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6849\/revisions"}],"predecessor-version":[{"id":8823,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6849\/revisions\/8823"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}