Setting Text Field Attribute: 'required'

Setting Text Field Attribute: 'required'

blabablabablabablaba Posts: 47Questions: 19Answers: 0
edited January 2018 in Free community support

Hi,

Referring to:
https://editor.datatables.net/reference/field/text

The document suggests that input attributes are set with the attr object which expects key / value pairs. This works well for 'maxlength' and 'placeholder':

        fields: [
            {
                label: "test:",
                name: "test",
                type: "text",
                attr: {
                    maxlength: '5',
                    placeholder: 'test input',
                    required: ?????????????????
                }
            }
        ]

However attribute 'required' does not exist as an attribute/value pair in the same way. I want to achieve the following:

<input name="test" maxlength="5" placeholder="test input" required>

How should I set the attr object to achieve an input with 'required' as above?

Steve

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394
    Answer ✓

    There is a "required" validator for Editor fields.
    https://editor.datatables.net/manual/php/validation

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin
    Answer ✓

    I've just tried it here and it seems to work as expected. Since its an HTML attribute you could also use required (i.e. as a string).

    Allan

  • Jack TingJack Ting Posts: 14Questions: 3Answers: 0
    edited May 2019

    The demo code doesn't work, currently!!
    Why??

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    @Jack Ting , which demo code is that? Can you give more information, please?

  • elias.saelias.sa Posts: 1Questions: 0Answers: 0

    The example given by @allan is not working correctly. I can still add new entries to the table without filling out the required fields.

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Hi @elias.sa ,

    Ah, I see. It seems it's honouring the other HTML attributes, see here, it just seems to have an issue with required. I've raised it internally (DD-891 for my reference) and we'll report back here when there's an update.

    Cheers,

    Colin

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin
    edited May 2019

    It is actually being set in the demo I have above, but there is no validation on the form, which is why it is effectively ignored. You need to use server-side validation (or initSubmit / preSubmit) even if you want to do client-side validation.

    Allan

This discussion has been closed.