0

I'm still using 4.1 and I'm trying to center a form content on screen. When I add a new form it stays on the "left" of the screen.

I've tried to change form.html (temmplates/shared) adding a div with a style like this:

margin-left : 10%; margin-right : 10%; 

up next to the form tag and closing it at the bottom.

The form goes to the right but not centered at all.

Anyone has any clue to help with this ?

Here is a screenshot of a simple Form (it's not basec on models)

form example

thanks Alejandro

AJM.MARTINEZ
  • 477
  • 2
  • 10

3 Answers3

0

There are some examples of different form layouts here that you can use to adjust the form layout itself. Not sure from your question if you want to center the form in the page or centre the fields.

If you just want to centre the Form within the webpage, you want to use margin: auto for centering css rather than setting a percentage on each side as shown here

Also note, if you amend files from the atk4 directory, you should make a copy into yoursite/templates/shared and amend it there so you can upgrade by overwriting the atk4 directory later without losing anything.

Trevor North
  • 2,286
  • 1
  • 16
  • 19
  • Trevor, thanks for your response. I've tried to add that css after the tag, but the fields doesn't look good. What I really want is to center the form (mantaining its style and the fields ordered). I'm keeping trying – AJM.MARTINEZ Jul 14 '12 at 21:48
  • Please post the whole piece of code - i still think margin: auto; is the answer but just posting one line of code doesnt make it clear what the problem is. – Trevor North Jul 15 '12 at 14:25
  • Right now I'm using this "
    " . If you put this in form.html right after The elements of the form goes to the center but you see that something is not good.
    – AJM.MARTINEZ Jul 16 '12 at 09:17
0

If i use the default form template (in this case in a CRUD), i get the following

enter image description here

If i make your change to the atk4/templates/form.html by adding

<div style="width: 50%; margin: 0 auto;">

as the second line in form.html and adding the corresponding

</div>

one line from the bottom, it shifts everything slightly to the right as follows

enter image description here

What is unclear in your question is what you are trying to achieve - do you want to move the fields within the form or do you want to centre the whole form on the page ?

I think the reason for the shifting is because of setting the width to 50% but I dont know what layout you are trying to obtain - do you just want to right align the labels to the fields maybe ? Please post screenshots in the original question of what you are getting and try to describe what you want to achieve.

Trevor North
  • 2,286
  • 1
  • 16
  • 19
  • Trevor, very thanks for your response and your screens. I see the form in the left of the screen , I see for your capture that naturally the form is centered (not the fields, the whole form) in the screen. I see the complete form in the left of the screen. I really appreciate your – AJM.MARTINEZ Jul 17 '12 at 03:32
  • I have posted a screenshot of the form. You can see it's all to the left, not centered. Thanks again – AJM.MARTINEZ Jul 17 '12 at 03:50
  • OK - think you need to be looking at the template you are using for your page and give that a div with margin:auto. If your page is using full width of the browser, and you want to centre something, you could create a view with the div to centre and then add your form to the div - will post once i get the disk on my VM extended - just filled to 100% so cant provide you a working example yet. – Trevor North Jul 17 '12 at 11:44
  • Trevor thanks. I've the chance to see that example I would appreciate it. – AJM.MARTINEZ Jul 17 '12 at 16:05
0

Ok - i've gone to a default of atk4.2 to demonstrate.

If you add a new page with defaults as follows

<?php
   class page_test extends Page {
   function init() {
     parent::init();
     $p=$this;

     $f=$p->add('MVCForm')->setModel('Customer');
   } 
}
?>

I get the following and the fields extend across the whole width of the page.

enter image description here

This is because the default page template takes the whole width and is expected.

In order to adjust the form, you can use view functionality so create a view under the /lib/View directory called Centre.php and put the following code in it

<?php
class View_Centre extends View {
   function init(){
      parent::init();
   }

   function defaultTemplate() {
     return array('view/centre');
   }

}
?>

Then create a new template for the view in yoursite/templates/default/view called centre.html and insert the following html code

<div style='width:50%; margin: auto;'>
  <?$Content?>
</div>

and then in the page, we add the view first and the form into the view rather than straight into the page.

<?php
  class page_test extends Page {
    function init() {
      parent::init();
      $p=$this;

      $v=$p->add('View_Centre');
      $f=$v->add('MVCForm')->setModel('Customer');
    } 
  }
 ?>

and this results in the following web page

enter image description here

The base ATK4 form is itself a view which means you can style the form however you want so if you get for example use a different style of form such as the one described here you can do this by copying yoursite/atk4/atk4/templates/shared/form.html to yoursite/atk4/templates/shared.form.html and changing the second line from

 <?form?>
   <div id="<?$_name?>" class="atk-form <?$class?>" style="<?$style?>">
   <?$hint?>
   <form class="<?$form_internal_class?>" id="<?$form_name?>" name="<?$form_name?>" action="<?$form_action?>" method="POST" <?$enctype?>>
   <fieldset class="<?$fieldset?>">

to

 <?form?>
   <div id="stylized>" class="myform <?$class?>" style="<?$style?>">
   <?$hint?>
   <form class="<?$form_internal_class?>" id="<?$form_name?>" name="<?$form_name?>" action="<?$form_action?>" method="POST" <?$enctype?>>
    <fieldset class="<?$fieldset?>">

Create a new form.css file in yoursite/templates/default/css which contains the styling

Copy yoursite/atk4/templates/shared/shared.html to yoursite/templates/shared/shared.html and add an extra tag

  <?$css_include?> 

just above the existing

   <?$js_include?>

and in Frontend.php, let every page find the new css file.

    $this->addLocation('templates',array(
                       'css'=>array(
                         'default/css',
                        ),
          ));
          $this->template->appendHTML('css_include','<link type="text/css" href="'.$this->api->locateURL('css','form.css').'" rel="stylesheet">');

which results in a styled form like this

enter image description here

Trevor North
  • 2,286
  • 1
  • 16
  • 19