Zend_Validate_Date returns true on 2011-02-31 - zend-framework

What should i do ?
$edit_end_date = '2011-02-31';
$validator_date = new Zend_Validate_Date(array('format' => 'yyyy-mm-dd'));
$isval = $validator_date->isValid($edit_end_date);
if((!$isval) || empty($edit_end_date))
{
echo "Please Enter Valid End Date. !";
}else{
echo "Entered Is Valid End Date. !";
}
how come it returns true date ?

According to the Zend API Docs, it appears that Zend_Validate_Date will only validate whether the argument passed to it, is a valid date construct (also considers locale), it will not validate if the date actually exists.
Zend_Validate_Date allows you to validate if a given value contains a date. This validator validates also localized input.
-- Edit --
Looks like you can use PHP's built in checkdate() function to determine if a date is valid or not.

There are bugs in data validation (ZF-7583 at issue tracker). Look at Zend_Validate_Date just doesn't work properly
You can use regex validation like in answer to linked question, but it will only check for syntax, not if date exists for real. For this you can use checkdate() - as Mike Purcell suggested - combined with Zend_Validate_Callback:
$validator1 = new Zend_Validate_Regex(
array('pattern' => '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/')
);
$validator1->setMessage(
"Date does not match the format 'yyyy-mm-dd'",
Zend_Validate_Regex::NOT_MATCH
);
$validator2 = new Zend_Validate_Callback(function($value)) {
// using checkdate() or other function
});

Related

Attempting to get the response from a TimeItem on my google form?

When I use getResponse(), it returns a string that doesn't even have the AM or PM value. Thus I cannot use the Date and Time functions on the response. Does someone know how to get my timeItem response to save properly.
I do know that I need to create a Date object in order to use the date and time functions. That's not the problem.
var tester = Responses[3].getResponseForItem(Items[7]).getResponse();
It returns at string like "3:00" when I would rather it return something like "3:00PM"
The TimeItem class is defined on the 24-hour clock. I don't think they explicitly provide AM and PM tags. Note, this is also the case with DateTimeItems.
Documenation: TimeItem
If you want the string to be 3:00PM/AM instead of 3:00, write a short conditional statement.
Something like:
if(timeItem > 12:00 AND timeItem < 23){
tester = tester + "PM";
} else {
tester = tester + "AM";
}

Codeigniter form validation callback rule issue

I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field.
The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message.
Any ideas on this?
EDIT:
This is the rule:
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');
And this is the password_check method:
function password_check($pwd) {
$containsLetterUC = preg_match('/[A-Z]/', $pwd);
$containsLetterLC = preg_match('/[a-z]/', $pwd);
$containsDigit = preg_match('/\d/', $pwd);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);
if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
$this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
return FALSE;
}
return TRUE;
}
The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.
Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).
Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.
There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077

CakePHP FormHelper Input Date

I have following question. When I use this Helper:
echo $this->Form->input('start_date', array('required'=>false, 'class'=>'form-control date'));
I get following output:
Where can I change this output type? I tried it in
/lib/Cake/View/Helper/FormHelper.php
I found in this lib file, that the Helper gets the function __getInput() and in the date case, following sub function:
case 'date':
$options['value'] = $selected;
return $this->dateTime($fieldName, $dateFormat, null, $options);
But in the function dateTime() I got lost. Is there any updated Helper out or is there a simple trick to change the HTML-output format?
Thanks & regards
Set input type as text
echo $this->Form->input('start_date', array('type'=>'text','required'=>false, 'class'=>'form-control date'));

how do i define an isset($_GET['id']) statement in an $_POST update query

I am carrying 'ID' over from a display table and want to store it in a variable called employee_display_id. I am aware that if you do not create the below function...
if(sset($_GET['id'])){
$employee_display_id = $_GET['iod'];
}
and your proceed to use the '$employee_display_id' variable, you receive an undefined variable error.
How do you incorporate the...
if(sset($_GET['id'])){
$employee_display_id = $_GET['iod'];
}
into...
if(isset($_POST['update']))
{
$updatename = htmlentities(strip_tags(mysql_real_escape_string($_POST['update_name'])));
$updateusername = htmlentities(strip_tags(mysql_real_escape_string($_POST['update_username'])));
echo $update_query = "UPDATE `employees` SET `name`='$updatename', `username`='$updateusername' WHERE `employee_id`='4'";/* $employee_display_id */
$update_result = mysql_query($update_query);
}
else
{
echo mysql_error();
}
so i can use the variable in the WHERE clause of the UPDATE query??
You are checking isset for $_GET['id'] and assigning $_GET['iod']
itz "iod" yu are assigning. Are you sure this variable assigned a value ???
Nazneen has a point. Double-check your variable names.
Also, do you know the difference between GET and POST? How they work?
Presumably, you want to update the employee data from an update link in a table? So your link would be something like http://example.com/update.php?id=6
Well then, in this example, GET['id'] would return 6, which you can then use with your code and pass it into your SQL query.
Example:
if(isset($_POST['update']))
{
if(isset($_GET['id'])){
$employee_display_id = $_GET['id'];
}
if(isset($_POST['update_name'])){
$updatename = htmlentities(strip_tags(mysql_real_escape_string($_POST['update_name'])));
}
// and so on...
}
Does that answer your question?
You could use a shorthand if to determine if the variable is set within the update command, but you need to set it to something or the query will not operate as you expect.
I would recommend checking if the variable is set prior to the SQL. If it's not set don't perform the query.
if(isset($employee_display_id)){
echo $update_query = "UPDATE `employees` SET `name`='$updatename', `username`='$updateusername' WHERE `employee_id`=" . $employee_display_id;
$update_result = mysql_query($update_query);
} else {
echo "ID not set."
}

Why does ColdFusion think that the value "7+" is a valid integer value, and how I can validate that it is not?

I have a form for users to input quantities. The form has client-side validation to ensure that the value is an integer and within a given range. The action page has server-side validation to ensure that the value is an integer and greater than zero.
However, one type of value gets through the validation and is causing my INSERT/UPDATE queries to throw exceptions. That value is an integer with a plus-sign - ie "7+" or "12+".
When such a value is entered, the ColdFusion-generated JavaScript validation throws a JavaScript error:
_CF_checkformAddToCart = function(_CF_this)
{
//reset on submit
_CF_error_exists = false;
_CF_error_messages = new Array();
_CF_error_fields = new Object();
_CF_FirstErrorField = null;
//form element itemQuantity 'INTEGER' validation checks
if (!_CF_checkinteger(_CF_this['itemQuantity'].value, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
//form element itemQuantity 'RANGE' validation checks
if (!_CF_checkrange(_CF_this['itemQuantity'].value, 0.0,500.0, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
}
Once I cancel out of the error popup, it goes to the action page, where I [try to] validate the value like so:
<cfif IsValid("integer", form.itemQuantity) AND form.itemQuantity GT 0>
<cfquery>
INSERT ....
However, if try this...
<cfset x = Int("7+") />
...ColdFusion throws an error.
Is it an integer or not ColdFusion???
How can get around this and validate my form input correctly?
isNumeric(form.itemQuantity) will return false for "7+", so to fully validate your input as an int, you can do this
<cfif isNumeric(form.itemQuantity) and IsValid("integer", form.itemQuantity) AND form.itemQuantity GT 0>
Due to the weird and wonderful nature of ColdFusion being typeless. It doesn't know what type of data you are working with and it tries to guess.
Its evaluating that 7+ is a valid. The validation built into ColdFusion makes a lot of assumptions and guesses.
My advise would be to not use it and to write your own validation routines that can be enhanced to do whatever you require.
For example
A user enters
2,075
Is this valid or invalid. Well if you have your own validation you can decide, you can say sure this is an integer and remote the , or you can say no they can't do that.
It's a small investment upfront that will pay off in the long run.
Turns out I can use LSParseNumber() to convert it to valid integer. So, now I'm testing for a valid integer, then resetting it using LSParseNumber() before attempting any database inserts:
<cfset addItemQty = 0 />
<cfif IsValid("integer", Trim(form.itemQuantity))>
<cfset addItemQty = LSParseNumber(Trim(form.itemQuantity)) />
</cfif>
I guess I'll have to re-engineer the front-end client-side validation to properly validate.