views:

27

answers:

1

I am using PHP and jQuery for a project.

As some forms are reused across different functionality, I've coded a strategy class to render the forms. An example of one such class looks something like this:

class strategy_render_member_form()
{

     public function execute() {
      return '<form>
              <input type="text" name="user_name" id="user_name"></input> 
               //...snipped 
              <input type="text" name="dob" id="dob"></input>
              </form>';
     }
}

A controller feeds this output to a view

class controller_register()
   {
        public function show_form() {

              // ..snipped
              $view->form = $this->strategy_render_register_form->execute();


        }
   }

I wish to format the 'dob' textbox as a calendar, using jQuery's DatePicker. Now the question is - where should I add the JavaScript function to do so?

<script type='text/javascript'>
$(function() {
    $( "input#dob" ).datepicker();
</script>

Should the controller add it to the view? Should the strategy define it? Should I use a callback or hook to 'decorate' the form control?

+1  A: 

IMO, this is best kept isolated from PHP as a Progressive Enhancement. It belongs to the UI behavior layer, e.g. into your JavaScript files. I'd register the clickhandler unobtrusively on page load in an external JS file (think bootstrap for JS), along with any other JavaScript that enhances the UI.

Gordon