User Tools

Site Tools


fieldcomponent_class

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
fieldcomponent_class [2021/10/08 21:10] – created sahlfieldcomponent_class [2022/11/30 13:22] (current) – [Adding the component to the form] sahl
Line 59: Line 59:
 // $componentfield can now be added to a form // $componentfield can now be added to a form
 </code> </code>
 +
 +For convenience the form also have an addComponent-function that does exactly the same as above in one line:
 +
 +<code php>
 +$textcomponent = new TextComponent();
 +$form->addComponent('Label for field', 'field_name', $textcomponent);
 +</code>
 +
 +===== Backend field validation =====
 +
 +It is also possible to perform backend field validation inside the component. The FieldComponent class has a ''parse()'' function which can be overwritten, and functions exactly like the similar-named function in Field. FieldComponent also has the triggerError function to raise an error.
 +
 +So backend validation for the component above could look like: 
 +
 +<code php>
 +  function parse($value) : bool {
 +    if ($value == 'cherry') {
 +      $this->triggerError('You are not allowed to write \'cherry\' in this field!');
 +      return false;
 +    }
 +    $this->value = $value;
 +    return true;
 +  }
 +</code>
 +
fieldcomponent_class.1633727426.txt.gz · Last modified: 2021/10/08 21:10 by sahl

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki