0

I have an ajax callback which returns a date type input field.

The problem is any other input type other than date is generated, but date type is not rendered at all. The title, and description is all generated, but the input field is not. Desperately, i have used the suffix tag to generate a date type field the good old html way. But i'm still concerned with if this is a bug or if im doing something wrong.

Here's the code

This one wont generate any fields, but will generate the title and description

function raffle_start_date_now($form, &$form_state)
{
    if($form_state['values']['raf_start_now'] == 0)
    {
        return  $form['raf_start_date'] = array(
                "#title" => "When to start this raffle?",
                "#description" => "Doesn't generate date input, but generates all other input",
                "#prefix" => "<div id='start_date_pick'>",
                "#suffix" => "</div>",
                "#required" => TRUE,
                "#type" => "date"
            );
    }
    else
    {
        return "<div id='start_date_pick'></div>";
    }

}

And changing the #type to "textfield" will render a textfield.

Currently, this is my workaround

function raffle_start_date_now($form, &$form_state)
{
    if($form_state['values']['raf_start_now'] == 0)
    {
        return  $form['raf_start_date'] = array(
                "#title" => "When to starz this raffle?",
                "#description" => "Doesn't generate date input, but generates all other input",
                "#prefix" => "<div id='start_date_pick'>",
                "#suffix" => "<input type='date' name='raf_start_date'/><br/></div>",
                "#required" => TRUE,
                "#type" => "date"
            );
    }
    else
    {
        return "<div id='start_date_pick'></div>";
    }

}
  • 1
    That AJAX callback is wrong. It's the form builder that changes the form element, not the AJAX callback. See the various examples api.drupal.org has. – kiamlaluno Jan 15 '16 at 18:39
  • I'm not able to understand the examples really well and not able to implement them for my usage either. I really don't understand the part where i have to make the form builder change the form element. Especially when most of the examples i've seen use prefix and suffix to replace an entire input where as i just want to append one. – Haider Ali Jan 15 '16 at 18:53
  • Also, you should show the form builder function, to get a more helpful answer. I am not sure what else we can add to the already existing examples, especially because you don't make clear what you don't understand. – kiamlaluno Jan 16 '16 at 3:42
  • @kiamlaluno I rewrote the form builder like this, its a tad bit ugly. notey.it/n/dDN4 – Haider Ali Jan 16 '16 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.