Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whilst Laravel has a wide range of validations rules included, checking for the presence of a word from a given list isn't one of them:</p> <p><a href="http://laravel.com/docs/validation#available-validation-rules" rel="noreferrer">http://laravel.com/docs/validation#available-validation-rules</a></p> <p>However, Laravel also allows us to create our own custom validation rules:</p> <p><a href="http://laravel.com/docs/validation#custom-validation-rules" rel="noreferrer">http://laravel.com/docs/validation#custom-validation-rules</a></p> <p>We can create validation rules using <code>Validator::extend()</code>:</p> <pre class="lang-php prettyprint-override"><code>Validator::extend('not_contains', function($attribute, $value, $parameters) { // Banned words $words = array('a***', 'f***', 's***'); foreach ($words as $word) { if (stripos($value, $word) !== false) return false; } return true; }); </code></pre> <p>The code above defines a validation rule called <code>not_contains</code> - it looks for presence of each word in <code>$words</code> in the fields value and returns false if any are found. Otherwise it returns true to indicate the validation passed.</p> <p>We can then use our rule as normal:</p> <pre class="lang-php prettyprint-override"><code>$rules = array( 'nickname' =&gt; 'required|not_contains', ); $messages = array( 'not_contains' =&gt; 'The :attribute must not contain banned words', ); $validator = Validator::make(Input::all(), $rules, $messages); if ($validator-&gt;fails()) { return Redirect::to('register')-&gt;withErrors($validator); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload