samedi 9 février 2019

With AutoNumeric how to get value raw Value on modified on validation?

I use autoNumeric(@4.1.0) in my laravel 5.7/ blade / jQuery v3.3.1 / Bootstrap v4.1.2 app inited with options no decimals:

    new AutoNumeric('#count', {
        maximumValue: '999999',
        minimumValue : 0,
        decimalPlaces : 0,
        showOnlyNumbersOnFocus : true
    });

and I use laraval validation https://github.com/proengsoft/laravel-jsvalidation library, which trigger when field is changed (not only whem Submit is clicked) so I got validation error

but I failed validate as with thousand separator I got value with “.” char,

Reading doc https://github.com/autoNumeric/autoNumeric I found rawValue with callback mentioned:

 const options = {
    styleRules : {
        userDefined: [
            // 1) If 'classes' is a string, set it if `true`, remove it if `false`
            { callback: rawValue => { return true; }, classes: 'thisIsTrue' },
            // 2) If 'classes' is an array with only 2 elements, set the first class if `true`, the second if `false`
            { callback: rawValue => rawValue % 2 === 0, classes: ['autoNumeric-even', 'autoNumeric-odd'] },
            // 3) Return only one index to use on the `classes` array (here, 'class3')
            { callback: rawValue => { return 2; }, classes: ['class1', 'class2', 'class3'] },
            // 4) Return an array of indexes to use on the `classes` array (here, 'class1' and 'class3')
            { callback: rawValue => { return [0, 2]; }, classes: ['class1', 'class2', 'class3'] },
            // 5) If 'classes' is `undefined` or `null`, then the callback is called with the AutoNumeric object passed as a parameter
            { callback: anElement => { return anElement.getFormatted(); } },
        ],
    },
}

So I need when field is modified and focus is out to callback rawValue and get raw ineteger value to put it into other hidden input for submitting.

1)But I did not find how tro use it. Could you please provide a sample how to make it?

2) As I use laravel form request class :

class RequestClass extends FormRequest
{
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $request= Request();
        return      [
            'count' => [
                'required',
                'integer',
                Rule::unique(with(new StorageCapacity)->getTableName())->ignore($storage_capacity_id),
            ],
            'sqft'                   =>  'required|max:10',
            'capacity_category_id'   => 'required|exists:' . ( with(new CapacityCategory)->getTableName() ).',id',
        ];

    }

}

If there is a way on FormRequest side to filter my value with thousand separator to get valid validation ?

But rawValue on JS side decision seems preferable...

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire