Set validation on field when display in form

Set validation on field when display in form

SarbastSarbast Posts: 85Questions: 0Answers: 0

Hello,

I have two fields shows in the form according to a condition. I want to set validation "notEmpty" on these fields when show and off when hide.

Regards

Replies

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Use this event: https://editor.datatables.net/reference/event/initSubmit

    And there is also an example on that page:

  • SarbastSarbast Posts: 85Questions: 0Answers: 0
    edited November 2021

    Hello rf1234,

    I have looked at these examples, but it doesn't work for my scenario. I just want to disable "required" attribute with hidden fields.

    Regards

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin

    Hi,

    You'll need to use a server-side event (e.g. preCreate and preEdit in this case) to add a notEmpty validator to the correct field. You'll also need to know what the correct field is to be able to do that - I'm not sure what logic you are using to determine that though?

    Allan

    @rf1234 - Nice reference to Section 31 ;)

  • SarbastSarbast Posts: 85Questions: 0Answers: 0

    Hi Allan,

    Could you please give me an example.

    Thanks

  • allanallan Posts: 61,439Questions: 1Answers: 10,052 Site admin
    ->on('preCreate', function ($editor) {
      if (/* some condition */) {
        $editor->field('field1')->validator( Validate::notEmpty() );
      }
      else {
        $editor->field('field2')->validator( Validate::notEmpty() );
      }
    })
    

    Obviously it needs the logic for the condition filled in, I don't know what you need for that, the field names and any other customisation you need, but that's the basic structure. Use similar for preEdit.

    Allan

Sign In or Register to comment.