how to return messages from process function on separate form class

Notice: This thread is very old.
alnux
Member | 106
+
0
-

hi, i made a separate form class extended from object class
BaseForm.php

<?php

namespace App\Libs\Forms;

use Nette\Object,
    Nette\Database\Context,
    Nette\Application\UI\Form;


use App\Libs\Containers\FormContainer;
/**
 * Base for Forms components
 */
class BaseForms extends Object
{

	protected $values;

	protected $form;

    protected $model;

    protected $postId;

	public function __construct()
    {
        $this->form = new Form;
    }

    /**
     * Set up translation option on forms
     * @param Nette\Localization\ITranslator $translator
     */
    public function setTranslator($translator)
    {
    	$this->form->setTranslator($translator);
    }

    /**
     * Default process form
     */
    public function process()
	{
	   $this->values = $this->form->getValues();
    }

    //-- Custom rules ---------------------------------------------------------

    /**
     * Check if item is unique on table
     * @param  Form::addText $item  var catchs from form
     * @param  string $field In this case is 'key' or 'name'
     * @return boolean
     */
    public function unique($item,$field)
    {

        return !$this->model->unique($field,$item->value);
    }
    //-------------------------------------------------------------------------
}

PrivilegeForm.php

<?php

namespace App\Modules\Admin\Forms;

use Nette\Application\UI\Form;

use App\Libs\Forms\BaseForms;

class PrivilegeForm extends BaseForms
{
	public function build($model, $postId=NULL)
	{

      $this->model = $model;
      $this->postId = $postId;

		$this->form->addProtection('forms.formtokenexpired');
    	$this->form->addText('key_name', 'forms.privilege.labels.key')
    	   ->setRequired('forms.privilege.required.key')
         ->setAttribute('placeholder', 'forms.privilege.attributes.placeholder.key')
         ->addRule(Form::MAX_LENGTH, 'forms.privilege.rules.length.key',64)
         ->addRule(Form::PATTERN, 'forms.privilege.rules.pattern.key','[a-z_-]+');

    	$this->form->addText('name', 'forms.privilege.labels.name')
    		->setRequired('forms.privilege.required.name')
         ->setAttribute('placeholder', 'forms.privilege.attributes.placeholder.name')
         ->addRule(Form::MAX_LENGTH, 'forms.privilege.rules.length.name',64)
         ->addRule(Form::PATTERN, 'forms.privilege.rules.pattern.name','[a-zA-ZñÑ ]+');

      $this->form->addTextArea('comment', 'forms.privilege.labels.comment')
         ->setAttribute('rows', 4)
        	->setAttribute('placeholder', 'forms.privilege.attributes.placeholder.comment')
         ->addRule(Form::MAX_LENGTH, 'forms.privilege.rules.length.comment',250)
         ->addRule(Form::PATTERN, 'forms.privilege.rules.pattern.comment','[a-zA-ZñÑ ]*');


      if(!$this->postId)
      {
         $this->form['key_name']->addRule(callback($this,'unique'), 'Este codigo de privilegio ya existe','key_name');
         $this->form['name']->addRule(callback($this,'unique'), 'Este nombre de privilegio ya existe','name');
         $this->form->addSubmit('enviar', 'Crear Privilegio');
      }
        /*else
        {
            $this->form['key_name']->addRule(callback($this,'editarExceptuando'), 'Este codigo de privilegio ya existe',array('campo'=>'key_name','id'=>$this->getParameter('postId')));
            $this->form['name']->addRule(callback($this,'editarExceptuando'), 'Este nombre de privilegio ya existe',array('campo'=>'name','id'=>$this->getParameter('postId')));
            $this->form->addSubmit('enviar', 'Editar Privilegio');
        }*/

      $this->form->onSuccess[] = callback($this, 'process');
       return $this->form;
	}

    public function process()
    {

      parent::process();
      $values = $this->form->getValues();

      if($this->form->isValid())
      {
         if ($this->postId)
         {
            $post = $this->model->get($postId);

            //$post->update($values);
            $message = 'Los datos del privilegio fueron actualizados';
         }
         else
         {

            //$post = $this->model->add($values);
            $message = 'El privilegio se creo con exito';
            //var_dump($message);
         }

         $this->flashMessage($message, 'success'); //--------this does not work
         //$this->redirect('Privilege:default');
      }
      else
      {
         //$this->flashMessage('No se pudieron guardar los datos', 'danger');
         //$this->redirect('Privilege:default');
      }

    }
}

im trying to return the message, as you know flashmessage method is part of Control class so i cant use this. How can i return the $message to presenter or if you have a suggestion of hoe i can render message on redirect page much better

here my presenter form component

	protected function createComponentNewEdit()
	{
	$privilegeForm = $this->form->getPrivilegeForm();
$privilegeForm->setTranslator($this->translator);
//dump($privilegeForm->build($this->privileges, $this->postId));
return $privilegeForm->build($this->privileges, $this->postId);
	}

thanks for all

Last edited by alnux (2014-06-30 01:19)

amik
Member | 118
+
0
-

There are two basic options:

  1. when creating your custom form, make it descendant of UI\Form, not a new class that holds the form as a property. Then, as UI\Form is PresenterComponent, you can call $this->getPresenter()->flashMessage()
  2. Store the message somewhere, then add another onSuccess[] handler in presenter:
class PrivilegeForm extends Object {
	public $message;
	...
	...
	public function process() {
		...
		...
		$this->message = 'any-message';
	}
}

protected function createComponentNewEdit()
{
	$privilegeForm = $this->form->getPrivilegeForm();
	...
	$privilegeForm->onSuccess[] = function() {
		$this->flashMessage($this->form->message);
		$this->redirect('Privileges:default');
	}
}

By the way, calling isValid() in process() has no point, as onSuccess[] event is raised only when form is valid.