Yii Framework Forum: Processing form generated with CHtml - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Processing form generated with CHtml Rate Topic: -----

#1 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 07 December 2010 - 09:38 AM

Hi there,

Since I don't want to (can't) use model in generation of a form (explained here) and therefore since I can't take advantages of using CForm, CActiveForm or Form Builder, I wrote a simple (test version yet) form with using CHtml:

echo(CHtml::beginForm());
echo(CHtml::label('Name', 'name'));
echo(CHtml::textField('name', 'test'));
echo(CHtml::submitButton('Send!'));
echo(CHtml::endForm());

How to process such forms?

I tried to use old-school, not-object-oriented, not yii-aware approach of looking what is inside $_POST array:

echo('name='.$_POST['name'].'<br /><br />');

But this failed with PHP error saying: "Undefined index: name".

What am I missing?

Both documentation (guide / cookbook) and forum post seems to be very focused on building forms with using models and I can't find anything that would easily explain how to build and process forms generated with pure CHtml approach, like in above example.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#2 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 07 December 2010 - 10:05 AM

View PostTrejder, on 07 December 2010 - 09:38 AM, said:

What am I missing?

I've noticed that I was getting "Undefined index: name" error, because I had error_reporting = E_ALL | E_STRICT set in php.ini. After reverting it to error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED all went smudgily. After reverting it back to what it was (most strict error reporting mode) I had to change above line to:

if (isset($_POST['name'])) echo('name='.$_POST['name'].'<br /><br />');

But then again, question in post title remains. Is using good, old $_POST array the only (the best) solution for processing forms generated with using CHtml?
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#3 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 07 December 2010 - 11:13 AM

Hi Trej...

I am using it all the time when I require some specific features on my forms (not those automated by Yii)

There are plenty of ways to do so but, why not using the good Yii approach with models but applying them to what we want? -My english s*ks... let me code... :)


-on the view:

echo CHtml::textField('FORMNAME[fieldname]');

-on the controller:

if(isset($_POST['FORMNAME']))

In this way I just need to check once for the variable and create objects for validation, save, update... just like CActiveRecord does...

Just dropping ideas... you know me
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#4 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 08 December 2010 - 02:24 AM

Hi Antonio,

View PostAntonio Ramirez, on 07 December 2010 - 11:13 AM, said:

There are plenty of ways to do so but, why not using the good Yii approach with models but applying them to what we want? -My english s*ks... let me code...

:) It is explained in the post, to which you've already answered (here), why I don't want to (can't) use model in generation of a form.

View PostAntonio Ramirez, on 07 December 2010 - 11:13 AM, said:

-on the view:

echo CHtml::textField('FORMNAME[fieldname]');


-on the controller:

if(isset($_POST['FORMNAME']))

Thanks! That means I'm using a good approach! :)
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#5 User is offline   Vert 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 02-March 12

Posted 01 August 2012 - 10:26 AM

View PostAntonio Ramirez, on 07 December 2010 - 11:13 AM, said:

-on the view:

 echo CHtml::textField('FORMNAME[fieldname]');


-on the controller:

 if(isset($_POST['FORMNAME']))




Hi, with this solution, to get any attribute use the name set in the form:

$_POST['FORMNAME']['attribute_name']


Thanks, i found a way!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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