Required fields only after creation

Hello all,

I have the need to set a couple of fields required only after the creation of a record.

Is there a standard way to do it?

Thanks

Parents
  • I like 's suggestion, it is very clean.

    I have not tried this, but you do have the ability to set the required property in the field definition of the view.
    So if you have separate record.php and create.php views for that module you could try setting the field to required and then setting 'required' => false in the create.php this would ensure that it is required in record, list, preview etc if the record is edited there, but  perhaps not require it in the create.

    Note that this is a very view specific solution and could become cumbersome to manage as there may be multiple ways to edit the record after creation.

    As I said, I have not tried it.

    Another potential solution is to intercept the save action in the view's controller and check if it's an update, and if the mandatory fields are not filled then alert the user that the field/s are required to update the record and prevent the save until they are all taken care of. But this involves multiple controllers, one for each view where the record can be modified and saved: record view, list view, preview etc... which may, again, be quite cumbersome.

    Maybe you can find a way to implement Damien's solution with a condition on whether the ID is set? The id only exists after the first save.

    Francesca Flamigni Shiekh

Reply
  • I like 's suggestion, it is very clean.

    I have not tried this, but you do have the ability to set the required property in the field definition of the view.
    So if you have separate record.php and create.php views for that module you could try setting the field to required and then setting 'required' => false in the create.php this would ensure that it is required in record, list, preview etc if the record is edited there, but  perhaps not require it in the create.

    Note that this is a very view specific solution and could become cumbersome to manage as there may be multiple ways to edit the record after creation.

    As I said, I have not tried it.

    Another potential solution is to intercept the save action in the view's controller and check if it's an update, and if the mandatory fields are not filled then alert the user that the field/s are required to update the record and prevent the save until they are all taken care of. But this involves multiple controllers, one for each view where the record can be modified and saved: record view, list view, preview etc... which may, again, be quite cumbersome.

    Maybe you can find a way to implement Damien's solution with a condition on whether the ID is set? The id only exists after the first save.

    Francesca Flamigni Shiekh

Children