views:

19

answers:

1

For example imagine I've a rest service, this service takes two parameters :

  • phone number
  • text

The goal is to send the message via a sms gateway.

I've a class Message which has two properties destinationNumber and textMessage. Before calling the gateway, I want to validate the data received by the rest service.

I've two questions relatives to how to validate the data :

  • Where should I put the validation rules ? in the model or in the controller
  • How should I use the sfValidator* classes from Symfony to validate the data (ie. where's the documentation for using sfValidator or where can I find some examples)

Any help would be appreciated.

A: 

What you want to do is use the form framework for this.

It handles all the validation for you. You pass the data from your REST request to a new form and call validate.

If you create your model the forms are generated for you, take a look at the base classes to see the default validators.

You can override these validators with your own, take one that is similar to what you are trying to achieve (string validator, email validator) and overload it with your own code.

See: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/10 for more info on the forms.

johnwards