How to use form validation with the CRUD component in AgileToolkit4?

1k Views Asked by At

everyone. I've started using atk4 in a personal project a couple weeks ago and have been facing some difficulties since then. This specific question I want to ask is about how to make form validations when using the CRUD component shipped with the atk4 framework.

I have already tried several different solutions, none of them solving my problem.

I have a feeling that the problem here might be that the form validation happens within the call of the method $form->isSubmitted() (am I correct?). Since when using a CRUD component within a Page we don't use that way of processing the form submission, we'd have to find alternatives to it. For example, let's say I have a Page with the following init() function:

function init() {
    parent::init();

    // create a CRUD and set a model to it
    $crud = $this->add('CRUD');
    $m = $crud->setModel('Person');

    if ($crud->form) {
        $fn = $crud->form->getField('first_name');
        $fn->validateNotNull('The first name must not be empty.');
    }
}

Even though I've added the validation to the first name field, it won't be validated. I've tried several things, unsuccessfully. I tried to extend the CRUD class and reimplement the formSubmit($form) function, adding the validation there. Even if I do it, it doesn't work.

Originally (in the CRUD class), there is the function:

function formSubmit($form){
    $form->update();
    $this->api->addHook('pre-render',array($this,'formSubmitSuccess'));
}

I tried to iterate through the form's fields and call its validate() method, but it didn't work. Also, if I try to do alter the function (in a MyCRUD class, let's say) like below,

function formSubmit($form){
    if ($form->isSubmitted()) {
        $form->update();
        $this->api->addHook('pre-render',array($this,'formSubmitSuccess'));
    }
}

there happens an infinite loop... Could someone help me out?


[EDIT]

One last question intimately related to this one. I've just tried to do the exact same validation proposed by romanish below but, instead of adding a CRUD to a page, I was just adding a Form, and it doesn't work -- though the CRUD does work. Instead, there happens a "Error in AJAX response: SyntaxError: Unexpected token

1

There are 1 best solutions below

2
On

CRUD component respects the validation you're doing inside the model. When data is entered into the form and button is clicked, $model->update() is called.

The execution continues into beforeUpdate() hook, which is the one you need to intercept.

http://agiletoolkit.org/learn/understand/model/actions

class Model_Book extends Model_Table {
  function init(){
    parent::init();
    // .... more definitions ...

    $this->addHook('beforeSave',$this);
  }

  function beforeSave(){
    if(strlen($this['book_name']<10))
       throw $this->exception('Name of the book is too short')
            ->setField('book_name');
}

If model is unable to save itself and will produce exception, Form automatically show it as a field error.