We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

[Volt] How can I set "required" parameter to select

I need to validate select field in volt file. I am not sure how can I set required field like this. Could you please suggest?

{{ select("title", titles, 'using': ['id', 'name'], 'required':'required') }}

This's not working.

What you mean it is not working? Just tested with your code and required attribute is added as intended.

<select id="title" name="title" required="required">

This will use browser's built in required validation. Perhaps you wanted to ask how to validate on the server side or?

edited Jun '17

Thanks for your reply. You mean I have to change my code from volt format to html? I have found that we have "required" parameter for text_field as below so I think select option should have it the same.

{{ text_field('name', 'required' : 'required') }}



93.7k
Accepted
answer

No, I don't mean that :)

Your browser sees the required attribute, but the validation does not fail because all your options have values.

<!-- Even if you did not select an option, browser auto selects the first one and the required attribute does not trigger warning -->
<select required>
    <option value="123">Test</option>
    <option value="444">Test 2</option>
</select>

<!-- If you have empty option as first, browser will trigger the required and throw warning -->
<select required>
    <option value="">- please choose -</option>
    <option value="123">Test</option>
    <option value="444">Test 2</option>
</select>

Can you achieve the secondary with Phalcon's Volt tag helper and using DB records.... I don't think so.

You will have to either using normal {% for %} to generate the options or modify the values array to contain this blank option.

Thank you Nikolay Mihaylov for your help. :) Now, I can solve this issue. Just add useEmpty and emptyText to select option as below.

{{ select ("title", titles, 'using': ['id', 'name'], required':'required', useEmpty':'true','emptyText':'------choose------') }}

cheers :)

Heh, never used selects liek you and didnt know about useEmpty and emptyText :) Thanks, learned something new!