1

I want to use validateField with models but it says Method is not defined for this object!

I code like this:

$user_payment=$this->add("Model_Payment");
$user_payment->getField("amount")
                    ->validateNotNull()
                    ->validateField('($this->get())<=0','Please enter a posetive number!');
webelizer
  • 418
  • 2
  • 11

2 Answers2

3

You are trying to apply ->validateNotNull() to Model. This method should be applied to Field of a Form Like:

$f = $this->add('Form);
$f->setModel('Model_YourModel');

$f->getField("amount")
                ->validateNotNull()....
Kostiantyn
  • 1,792
  • 2
  • 16
  • 21
  • thanks but now I have another problem! I have same error for this code: $mwform=$mwr->add("Form"); $mwform->addField("Line","mwamount","Amount")->setFieldHint('Unit is $...'); $mwform->getField("mwamount") ->validateNotNull() ->validateField('($this->get())!=20','Please enter a posetive number!'); $mwform->addSubmit("submit"); – webelizer Sep 18 '13 at 15:20
  • What $mwr is? Usually we use $this->add('Form'); Or you can insert your form into the view like: $v = $this->add('View'); $f = $v->add('Form'); and so on – Kostiantyn Sep 19 '13 at 07:27
  • 1
    OH, and for the Form class use **->getElement();** I like to use it through variable luke: '$f = $this->add('Form') $ff = $f->addField('...'); $ff->validateNotNull();' It looks more nice for me but it's not required – Kostiantyn Sep 19 '13 at 07:37
2

There is no method getField() in Model_Table class.

You can call this method in Form olny

$f = $this->add('Form');
$f->setModel('Model_Payment');
$f->getField("amount")
       ->validateNotNull()
       ->validateField('($this->get())<=0','Please enter a posetive number!');

Check source of this method https://github.com/atk4/atk4/blob/master/lib/Form/Field.php#L235

Vadym
  • 749
  • 3
  • 15