Jump to content

Remove (required) field from a form


pwFoo
 Share

Recommended Posts

At first... I wish you all a happy new year!!!  :biggrin:

I testing the dev updates to form api (array input) and also would try to remove a required field before send / render the form.

In the past version(s) a previously added field couldn't simply removed if it was a required field. During form process the removed field input was checked because it's required... But the field was removed before render it...

Now I updated to the latest dev to test array field input and also the remove field from form behavior...

   // page to work with

    $refPage = $pages->get(1012);
    
    $form = $modules->get("InputfieldForm");
    
    // submit button as array
    $submitBtn = array(
 
            'id' => 'submit',
            'name' => 'submit', 
            'value' => 'Submit',
            'type' => 'InputfieldSubmit',
    );
 
    // add fields from a defined page $refPage and the submit button to form
    $form->add($refPage->getInputfields());
    $form->add($submitBtn);
 
    // modify the form: remove title field
    $toRemove = $form->get('title');
    $form->remove($toRemove);
    
    // render modified form - title field should be removed and no more required... but still rendered!!!
    $out = $form->render();

Why the title field isn't removed from the form  :blink: Is it a bug at PW dev? Or is there an error at my code?

Because field isn't removed at the moment I can't test if a removed required field is handled as required during form process... So that would be the second step to test  :rolleyes:

Link to comment
Share on other sites

As before I'll generate a field list before I add to form...

Instead of use getInputfields() I'll use a find to select / remove fields. 

$selectedFields = $refPage->fields->find('name=title|body|image, name!=title');

Selector part will be generated dynamic to make it flexible. 

Because PW form api works with array input I'll simplify my FormHelper to use the existing array syntax instead of my own.

Link to comment
Share on other sites

The easiest way to do that is:

$fieldsToIgnore = array('title');

$fields = $refPage->getInputfields();

foreach($fields as $f){
    if (!in_array($f->name, $fieldsToIgnore)){
            $form->append($f);
    }
}
  • Like 1
Link to comment
Share on other sites

Hi adrian,

thank's. I know this way and have decided to use it again / furthermore ;)

You're right, should be the easiest way to do it.

Tried another way which also works, but because of missing page / template context (I think...) there are some limitations (missing options,...) and so there should be no real gain compared to array created fields. 

Example from inside a testing module.

       $fields = $this->fields->find('name={$fieldSelector}, name!=title');    // title doesn't work!!!

        
        foreach ($fields as $field) {
            $inputfield = $modules->get($field->inputfieldClass);
            $form->add($inputfield);
        }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...