Lançado Adianti Framework 8.1!
Clique aqui para saber mais
Validação em tempo real de execução com TRequiredValidator() Olá pessoal. Seguindo os meus testes buscando mais conhecimento, como já postei anteriormente uma dúvida (pode ser útil a alguém e quem puder me ajude - http://www.adianti.com.br/forum/pt/view_3918?problema-com-onchangeradio-no-classe-habilita-desabilita-campo), também quero fazer validações aproveitando TRequiredValidator(), só que em tempo de execução, ou seja, ao teclar T...
HB
Validação em tempo real de execução com TRequiredValidator()  
Olá pessoal.
Seguindo os meus testes buscando mais conhecimento, como já postei anteriormente uma dúvida (pode ser útil a alguém e quem puder me ajude - www.adianti.com.br/forum/pt/view_3918?problema-com-onchangeradio-no-),
também quero fazer validações aproveitando TRequiredValidator(), só que em tempo de execução, ou seja, ao teclar TAB pelo campo, gostaria de ativar TRequiredValidator() no exato momento da passagem pelo campo.
Já vi no tutor e também no livro que existe o método onExitAction, mais confesso que ainda tenho dificuldade em entender esse método e conseguir implementar o TRequiredValidator().
Gostaria de saber se é possível implementar o TRequiredValidator() em tempo de execução, pois, até agora só vi funcionar com o onSave no momento de salvar os dados do formulário.
No meu exemplo abaixo, eu gostaria que ao teclar TAB no campo $campo1 = new TEntry('campo1'), caso não seja preenchido, a aplicação devolve a mensagem de obrigatoriedade do preenchimento.
Podem me ajudar a implementar essa validação?

Código Completo
  1. <?php
  2. /**
  3.  * TestedoisForm Form
  4.  * @author  <your name here>
  5.  */
  6. class TestedoisForm extends TPage
  7. {
  8.     protected $form// form
  9.     
  10.     /**
  11.      * Form constructor
  12.      * @param $param Request
  13.      */
  14.     public function __construct$param )
  15.     {
  16.         parent::__construct();
  17.         
  18.         // creates the form
  19.         $this->form = new TQuickForm('form_Testedois');
  20.         $this->form->class 'tform'// change CSS class
  21.         $this->form = new BootstrapFormWrapper($this->form);
  22.         $this->form->style 'display: table;width:100%'// change style
  23.         
  24.         // define the form title
  25.         $this->form->setFormTitle('Testedois');
  26.         
  27.         // create the form fields
  28.         $id     = new TEntry('id');
  29.         $campo1 = new TEntry('campo1');
  30.         $campo2 = new TEntry('campo2');
  31.         $campo3 = new TEntry('campo3');
  32.         $campo4 = new TEntry('campo4');
  33.         //$testeum_id = new TEntry('testeum_id');
  34.         
  35.         $radio_enable = new TRadioGroup('testeum_id');
  36.         $radio_enable->addItems(array('1'=>'Grupo 1''2'=>'Grupo 2'));
  37.         $radio_enable->setLayout('horizontal');
  38.         $radio_enable->setValue(1);
  39.         // add the fields
  40.         $this->form->addQuickField('Id',      $id,  '10%' );
  41.         $this->form->addQuickField('',        $radio_enable80);
  42.         $this->form->addQuickField('Grupo 1'$campo1,  '40%' );
  43.         $this->form->addQuickField('',        $campo2,  '40%' );
  44.         $this->form->addQuickField('Grupo 2'$campo3,  '40%' );
  45.         $this->form->addQuickField('',        $campo4,  '40%' );
  46.         //$this->form->addQuickField('Testeum Id', $testeum_id,  '50%' );
  47.         
  48.         
  49.         $radio_enable->setChangeAction( new TAction( array($this'onChangeRadio')) );
  50.         self::onChangeRadio( array('testeum_id'=>1) );
  51.         if (!empty($id))
  52.         {
  53.             $id->setEditable(FALSE);
  54.         }
  55.         
  56.         /** samples
  57.          $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  58.          $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  59.          $fieldX->setSize( 100, 40 ); // set size
  60.          **/
  61.          
  62.         // create the form actions
  63.         $btn $this->form->addQuickAction(_t('Save'), new TAction(array($this'onSave')), 'fa:floppy-o');
  64.         $btn->class 'btn btn-sm btn-primary';
  65.         $this->form->addQuickAction(_t('New'),  new TAction(array($this'onClear')), 'bs:plus-sign green');
  66.         
  67.         // vertical box container
  68.         $container = new TVBox;
  69.         $container->style 'width: 90%';
  70.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  71.         $container->add(TPanelGroup::pack('Teste Habilita / Desabilita Campos'$this->form));
  72.         
  73.         parent::add($container);
  74.     }
  75.     /**
  76.      * Save form data
  77.      * @param $param Request
  78.      */
  79.     public function onSave$param )
  80.     {
  81.         try
  82.         {
  83.             TTransaction::open('teste'); // open a transaction
  84.             
  85.             /**
  86.             // Enable Debug logger for SQL operations inside the transaction
  87.             TTransaction::setLogger(new TLoggerSTD); // standard output
  88.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  89.             **/
  90.             
  91.             $this->form->validate(); // validate form data
  92.             
  93.             $object = new Testedois;  // create an empty object
  94.             $data $this->form->getData(); // get form data as array
  95.             $object->fromArray( (array) $data); // load the object with data
  96.             $object->store(); // save the object
  97.             
  98.             // get the generated id
  99.             $data->id $object->id;
  100.             
  101.             $this->form->setData($data); // fill form data
  102.             TTransaction::close(); // close the transaction
  103.             
  104.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  105.         }
  106.         catch (Exception $e// in case of exception
  107.         {
  108.             new TMessage('error'$e->getMessage()); // shows the exception error message
  109.             $this->form->setData$this->form->getData() ); // keep form data
  110.             TTransaction::rollback(); // undo all pending operations
  111.         }
  112.     }
  113.     
  114.     /**
  115.      * Clear form data
  116.      * @param $param Request
  117.      */
  118.     public function onClear$param )
  119.     {
  120.         $this->form->clear(TRUE);
  121.     }
  122.     
  123.     /**
  124.      * Load object to form data
  125.      * @param $param Request
  126.      */
  127.     public function onEdit$param )
  128.     {
  129.         try
  130.         {
  131.             if (isset($param['key']))
  132.             {
  133.                 $key $param['key'];  // get the parameter $key
  134.                 TTransaction::open('teste'); // open a transaction
  135.                 $object = new Testedois($key); // instantiates the Active Record
  136.                 
  137.                 $this->form->setData($object); // fill the form
  138.                 TTransaction::close(); // close the transaction
  139.             }
  140.             else
  141.             {
  142.                 $this->form->clear(TRUE);
  143.             }
  144.         }
  145.         catch (Exception $e// in case of exception
  146.         {
  147.             new TMessage('error'$e->getMessage()); // shows the exception error message
  148.             TTransaction::rollback(); // undo all pending operations
  149.         }
  150.     }
  151.     
  152.     
  153.     /**
  154.      * on ChangeRadio change
  155.      * @param $param Action parameters
  156.      */
  157.     public static function onChangeRadio($param)
  158.     {
  159.         if ($param['testeum_id'] == 1)
  160.         {
  161.             TEntry::enableField('form_Testedois''campo1');
  162.             TEntry::enableField('form_Testedois''campo2');
  163.             
  164.             TEntry::disableField('form_Testedois''campo3');
  165.             TEntry::disableField('form_Testedois''campo4');
  166.             
  167.             TEntry::clearField('form_Testedois''campo3');
  168.             TEntry::clearField('form_Testedois''campo4');
  169.         }
  170.         else
  171.         {
  172.             TEntry::disableField('form_Testedois''campo1');
  173.             TEntry::disableField('form_Testedois''campo2');
  174.             
  175.             TEntry::enableField('form_Testedois''campo3');
  176.             TEntry::enableField('form_Testedois''campo4');
  177.             
  178.             TEntry::clearField('form_Testedois''campo1');
  179.             TEntry::clearField('form_Testedois''campo2');
  180.         }
  181.     }
  182.         
  183. }
  184. ?>


Obrigado,
José Humberto Júnior

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (2)


HB

Aí pessoal, alguém pode dar uma força aqui?
:)
NR

Veja se o link abaixo ajuda:
adianti.com.br/forum/pt/view_661?adicionando-validacoes-em-formulari