form_error($param, $options=array(), $catalogue= 'messages') FORM VALIDATION, REPOPULATION and VALIDATORS FORM VALIDATION VALIDATORS The validators can be found in the symfony lib validator directory. YAML VALIDATION FILE Each validator is a particular class that can have certain parameters. To validate the form data, create a YAML validation file with the same name of You can easily create new ones. the action called by the form in the validate directory of the module. This file sfStringValidator contain the name of fields that need to be validated and the validators. apply string-related constraints to a parameter validation file sample: sfStringValidator: /<app_name>/modules/<module_name>/validate/send.yml values: [foo, bar] values_error: The only accepted values are foo and bar fillin: E.g.: To validate the form data insensitive: false # If true, comparison w/ values is case insensitive enabled: true on the call to the send action, min: 2 validators: a configuration file called min_error: Please enter at least 2 characters myStringValidator: max: 100 send.yml must be created max_error: Please enter less than 100 characters class: sfStringValidator param: sfNumberValidator min: 2 verifies if a parameter is a number and allows you to apply size constraints min_error: This field is too short (2 characters minimum) sfNumberValidator: max: 100 nan_error: Please enter an integer max_error: This field is too long (100 characters maximum) min: 0 methods: [post] # This is the default setting min_error: The value must be at least zero max: 100 fields: max_error: The value must be less than or equal to 100 name: required: sfRegexValidator msg: The name field cannot be left blank allows you to match a value against a regular expression pattern myStringValidator: sfRegexValidator: match: No match_error: Posts containing more than one URL are considered as spam ACTION MODIFICATION pattern: /http.*http/si By default, symfony looks for a handleError<name_of_action>() method in The match param determines if the request parameter must matchthe pattern the action class whenever the validation process fails, or displays the to be valid (value Yes) or match the pattern to be invalid (value No) <name_of_action>Error.php template if the method doesn't exist. sfCompareValidator To display the form again with an error message in it, override the default handleError<name_of_action>() method for the form handling action and checks the equality of two different request parameters; very useful for end it with a redirection to the action with display the form. E.g.: password check fields: class ContactActions extends sfActions{ password1: The check param contains ... required: the name of the field that public function handleError() { msg: Please enter a password $this->forward('contact', 'index'); the current field must password2: match to be valid. } required: } msg: Please retype the password You can add an error manually with the setError() method of the sfRequest: sfCompareValidator: check: password1 $this->getRequest()->setError('name', 'The name field cannot be left blank'); compare_error: The two passwords do not match sfPropelUniqueValidator TEMPLATE MODIFICATION validates that the value of a request parameter doesn't already exist in You can detect whether the form has errors by calling the ->hasErrors() method your database. Useful for primary keys. In this example, the validator of the sfRequest object. To get the list of the error messages,use the method fields: will look in the database for ->getErrors(). So you should add the following lines at the top of the template: nickname: a record of class User where sfPropelUniqueValidator: <?php if ($sf_request->hasErrors()): ?> the login column has the same class: User <p>The data you entered seems to be incorrect. value as the field to validate. column: login Please correct the following errors and resubmit:</p> unique_error: This login already exists. Please choose another one. <ul> <?php foreach($sf_request->getErrors() as $error): ?> sfEmailValidator <li><?php echo $error ?></li> verifies if a parameter contains a value that qualifies as an email <?php endforeach ?> sfEmailValidator: </ul> strict: true <?php endif ?> email_error: This email address is invalid To show the error message next to the field with error, simply add the sfFileValidator following line to every field: applies format (an array of mime types) and size constraints to file upload <?php if ($sf_request->hasError('<name_of_the_field>')): ?> fields <?php echo $sf_request->getError('<name_of_the_field>') ?> fields: <?php endif ?><br /> image: required: msg: Please upload an image file file: True FORM REPOPULATION sfFileValidator: mime_types: If you want your form to be filled in with the values - 'image/jpeg' previously entered by the user, simply add these lines By default, the automatic - 'image/png' to your YAML validation file: repopulation works for: - 'image/x-png' fillin: - 'image/pjpeg' enabled: true # activate repopulation text inputs, check mime_types_error: Only PNG and JPEG images are allowed param: boxes, radio buttons, max_size: 512000 name: test # Form name, not needed if there is text areas and select max_size_error: Max size is 512Kb only one form in the page components (simple skip_fields: [email] # Do not repopulate these fields and multiple) sfUrlValidator exclude_types: [hidden, password] verifies a parameter contains a value that qualifies as a valid URL. # Do not repopulate these field types The fillin feature doesn't sfUrlValidator: check_types: [text, checkbox, radio] # Do repopulate repopulate: url_error: This URL is invalid converters: # Converters to apply file tags htmlentities: [first_name, comments] sfDateValidator htmlspecialchars: [comments] verifies a parameter is of a date format.
http://andreiabohner.wordpress.com This cheat-sheet is not an official part of the symfony documentation