Symfony Server Validation

ADVERTISEMENT

Open-Source PHP5 MVC Framework
Agile Development
VALIDATION HELPERS
<?php echo use_helper('Validation’) ?>
SERVER VALIDATION
form_has_error($param)
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.
You can easily create new ones.
To validate the form data, create a YAML validation file with the same name of
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
sfStringValidator:
validation file sample:
values:
[foo, bar]
/<app_name>/modules/<module_name>/validate/send.yml
values_error: The only accepted values are foo and bar
fillin:
insensitive:
false # If true, comparison w/ values is case insensitive
E.g.: To validate the form data
enabled:
true
min:
2
on the call to the send action,
min_error:
Please enter at least 2 characters
validators:
a configuration file called
max:
100
myStringValidator:
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:
sfRegexValidator
required:
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
checks the equality of two different request parameters; very useful for
handleError<name_of_action>() method for the form handling action and
password check
end it with a redirection to the action with display the form. E.g.:
fields:
class ContactActions extends sfActions{
password1:
The check param contains
...
required:
the name of the field that
public function
handleErrorSend()
{
msg:
Please enter a password
the current field must
$this->forward('contact', 'index');
password2:
match to be valid.
}
required:
}
msg:
Please retype the password
sfCompareValidator:
You can add an error manually with the setError() method of the sfRequest:
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
your database. Useful for primary keys.
You can detect whether the form has errors by calling the ->hasErrors() method
In this example, the validator
fields:
of the sfRequest object. To get the list of the error messages,use the method
will look in the database for
nickname:
->getErrors(). So you should add the following lines at the top of the template:
a record of class User where
sfPropelUniqueValidator:
the login column has the same
<?php if ($sf_request->
hasErrors()
): ?>
class:
User
value as the field to validate.
<p>The data you entered seems to be incorrect.
column:
login
Please correct the following errors and resubmit:</p>
unique_error: This login already exists. Please choose another one.
<ul>
sfEmailValidator
<?php foreach($sf_request->
getErrors() as $error
): ?>
<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
fields
<?php if ($sf_request->
hasError('<name_of_the_field>')
): ?>
fields:
<?php echo $sf_request->
getError('<name_of_the_field>')
?>
image:
<?php endif ?><br />
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
- 'image/png'
By default, the automatic
to your YAML validation file:
- 'image/x-png'
repopulation works for:
fillin:
- 'image/pjpeg'
text inputs, check
enabled: true
# activate repopulation
mime_types_error: Only PNG and JPEG images are allowed
boxes, radio buttons,
param:
max_size:
512000
text areas and select
name: test
# Form name, not needed if there is
max_size_error: Max size is 512Kb
components (simple
only one form in the page
and multiple)
sfUrlValidator
skip_fields: [email] # Do not repopulate these fields
exclude_types: [hidden, password]
verifies a parameter contains a value that qualifies as a valid URL.
The fillin feature doesn't
# Do not repopulate these field types
sfUrlValidator:
repopulate:
check_types: [text, checkbox, radio] # Do repopulate
url_error:
This URL is invalid
file tags
converters:
# Converters to apply
sfDateValidator
htmlentities:
[first_name, comments]
htmlspecialchars: [comments]
verifies a parameter is of a date format.
This cheat-sheet is not an official part of the symfony documentation

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go