Set validation on field when display in form

Set validation on field when display in form

SarbastSarbast Posts: 64Questions: 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: 1,882Questions: 55Answers: 281

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

    And there is also an example on that page:

  • SarbastSarbast Posts: 64Questions: 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: 55,114Questions: 1Answers: 8,654 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: 64Questions: 0Answers: 0

    Hi Allan,

    Could you please give me an example.

    Thanks

  • allanallan Posts: 55,114Questions: 1Answers: 8,654 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.