Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:35:07+00:00 2026-06-13T17:35:07+00:00

I have this search function : public static function search_form() { $form = new

  • 0

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]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:35:07+00:00Added an answer on June 13, 2026 at 5:35 pm

    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]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a similar code snippet like this class Search { public function search($for,
I have a class like this: class someClass { public static function getBy($method,$value) {
For example i have this function that search for files: public void Search(string strExtension,
I have code like this: function search_keyword(){ $keyword = $this->db->escape_like_str(trim($_POST['keyword'])); $sql = ( SELECT
I have code like this: function search_keyword(){ $keyword = trim($_POST['keyword']); $search_explode = explode( ,
I have code like this: function search_keyword(){ $keyword = trim($_POST['keyword']); $search_explode = explode( ,
hai I have a magento web site. In this the search function compare with
i'm using autocomplete from jquery... and i have this: $('#name_search').autocomplete({ source: search/name.php, open: function(){
I have this configuration: A page containing a search field, at the submit in
I have some troubles in dealing with this search() function that i wrote. Well,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.