Welcome Guest, Not a member yet? Register   Sign In
set_value() not working on custom validation field
#1

[eluser]ci_user[/eluser]
set_value() not working on custom validation field. The posted value for the custom validation field comes back empty on page reload when validation fails.

The view:
Code:
$date = array(
'name' => 'date',
'value' => set_value('date'),
'id' => 'datepicker'
);
&lt;?=form_error('date');?&gt;<label>Date: </label>&lt;?=form_input($date);?&gt;

The controller:
Code:
$this->form_validation->set_rules('date', 'Date', 'trim|required|valid_date');
$this->form_validation->set_message('valid_date', 'The date format must be dd/mm/yyyy.');

My_Form_Validation:
Code:
public function valid_date($val)
    {
        if(!preg_match('^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$^', $val)){
return false;
     }
}
#2

[eluser]Aken[/eluser]
set_value() doesn't reset values, it repopulates a field with the POSTed value. Read the user guide for more info.
#3

[eluser]ci_user[/eluser]
Yes. I am trying to repopulate the field with the posted value. It comes back empty.
#4

[eluser]Aken[/eluser]
The problem is you aren't returning TRUE in your valid_date() callback if the field passes. This returns the NULL value, which the form validation library assumes as you altering the value rather than validating it (kind of like running trim() - it alters the value).

Do something like this instead:

Code:
public function valid_date($val)
{
    return (bool) preg_match('^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$^', $val);
}
#5

[eluser]Aken[/eluser]
Also, your date is validating a mm/dd/yyyy format, not dd/mm/yyyy like your error message says.
#6

[eluser]ci_user[/eluser]
That works! Thank you!

Incidentally, when I try to add [CLOSED] to the thread title I get: "Error Message Due to a heavy increase of spam within our community, the privilege of posting certain content is currently reserved for active community members and those who have purchased our software.
If you feel you have received this in error, please contact [email protected]"

What's the deal?
#7

[eluser]Aken[/eluser]
I think it's just a limitation based on your post count, dunno for sure, though. I wouldn't worry about it.
#8

[eluser]RaGe10940[/eluser]
[quote author="Aken" date="1343678951"]The problem is you aren't returning TRUE in your valid_date() callback if the field passes. This returns the NULL value, which the form validation library assumes as you altering the value rather than validating it (kind of like running trim() - it alters the value).

Do something like this instead:

Code:
public function valid_date($val)
{
    return (bool) preg_match('^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d$^', $val);
}
[/quote]

Just wanted you to realize you saved me days of work. I've had the issue where set_value would give a blank value. Well thats because I wasn't giving a return TRUE.

wow thank you so much!




Theme © iAndrew 2016 - Forum software by © MyBB