greggilbert / recaptcha

[ABANDONED] reCAPTCHA Validator for Laravel 5
MIT License
713 stars 196 forks source link

Error response #103

Closed mbaxter91288 closed 8 years ago

mbaxter91288 commented 8 years ago

Hi,

This is not an issue but I can't seem to tag this with "question" or anything.

When looping over all errors like below I get 2 responses:

@if (count($errors) > 0)
    <div class="form-errors">
        <ul class="error-list">
            @foreach($errors->all('<li>:message</li>') as $message)
            {!! $message !!}
            @endforeach
        </ul>
    </div>
@endif

I then get the following:

The g-recaptcha-response field is required.
Please ensure that you are a human!

Is there a way to only return one error? I can see how to change the "Please ensure that you are a human!" via the config, but not how to suppress the "The g-recaptcha-response field is required.".

Cheers, Mat

DigitalCoder commented 8 years ago

The g-recaptcha-response field is required. as you may have declared in the rules array in your request file for example :

public function rules()
    {
        return [
            'name'     => 'required|min:6|max:255',
            'email'    => 'required|email|unique:contact,email|max:255',
            'g-recaptcha-response' => 'required|recaptcha',
        ];
    }

and second try to debug with

<?php 
echo '<pre>';
var_dump($errors);
echo '</pre>';
?>

then it returns :

object(Illuminate\Support\ViewErrorBag)#261 (1) {
  ["bags":protected]=>
  array(1) {
    ["default"]=>
    object(Illuminate\Support\MessageBag)#262 (2) {
      ["messages":protected]=>
      array(1) {
        ["g-recaptcha-response"]=>
        array(2) {
          [0]=>
          string(43) "The g-recaptcha-response field is required."
          [1]=>
          string(33) "The captcha field is not correct."
        }
      }
      ["format":protected]=>
      string(8) ":message"
    }
  }
}

so you should need to get ["g-recaptcha-response"][1]

DigitalCoder commented 8 years ago

but you can also defined in the required rules array in your language translated validation.php file resources/lang/en/validation.php with this :

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],

        'g-recaptcha-response' => [
            'required' => 'We need to check the captcha field!',
        ]
    ],
DigitalCoder commented 8 years ago

also if you do not setup in your language translated validation.php file resources/lang/en/validation.php with

/*
     * Recaptcha
     */
    "recaptcha" => 'The :attribute field is not correct.',

You will see the default message : Please ensure that you are a human!

DigitalCoder commented 8 years ago

Me I did not setup "recaptcha" translation but I added

'custom' => [
        'g-recaptcha-response' => [
            'required' => 'The checkbox for the captcha must be selected !',
        ]
    ],

and in my view I've used this

{!! Recaptcha::render(['lang'=>'en', 'theme'=>'light']) !!}
@if ($errors->has('g-recaptcha-response'))
<div class="help-block alert alert-danger">
    <ul class="error-list">
        @foreach ($errors->get('g-recaptcha-response') as $message)
            <li><strong class="text-danger">{!! $message !!}</strong></li>
        @endforeach
    </ul>
</div>
@endif
greggilbert commented 8 years ago

This is an issue more about Laravel's validation. You may want to consider implode()ing the messages together.