Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1. This would be fairly simple if you could access the $this[data] from laravel's validator, but if you're just using Validator::extend for your custom rule, I don't believe you can. Nor will you be able to access other validation rules from within your rule, which could come in handy here. So the cleanest would probably be to extend the validator class. </p> <pre><code>class CustomValidator extends Illuminate\Validation\Validator { public function validateIfInVendorArray($attribute, $value, $parameters) { $other = $parameters[0]; $vendor = $this-&gt;data[$other]; //populate your array from your service call, then check if present $vendorArray = your service call or wherever you have it; //if in array, return the result of the Validator's validateRequired method, which we can access since it's protected in Validator if (in_array($vendor,$vendorArray)) return $this-&gt;validateRequired($attribute,$value); //if it wasn't in the array, return true to pass validation even if it doesn't exist return true; } } </code></pre> <p>Just remember to also <a href="http://laravel.com/docs/validation#custom-validation-rules" rel="nofollow">register your custom validator resolver</a></p> <p>To use your rule you just pass in the attribute name of the vendors field as a parameter.</p> <p>2. I think this depends on where your array is required, and how often.</p> <p>If it's only required for this validation I would do it inside your custom validation rule, to avoid piling up more work for the controller. </p> <p>Personally I would move the validation rules to a custom validator class, and then you could have the vendor array as a field of that custom validator. This custom validator class would then be a service for your controller to consume, see <a href="https://tutsplus.com/lesson/validation-services/" rel="nofollow">https://tutsplus.com/lesson/validation-services/</a> for a good example.</p> <p>I haven't tested any of this myself, but I believe it should work! Sorry if it's not the most elegant solution.</p>
    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.
    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