Mokoala v4.0

§ Handling Forms » Handling Form Processing

To demonstrate handling MK_Form processing let's use the form from the first example.

$fields = array(
	'email' => array(
		'label' => 'Email',
		'type' => 'text',
		'validation' => array(
			'email' => array(),
			'instance' => array()
		)
	),
	'password' => array(
		'label' => 'Password',
		'type' => 'text',
		'validation' => array(
			'instance' => array(),
		),
		'attributes' => array(
			'type' => 'password'
		)
	),
	'login' => array(
		'type' => 'submit',
		'attributes' => array(
			'value' => 'Login'
		)
	)
);

$form = new MK_Form($fields);

print $form->render();

All we need do is call the MK_Form::isSuccessful() method. If the form has been submitted and all of the validation rules met then this method will return true, otherwise it'll return false. The code below will print a message if the form is successfully submitted, otherwise it will display the form, with validation errors.

if($form->isSuccessful()){
	print "<p>Form submitted successfully</p>";
}else{
	print $form->render();
}

Printing Form Field Values

We've covered how to detect if a form has been correctly submitted, but in this example we'll look at how to read a form's data. For consistency we'll continue the example from above. Don't worry, if you've lost track, the full code is at the bottom of the page!

In the below example we use the MK_Form::getField() method, which returns an instance of MK_Form_Field. We can then call MK_Form_Field::getValue() to return the field's value.

if($form->isSuccessful()){
	print "<p>Form submitted successfully</p>";
	print "<p>Email: ".$form->getField('email')->getValue()."</p>";
	print "<p>Password: ".$form->getField('password')->getValue()."</p>";
}else{
	print $form->render();
}

Summary

We've covered quite a lot there. Below if the full code example.

$fields = array(
	'email' => array(
		'label' => 'Email',
		'type' => 'text',
		'validation' => array(
			'email' => array(),
			'instance' => array()
		)
	),
	'password' => array(
		'label' => 'Password',
		'type' => 'text',
		'validation' => array(
			'instance' => array(),
		),
		'attributes' => array(
			'type' => 'password'
		)
	),
	'login' => array(
		'type' => 'submit',
		'attributes' => array(
			'value' => 'Login'
		)
	)
);

$form = new MK_Form($fields);

if($form->isSuccessful()){
	print "<p>Form submitted successfully</p>";
	print "<p>Email: ".$form->getField('email')->getValue()."</p>";
	print "<p>Password: ".$form->getField('password')->getValue()."</p>";
}else{
	print $form->render();
}

Authenticating Users

Basic Guides

Handling Forms

Social Sign In

Using the Dashboard

Working With Data

levitra online