Hello,

I am creating a MVC application.

Now I get the following error:

Catchable fatal error: Object of class PHP_Form could not be converted to string in /Applications/MAMP/htdocs/framing9/application/module/default/controller/user.php on line 67

This is the code in my controller

public function login()
    {
        // require the PHP_Form class
        Loader::load(PHP_Form);

    $form = new PHP_Form("myform");

	$element = new PHP_Form_Element_Text("field1","text","UPPERCASE only.");
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Element_Password("field2","password");
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Element_Textarea("field3","textarea");
	$element->cols = 50;
	$element->rows = 3;
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Element_Checkbox("field4","checkbox");
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Element_Select("field5","select");
	$element->option("","");
	$element->option("option1","Option 1");
	$element->option("option2","Option 2");
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Element_Select("field6","select (multiple)");
	$element->multiple = true;
	$element->option("option1","Option 1");
	$element->option("option2","Option 2");
	$element->option("option3","Option 3");
	$element->option("option4","Option 4");
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Element_Radio("field7","radio");
	$element->option("option1","Option 1");
	$element->option("option2","Option 2");
	$form->add($element);

	$form->newline();

	$element = new PHP_Form_Button("Button 1","Button1Clicked");
	$form->add($element);

	$form->run(); [B]//line 67[/B]
	$this->view->set("form","$form");

}

This is my form class code

class PHP_Form {
	var $form_class = "form";
	var $error_class = "form_error";
	var $cell_class_valid = "form_cell_valid";
	var $cell_class_invalid = "form_cell_invalid";
	var $label_class_valid = "form_label_valid";
	var $label_class_invalid = "form_label_invalid";
	var $element_class_valid = "form_element_valid";
	var $element_class_invalid = "form_element_invalid";
	var $description_class_valid = "form_description_valid";
	var $description_class_invalid = "form_description_invalid";

var $action;
var $autobreak = false;

var $error;

var $name;
var $layout;
var $row = 0;
var $data;
var $functions;
var $onLoad="";
var $submitted = false;
var $show = true;

function __construct($name="",$onLoad=""){
	$this->name = $name;
	$this->onLoad = $onLoad;
}

function validate(){
	$valid = true;
	if (count($this->layout) > 0){
		foreach($this->layout as $row){
			foreach($row as $element){ $valid = $element->validate(&$this->error) && $valid; }
		}
	}
	return $valid;
}

function populate(){
	if (count($this->layout) > 0){
		foreach($this->layout as $rowid => $row){
			foreach($row as $elementid => $element){
				if ($element->populate === true){ $this->layout[$rowid][$elementid]->value = str_replace("'","'",$this->data[$element->name]); }
			}
		}
	}
}

function add($element){
	$element->form = $this->name;
	if (!empty($this->cell_class_valid) && empty($element->cell_class_valid)){ $element->cell_class_valid = $this->cell_class_valid; }
	if (!empty($this->cell_class_invalid) && empty($element->cell_class_invalid)){ $element->cell_class_invalid = $this->cell_class_invalid; }
	if (!empty($this->label_class_valid) && empty($element->label_class_valid)){ $element->label_class_valid = $this->label_class_valid; }
	if (!empty($this->label_class_invalid) && empty($element->label_class_invalid)){ $element->label_class_invalid = $this->label_class_invalid; }
	if (!empty($this->element_class_valid) && empty($element->element_class_valid)){ $element->element_class_valid = $this->element_class_valid; }
	if (!empty($this->element_class_invalid) && empty($element->element_class_invalid)){ $element->element_class_invalid = $this->element_class_invalid; }
	if (!empty($this->description_class_valid) && empty($element->description_class_valid)){ $element->description_class_valid = $this->description_class_valid; }
	if (!empty($this->description_class_invalid) && empty($element->description_class_invalid)){ $element->description_class_invalid = $this->description_class_invalid; }
	$this->layout[$this->row][] = $element;
	if (get_class($element) == "form_button"){ $this->functions[$element->label] = $element->function; }
	if ($this->autobreak){ $this->row++; }
}

function newline(){
	$this->row++;
}

function show(){
	if ($this->show && (count($this->layout) > 0)){
		$action = empty($this->action) ? $_SERVER["REQUEST_URI"] : $this->action;
		echo "<form method='POST' action='$action'>\n";
		echo "<input type='hidden' name='form_".$this->name."_submitted' value='1'>\n";
		echo "<div".((!empty($this->form_class)) ? " class='".$this->form_class."'" : "").">\n";
		foreach($this->layout as $row){
			echo "<div>\n";
			foreach($row as $element){ echo $element->create()."\n"; }
			echo "<div STYLE='clear: both;'></div>\n";
			echo "</div>\n";
		}
		if (!empty($this->error)){ echo "<div".((!empty($this->error_class)) ? " class='".$this->error_class."'" : "").">".$this->error."</div>\n"; }
		echo "</div>\n";
		echo "</form>\n";
	}
}

function run(){
	$this->submitted = ($_POST["form_".$this->name."_submitted"] == 1) ? true : false;
	if ($this->submitted){
		$this->data = $_POST["form_".$this->name];
		$this->populate();
		if ($this->validate()){
			$function = $this->functions[$this->data["button"]];
			if (function_exists($function)){ $this->show = $function(&$this->data,&$this->error,&$this->error_message); }
		}
		$this->show();
	}else{
		if (function_exists($this->onLoad)){
			$function = $this->onLoad;
			$this->show = ($this->show && $function(&$this->data, &$this->error));
			$this->populate();
		}
		$this->show();
	}
}
}

The line in the controller for post to the view class

$this->view->set("form","$form");

Can someone tell me what the problem is? Where do I have to make a change?

Thanks in advance.

    Edit: My analysis was erroneous, so I've removed it (I didn't read the code closely enough).

       $this->view->set("form","$form"); 

      Why do you have $form in quotes?

        I feel compelled to point out that the program isn't working with it either....

        Its effect would be to turn the $form object into a string. Which would work if the object has a __toString() method. But if it doesn't, the result would be an error message of the form "Catchable fatal error: Object ... could not be converted to string".

          Is it possible the echo's in the form.class (function show()) give the problem? Because the echo is already in de view.class.

            Write a Reply...