lundi 3 avril 2017

Change input before validation because the number format is different from number validation in Laravel 5

I am trying to change the input that have a different number format than the format of the number validation rule. I tried other proposed solutions found on Laracasts forum or in Stack Overflow to no avail as nothing works.

Right now the solution I found sounds like cheating because I am using javascript to send a correct formatted field to Laravel and using the typed input just for looks (as it has a mask applied to it).

The best solution I can see is to remove the mask that I appply to the input before it hits Laravel and the number validation but I couldn't make it work. The other solution would be to change the input before it hits the validator in the FormRequest itself, but again I can't make it work either.

I want suggestions on what to do because right now I am facing the issue that I will need to change ALL fields that are numbers to have that extra input to be send instead of the real input field.

Thanks in advance.

Library used for mask: jquery.inputmask

Input fields

<label for="valor_venda" class="control-label">Valor de Venda</label>
                        <span class="required">*</span>
                        <input type="text" name="valor_venda" id="valor_venda" class="form-control">
                        <input type="text" name="valor_venda_formatado" id="valor_venda_formatado" class="form-control">

javascript applied (using jQuery)

$("#valor_venda").inputmask('decimal', {
      radixPoint:",",
      groupSeparator: ".",
      autoGroup: true,
      digits: 2,
      digitsOptional: true,
      placeholder: '0',
      rightAlign: true,
      onBeforeMask: function (value, opts) {
        return value;
      }
    });

    $('#valor_venda').on('keyup', function () {
       var formatar = $('#valor_venda').val();
        formatar = formatar.replace(/\./g,'');
        formatar = formatar.replace(/\,/g,'.');
        $('#valor_venda_formatado').val(formatar);
    });

TrocarMoedaRequest (only the rules array)

public function rules()
    {

        return [
            'tipo' => 'required',
            'moeda_id' => 'required|integer|not_in:0',
            'valor_venda_formatado' => 'required|numeric|not_in:0',
            'taxa' => 'required|numeric|not_in:0',
        ];
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire