symfony1 - Symfony form validation for date fields (sfWidgetFormI18nDate) -
I am using Symphony 1.2.9, and I have a form that has two date fields:
Start_date and end_date.
I want to apply the following verification criteria for the 'start_date' field:
- i). Can not be less than today's date ii) can not exceed end_date iii). Can not be longer than 1 month
For end_date, I want the following restrictions:
- i). Can not be longer than 3 months from today
I wrote a postal verifier as follows:
$ today = date ('ym -d '); // Post Validated Check Make sure that the end date & gt; Start date $ the-as-> Valid shoe- & gt; Setpoint declarator (new sfValidator and new array (sfValidatorSchemaCompare ('start_date', '<', 'end_date', array ('throw_global_error' => true), array ('invalid' = & gt; '' start date ("% Left_field%") should be before the end date ("% right_field%");
New sfValidatorSchemaCompare ('start_date', '& lt;', $ Today, Array ('throw_global_error' = & gt; true), array ('invalid' = & gt; 'Initial date ("% left_field%") can not be before today's date: ('. $ Today. ') Array ( 'Invalid' = & gt; 'end date' ('& lt; br / & gt;')), new sfValidatorSchemaCompare ('end_date', '& gt;', $ Today, array ('throw_global_error' = & gt; % Left_field ("% Right_field%")
However, this does not work That is, there is no way to implement official sanctions from today's date or offsets from today's date.
A solution will be very welcome.
For individual code readability, I would like to take your post validation check to a post validate method. Perm, like:
Configure public functions () {// Your normal configuration stuff goes here // Your post validation method $ this- & gt; ValidatorSchema- & gt; SetPostValidator (new sfValidatorCallback (array ('callback' = & gt; array ($ this, 'postValidate')))); }
Then you can do something like:
public function postValidate ($ validator, $ value) {$ today = date ("Ymd "); If (stratome ($ value ["start_date"]) & lt; strtotime ($ Today)) {$ error = new sfValidator error ($ verifier, "start date can not be earlier than today"); Throw new sfValidatorErrorSchema ($ verifier, array ('start_date' = & gt; $ error)); } {Strtotime ($ value ["start_date"]) gt; strtotime ($ value ["end_date"])) {// same verification error here} // etc ... ...
Comments
Post a Comment