2

As outlined here I'm working with GeoPositionFields. Because this is not supported from Zend, I went with a standard RegEx validator.

It works great but I still need a custom error message - any ideas how to achieve this?

The one in my example just does nothing...

/**
 * @ORM\Column(type="string")
 * @Form\Filter({"name":"StringTrim"})
 * @Form\Validator({"name":"Regex", "options":{"pattern":"/(-?\d{1,3}\D\d+)[^\d-]+(-?\d{1,3}\D\d+)/"}})
 * @Form\ErrorMessage("My custom message")
 * @Form\Attributes({"type":"text"})
 * @Form\Options({"label":"GeoPos"})
 *
 */
protected $geopoint;

Even this one is just beeing ignored:

@Form\Messages({"regexNotMatch": "My custom message"})
Community
  • 1
  • 1
Ron
  • 22,128
  • 31
  • 108
  • 206

2 Answers2

8

You will have to overwrite the default messages using the messages key from the options.

Try this (i guess you'll have to trim this into one line though for the annotations to work ;) That's up to you, hehe.

@Form\Validator({
    "name":"regex", 
    "options":{
        "pattern":"/(-?\d{1,3}\D\d+)[^\d-]+(-?\d{1,3}\D\d+)/",
        "messages":{
            "regexInvalid":"Regex is invalid, Booo!",
            "regexNotMatch": "Input doesn't match, bleeeeh!",
            "regexErrorous": "Internal error, i'm like wtf!"
        }
    }
})

Each Validator has it's own messages. You're best advised to see the Source-Code to find out what ErrorMessages are present within each Element. To give you an example, please follow this link (click) to see how to find out about the messages-keys.

When using the array-style-syntax for creating forms outside of Annotations, it may be a little bit safer to go the statis approach for the keys like

'messages' => array(
    \Zend\Validator\Regex::INVALID => "Regex is invalid, Booo!",
    //etc...
)

Since strings - at least in theory - could always change, the constants won't.

Sam
  • 16,435
  • 6
  • 55
  • 89
  • 1
    hm... still no reaction: `@Form\Validator({"name":"Regex", "options":{"pattern":"/(-?\d{1,3}\D\d+)[^\d-]+(-?\d{1,3}\D\d+)/"}, "messagesTemplates":{"regexInvalid":"Regex is invalid, Booo!","regexNotMatch": "Input doesn't match, bleeeeh!","regexErrorous": "Internal error, i'm like wtf!"}})` – Ron Dec 28 '12 at 11:16
  • Are you getting default error messages if you leave the templates? Any errors at all? – Sam Dec 28 '12 at 11:22
  • My bad, it's `messages` not `messagesTemplates` - editted the answer, sorry. – Sam Dec 28 '12 at 11:29
1
/^(\-?\d+(?:\.\d+)?),?\s*(\-?\d+(?:\.\d+)?)$/

This regexp validates and captures GEO input:

  • In format Latitude, Longitude
  • In format Latitude Longitude
  • Coordinates copied directly from GoogleMaps
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51