mikebronner / nova-map-marker-field

Provides an visual interface for editing latitude and longitude coordinates.
MIT License
131 stars 36 forks source link

Validation #25

Closed timcv closed 4 years ago

timcv commented 4 years ago

Hi,

Would it be possible to add validations to this field?

I would like to set the field as 'required'

Thanks in advance

//Tim

mikebronner commented 4 years ago

Hi @timcv Yes, you should be able to apply validations, just like with other Nova fields. Let me know if they don't work, but I have been using "required" in my apps on this field as well.

timcv commented 4 years ago

Strange, i tried again and it wont validate...

This is how my fields look like:

public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('name', 'name')
                ->rules('required', 'max:255'),
            MapMarker::make("Location")
                     ->defaultLatitude(62.38583179)
                     ->defaultLongitude(16.321998712)
                     ->defaultZoom(4)
                     ->hideFromIndex()
                     ->rules('required'),
            BelongsToMany::make('missions', 'missions', Mission::class)
                         ->fields(function () {
                             return [
                                 Text::make('order'),
                             ];
                         }),
        ];
    }
mikebronner commented 4 years ago

If you provide default values, your validation requirements are already satisfied. :) Default values populate the field as if the user made the selection.

timcv commented 4 years ago

I get the same result when i remove the default values.

MapMarker::make("Location")
                     ->hideFromIndex()
                     ->rules('required'),

When i have the default values, nothing is saved to the database and there is no validation error.

mikebronner commented 4 years ago

@timcv Actually, I just retested, and validation does with, even with default values:

            MapMarker::make("Location")
                ->help("Identify the location where the building is, or once
                    stood. Enter the address in the map to get close, then
                    manually move the map around to get to the exact spot.")
                ->hideFromIndex()
                ->defaultZoom(12)
                ->defaultLatitude(41.823611)
                ->defaultLongitude(-71.422222)
                ->rules("required", function ($attribute, $value, $fail) {
                    if (intval($value) === 0) {
                        return $fail('The map location must be specified. Please
                            move the map marker to the correct location.');
                    }
                }),