I have this search function :
public static function search_form()
{
$form = new Form('search_form');
$form->field('keyword', 'text', array
(
'min_length' => 4,
'max_length' => 15,
'alphanumeric' => lang('alphanumeric')
));
$form->field('category', 'select', array
(
'cat1' => 'Category 1',
'cat2' => 'Category 2',
'cat3' => 'Category 3'
), $value);
if($data = $form->validate())
{
header('Location: '.WEB.sprintf('search/'.$data['keyword'].'/'.$data['category']));
}
return $form;
}
And the optional validation, that works fine with inputs, but not with dropdowns :
// Validate
public function validate()
{
$this->script();
if(!$this->submitted)
{
return false;
}
$this->valid = true;
foreach($this->fields as $field)
{
$value = $this->request[$field[0]];
if(isset($field[2]['optional']) && $field[2]['optional'])
{
if($value == '') continue;
}
foreach($field[2] as $validator=>$data)
{
if($validator == 'optional') continue;
$custom = !method_exists($this, $validator);
if((!$custom && !$this->$validator($value, $data)) || ($custom && !$this->custom($data, $value)))
{
$this->valid = false;
$this->errors[$field[0]] = $this->error_message($field[0], $validator, $data);
break;
}
}
}
return $this->valid?$this->request:false;
}
When I add the optional parameter in an text input, it works, but not in the select input :
$form->field('category', 'select', array
(
'optional' => true,
.................
It is transformed as an value in HTML
Here is the select case :
# Input
switch($field[1])
{
case 'text':
case 'password':
if($this->submitted)
{
echo '<input id="'.$this->id.'_'.$field[0].'" class="text" type="'.$field[1].'" name="'.$field[0].'" value="'.htmlentities(utf8_decode($this->request[$field[0]]), ENT_QUOTES).'"/>';
} else
{
echo '<input id="'.$this->id.'_'.$field[0].'" class="text" type="'.$field[1].'" name="'.$field[0].'"'.(isset($field[3])?' value="'.$field[3].'"':'').'/>';
}
break;
case 'textarea':
echo '<textarea type="text" id="'.$this->id.'_'.$field[0].'" name="'.$field[0].'">';
echo '</textarea>';
break;
case 'select':
echo '<select id="'.$this->id.'_'.$field[0].'" name="'.$field[0].'">';
foreach($field[2] as $key=>$value)
{
echo '<option value="'.$key.'"';
if($this->submitted && $this->request[$field[0]] == $key)
{
echo ' selected="selected"';
} elseif(isset($field[3]) && $field[3] == $key)
{
echo ' selected="selected"';
}
echo '>'.$value.'</option>';
}
echo '</select>';
break;
}
The problem is that I need the validator for the first input (keyword) but not for the dropdown, and it seems that I have to validate both fields, which I don’t want. Is there a way to bypass the second validation ?
Edit : Actually, I don’t know why the dropdown doesn’t pass the validation, I just get the “error_category” when trying to submit the form.
Problem solved :
The validating function was actually trying to validate the second field ($field[2]) and looking for the [‘optional’] value, but couldn’t find it, as my second field was in fact the categories array.
To solve the problem, I just added a new array in second position, containing the “optional = true” and moved my actual array in third position.
The validation was then okay, but the option values didn’t showed up so I just had to change :
foreach($field[2] as $key=>$value)
{
echo '<option value="'.$key.'"'; ...
and replace $field[2] by $field[3]
Then just move the value field (previously $field[3]) to $field[4]
Problem solved :
The validating function was actually trying to validate the second field ($field[2]) and looking for the [‘optional‘] value, but couldn’t find it, as my second field was in fact the categories array. To solve the problem, I just added a new array in second position, containing the “optional = true” and moved my actual array in third position.
The validation was then okay, but the option values didn’t showed up so I just had to change :
and replace $field[2] by $field[3] Then just move the value field (previously $field[3]) to $field[4]