Yii Framework Forum: CForm - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

CForm Rate Topic: -----

#21 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 23 June 2009 - 07:27 PM

I don't see how this CForm would work for all situations.  Sometimes forms are more complicate then a series of fields one after another.  For instance,  I have a form where the first row has two inputs: a text field, and a checkbox on the right of it.  Then there are more form elements below this.  Like this:


Date:  _____ - or - <checkbox> N/A
Field2: _____
Etc:    _____


Using a template that loops through the elements and prints them does not make this possible.

And what might not be cool, is if someone set up there forms using CForm and then realize they need to make an adjustment that is not possible with CForm.  Then they need to re-write all there forms the "old" way.  I know people don't like typing out forms, but maybe that should be left to the yiic program
0

#22 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 23 June 2009 - 07:42 PM

You should be able to create the form view like this too, as apposed to looping through the elements:


<?php
$form->begin();

echo $element->label['fieldA'].': '.$element['fieldA']->render().' - or - '.$elements['fieldB']->render().' '.$elements['fieldB']->label;


$element = $elements['email'];
echo $element->label."<br/>".$element->render()."<br/>";

$form->end();
?>


0

#23 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,896
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 June 2009 - 07:47 PM

CForm can be rendered in two ways.

The first way is using a predefined form template. Its benefit is fast prototyping and can render forms in consistent design styles. Its drawback is that the rendering layout may be too rigid to be used for complicated forms.

The second way is explicitly rendering each element in CForm in a view, just like we currently do using CHtml active methods.

echo $form['username']=>renderLabel();
echo $form['username']=>renderInput();


0

#24 User is offline   zerone 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-June 09

Posted 23 June 2009 - 08:19 PM

Quote

CForm can be rendered in two ways.



The first way is using a predefined form template. Its benefit is fast prototyping and can render forms in consistent design styles. Its drawback is that the rendering layout may be too rigid to be used for complicated forms.



The second way is explicitly rendering each element in CForm in a view, just like we currently do using CHtml active methods.


echo $form['username']=>renderLabel();
echo $form['username']=>renderInput();


Exactly. Also with optional arguments:


echo $form['username']=>renderLabel('optional new label');
echo $form['username']=>renderInput('optional initial value');


I vote for the widgets idea. It would give infinite extensible options.
And I think we can learn from Django:


class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)


Maybe using a factory:


class ContactForm extends CForm
{
    public subject = CWidget::text(array('max_length' => 100));
    public message = CWidget::textarea();
    public sender = CWidget::email();
    public cc_myself = CWidget::boolean(false);
}


I personally don't like to throw it all with config files. They are not extensible, they are hard to port and other reasons..


0

#25 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 24 June 2009 - 02:00 AM

I totally dislike the idea of widgets. What could ever be added to them?

If widgets will get implemented, I hope CHtml will be still available.
0

#26 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 24 June 2009 - 07:09 AM

What about this?
Taken the Idea of Jonah of have CField (etc) do something like this:

Each C*Field can be related with a property of a model
Example:

<?php
$name = new CTextFied(array('model'=>$model,'property'=>'someProepertyOfModel',... otjer properties...)) ; // note that this can be done in a controller.


The class CForm may have a method named addFieds(), which receives an array of instances of CField (or subclass).

Then in the controller we can do:


<?php
$form = new CForm;
$form->addFields(array('name'=>$name, 'pass'=>$pass));
// etc.


And we can render the form (as Qiang says) in two ways

Quote

CForm can be rendered in two ways.



The first way is using a predefined form template. Its benefit is fast prototyping and can render forms in consistent design styles. Its drawback is that the rendering layout may be too rigid to be used for complicated forms.



The second way is explicitly rendering each element in CForm in a view, just like we currently do using CHtml active methods.



$form->begin()
echo $form->name=>renderLabel();
echo $form->pass=>renderInput();
$form->end();


0

#27 User is offline   zerone 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-June 09

Posted 24 June 2009 - 07:23 AM

Quote

I totally dislike the idea of widgets. What could ever be added to them?


? Widgets can be extended.. Also on your model you have a "filename" textfield.
How do you make the form display an uploadfile field? Hacks? Nah..

Make the Form be the link between a field in the model and a Widget which will represent that field.

Also with Widgets you can have multiple outputs for different page viewers (browser, mobile, iphone) The logic don't change, only the View layer.



0

#28 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 24 June 2009 - 09:44 AM

Quote

How do you make the form display an uploadfile field? Hacks? Nah..

Did you mean the file-type input? Why would they need an additional widget?

Quote

The logic don't change, only the View layer.

But I can change the view even without widgets.

Adding fundamentally new layers to this system makes totally no sense to me. Is robustness inevitable in open source softwares?

Can't these widgets be added as a separate extension?
0

#29 User is offline   zerone 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-June 09

Posted 24 June 2009 - 10:14 AM

So you say to limit the CForm to the "default" fields? And what is your proposal to define a new form class? And the output.
0

#30 User is offline   dalip 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 21-December 08

Posted 24 June 2009 - 01:23 PM

Widgets are more versatile. It would be easier to have a CEmailField class which extends the CTextField class + adds javascript email validation. Or have a CDatetimeField which shows a neat calendar through javascript. One widget can show multiple textfields for a birthdate for example, a field for the year/month/day.

I'm not saying CHtml form methods should be removed, many widgets might use those methods but for form generation it'll be much neater to have widgets.
0

#31 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 24 June 2009 - 02:24 PM

@zerone,

File type input *is* a default field. I agree with Qiang's basic proposal without widgets.

@dalip,

But client-side javascript (like calendar) is not something basic that should be integrated into the core package, is it?
0

#32 User is offline   zerone 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-June 09

Posted 24 June 2009 - 02:33 PM

Quote

@zerone,



File type input *is* a default field. I agree with Qiang's basic proposal without widgets.



@dalip,



But client-side javascript (like calendar) is not something basic that should be integrated into the core package, is it?


But how do you represent a filefield?!!

And dalip only wrote a few examples of extensions to widgets.
0

#33 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 24 June 2009 - 02:48 PM

Am I really getting something such wrong? :-[

Quote

But how do you represent a filefield?!!

Aren't we talking about <input type="file">?

Also, I didn't shoot everything that moves, just used dalip's examples as a reason not to include these widgets into core.
0

#34 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,896
  • Joined: 04-October 08
  • Location:DC, USA

Posted 24 June 2009 - 02:53 PM

peace...it seems there's some misunderstanding here. ;)

What in my mind currently is that we will reuse CHtml's active methods while still allows an element to use a widget. For example,

'elements'=>array(
    'username'=>array('type'=>'TextField'),
    'password'=>array('type'=>'MyFancyPasswordWidget'),
),


So 'username' will use CHtml::activeTextField() to render the input field while 'password' will use a widget. As a result, we will no longer need to implement widgets for those basic input fields since they are already provided by CHtml.
0

#35 User is offline   zerone 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 16-June 09

Posted 24 June 2009 - 03:07 PM

Will there be a way to pass values to the field/widget constructors?
0

#36 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,896
  • Joined: 04-October 08
  • Location:DC, USA

Posted 24 June 2009 - 03:10 PM

Yes, the name-value pairs (except some reserved names) in each array will be passed to the activeMethod's htmlOptions or widget's properties.
0

#37 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,896
  • Joined: 04-October 08
  • Location:DC, USA

Posted 25 June 2009 - 10:55 PM

An initial implementation is checked in. Using the blog demo as an example, the create action and view can be rewritten like the following:

actionCreate:

<?php
public function actionCreate()
{
$form=new PostForm($this, new Post);
if($form->submitted())
{
if($form->clicked('preview'))
$form->model->validate();
else if($form->clicked('create') && $form->model->save())
$this->redirect(array('show','id'=>$form->model->id));
}
$this->render('create',array('form'=>$form,'model'=>$form->model));
}


create.php

<?php
<h2>Create New Post</h2>

<?php echo $form->render(); ?>

<?php if($form->clicked('preview') && !$model->hasErrors()): ?>
...display preview here...


PostForm:

<?php
class PostForm extends CForm
{
public function init()
{
$spec=array(
'elements'=>array(
'title'=>array(
'type'=>'text',
'size'=>65,
'maxlength'=>128,
),
'content'=>array(
'type'=>'textarea',
'rows'=>20,
'cols'=>50,
),
'tags'=>array(
'type'=>'text',
'size'=>65,
),
'status'=>array(
'type'=>'dropdownlist',
'items'=>Post::model()->statusOptions,
),
),
'buttons'=>array(
'update'=>array(
'type'=>'submit',
'label'=>'Save',
'on'=>'update'
),
'create'=>array(
'type'=>'submit',
'label'=>'Create',
'on'=>'insert',
),
'preview'=>array(
'type'=>'submit',
'label'=>'Preview',
),
),
);
$this->configure($spec);
}
}


Features:

* Supports sub-forms and multiple models
* Supports widgets and plain strings to be embedded in the form
* Arbitrary manipulation of the whole form hierarchy, using the familiar array syntax. For example

<?php
$form['title']->size=100;


Pros:

* The _form.php partial view is eliminated, which is replaced by the single call $form->render().
* Code relevant to input configuration is separated from the rest of the presentational code.
* By customizing the base form's render() method, we can change the appearance of the forms in an application consistently and easily.

Cons:

* The form rendering logic is relatively rigid since it treats all kinds of forms the same.
* It introduces an additional layer of abstraction, which means additional learning curve.
0

#38 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 26 June 2009 - 04:26 AM

Quote

Pros:

* The _form.php partial view is eliminated, which is replaced by the single call $form->render().

Wow, it's elegant.  :o

Quote

Cons:

* It introduces an additional layer of abstraction, which means additional learning curve.

I think the learning effort is already high enough  ;) as the functionalities of the yii is so rich. We can use yiic or other automation in order to reduce the effort. So I am for CForm.
0

#39 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 26 June 2009 - 04:43 AM

@mocapapa,

Yes, it's elegant as long as you don't need customized layout.

Perhaps it would be a good idea to render the corresponding form class for each activerecord model during code generation.

@For those, who have not much experience with PHP:
  • In CForm subclasses, in init() you have to call $this->configure() with a single parameter, which is in array containing the specification (which can be stored elsewhere, even in configuration files, or as application parameter).
  • In the current implementation, output is rendered as follows: the main form and all its subforms are enclosed by fieldset tags; all visible input elements are shown as items of unordered lists.

0

#40 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,896
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 June 2009 - 06:31 AM

@pestaa: you already read the code! The render() method is not finished yet. Needs to make the default rendering more pretty.
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users