/** * Custom validator for this form * No validation is provided by the Form Class, * that's for the user to define using whatever validation methods they want within this method * @param $fieldName * @param $value * @return bool */ public function validate($fieldName, $value) { if ($fieldName == 'name' && empty($value)) { $this->getFields($fieldName)->addErrorMessage('This field is required.'); return false; } if ($fieldName == 'terms' && !$value) { $this->getFields($fieldName)->addErrorMessage('You must agree with the terms and conditions to continue.'); return false; } //valid! return parent::validate($fieldName, $value); }
} /** * alternative to: MyCustomElement::addTemplate('myfield', '<p>this is my custom field - {{value}}</p>'); * @return string */ public function getTemplate() { //return a custom template when the new Element type is being built if ($this->getType() == 'myfield') { return "<p data-name='{{name}}' id='{{id}}' class='{{class}}' {{attributes}} >\n This is the template of the custom Element type named 'myfield' - <strong>{{value}}</strong></p>"; } return parent::getTemplate(); } } //end extended class $inlineForm = Form::Create('inline_form'); //update the form to use the extended Element class for building its fields $inlineForm->setCustomElementBuilder('\\MyCustomElement'); //add some fields to the form $inlineForm->addField('name', 'text', array('label' => 'Your Name', 'class' => ['class1', 'class2'], 'attributes' => ['required', 'title' => 'Your Name', 'placeholder' => '...']))->addField('gender', 'radio', array('options' => ['m' => 'Male', 'f' => 'Female'], 'attributes' => ['required']))->addField('my_custom_field', 'myfield', array('label' => false, 'value' => 'This is the value of this field', 'attributes' => ['style' => "color:red;"])); $inlineForm->addField('save', 'submit', ['value' => 'Submit']); //disable html5 validator for this form $inlineForm->setAttributes(['novalidate']); //handle POST if (isset($_POST[$inlineForm->getName()])) { //populates the form with posted data $inlineForm->handlePostRequest(); //validate the form using our custom validator defined above if ($inlineForm->isValid()) { //success! echo $inlineForm->getName() . ' submitted valid data.<br/>';
/** * @param Form $form * @return string */ public function mncedimHtmlFormClose(Form $form) { $csrf = $form->getFields('_csrf'); return ($csrf ? $csrf->render() : '') . $form->close(); }