0

I have this simple code:

<form method="post" accept-charset="UTF-8">

    <input type="hidden" name="action" value="entries/saveEntry">
    <input type="hidden" name="redirect" value="services/{slug}">
    <input type="hidden" name="sectionId" value="3">
    <input type="hidden" name="enabled" value="1">

    Service <input type="text" name="fields[service]"><br>

    {# Entry field (required) #}
    Client <input type="text" name="fields[client][]"><br>

    <input type="submit" value="Submit">

</form>

{% if entry is defined %}
    <pre>{{ dump(entry.getErrors()) }}</pre>
{% endif %}

The client field is set as required.

Submitting the form with empty client field I get no validation error but a CDbException.

If I remove the line with the client input field I get a validation error as expected:

array(1) { ["client"]=> array(1) { [0]=> string(23) "Client cannot be blank." } }

What am I missing? How can I validate entry fields?

  • What was the CDbException error? – Luke Pearce Jul 14 '16 at 23:22
2

You'll need to make sure the entries title is set otherwise that will throw an error. You can do this using a field in your form or set it in the Control Panel to automatically be set using another field.

Currently you are setting the client field to save using an array with [client][] but the form input is set to text which only provides string values.

This means that when you leave the field blank it uses an empty array as it's value ( [""] ) hence why you don't get the required validation error.

If you want to provide an array of clients you'd need to use a different input type and also make sure that your client fieldType is set to something that can store arrays.

Code suggestion:

<form method="post" accept-charset="UTF-8">

  <input type="hidden" name="action" value="entries/saveEntry">
  <input type="hidden" name="redirect" value="services/{slug}">
  <input type="hidden" name="sectionId" value="3">
  <input type="hidden" name="enabled" value="1">

  Title
  <input type="text" name="title"><br>

  Service
  <input type="text" name="fields[service]"><br>

  Client 
  <input type="text" name="fields[client]"><br>

  <input type="submit" value="Submit">
</form>

{% if entry is defined %}
  <pre>{{ dump(entry.getErrors()) }}</pre>
{% endif %}
| improve this answer | |
  • I must use the input field name fields[client][] because it's the only way (I know) to write in an entry field. fields[client] does nothing. The big question is how can I validate such a field? – Christian Steger Jul 15 '16 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.