which loop while I should use and how

Posted 1 month ago by chechogrom

Hello everyone, I have a form in which I have a select with the plugin select 2 and I have the option of tags = true, in this select I send name-value-description (optional), and the format would be for example: john doe-15000 jane doe-12500-optional description.

for the user to send the correct format create a validation rule

form input

<div class="form-group col-md-6 {{ $errors->has('pagos') ? 'has-error' : '' }}">
    <label for="pagos">Pago / valor pago <i class="fa fa-info-circle" data-toggle="tooltip" data-placement="right"              title="Ej: andres-50000 luis-78500"></i></label>
    <select class="form-control select2-pagos" multiple="multiple" name="pagos[]" data-placeholder="Dele un nombre al pago y su valor, separelos por '-'" style="width: 100%;">
        @foreach (old('pagos') as $pago)
            <option value="{{ $pago }}" selected="selected">{{ $pago }}</option>
    @if ($errors->has('pagos'))
    <label class="control-label" for="pagos"><i class="fa fa-times-circle-o"></i>
      <strong>{{ $errors->first('pagos') }}</strong>


    'pagos' => ['required', new ValidFormatPagos],


public function passes($attribute, $values)
      foreach ($values as $key => $value) {
        $pagoSeparados = explode('-',$value);
        // dd($values);
        if (count($pagoSeparados) == 2) {
          if (is_string($pagoSeparados[0]) && ctype_digit($pagoSeparados[1])) {
            return true;
        }elseif (count($pagoSeparados) > 2) {
          if (is_string($pagoSeparados[0]) && ctype_digit($pagoSeparados[1]) && is_string($pagoSeparados[2]) != true) {
            return true;
      return false;

     * Get the validation error message.
     * @return string
    public function message()
        return 'El formato para los pagos es incorrecto.';

first I go through the values ‚Äč‚Äčthat they send from the form with a foreach and I divide them by the character "-" (0 => "John doe", 1 => "15000") then I ask if the array is equal to 2 is that it comes name and value, the name will always be in pos 0 and the value in pos 1, then ask if the value in pos 0 is string and the value in pos 1 is an int, if these two conditions are true return true. up to here everything is fine, if in the select send ([john doe-15000] [Jane doe]) the validation happens since in the first condition it is true but the second one is not, how can I make it evaluate all the tags and not only the first, I'm sorry if I was not clear somewhere you can ask me, I'm using google translate, and thanks in advance to those who can help me

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.