$form->add('checkbox_test', 'Checkbox Test', array( 'type' => 'checkbox', 'options' => array('3' => 'Three', '4' => 'Four'), ), array( array('required') ) ); $form->add('radio_test', 'Radio', array( 'type' => 'radio', 'options' => array('5' => 'Five', '6' => 'Six'), ), array( ) );
$fieldset->add('roles', 'Roles', array( 'type' => 'checkbox', 'options' => array( 'one' => 'one', 'two' => 'two' ) ));
public static function set_form_fields(Fieldset $form, $user_id = null) { // Add my validation class in order to use callbacks strong_password and unique $val = $form->validation(); $val->add_callable('validation'); $form->add('first_name', 'First Name', array( 'type' => 'text', 'value' => ! empty($admin['first_name']) ? $admin['first_name'] : '', 'id' => 'first-name', 'size' => 50, ), array( array('required'), array('trim'), array('max_length', 50), ) ); $form->add('submit', null, array( 'type' => 'submit', 'value' => 'Submit' ) ); }
$fieldset->add('roles', 'Roles', array( 'type' => 'checkbox', 'options' => array( 'one' => 'one', 'two' => 'two' ), 'value' => array('one') )
Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'Security' => APPPATH.'classes/security.php', 'Session' => APPPATH.'classes/session.php', 'Validation' => APPPATH.'classes/validation.php', 'Fieldset' => APPPATH.'classes/fieldset.php', ));
class Validation extends \Fuel\Core\Validation { public static function test() { $x = 1; } public function _validation_unique($form_value, $params) { list($db_table, $unique_field, $id_field, $id, $form_name) = explode('|', $params); $query = DB::select("LOWER (\"$unique_field\")") ->from($db_table) ->where("$unique_field", '=', MBSTRING ? mb_strtolower($form_value) : strtolower($form_value)) ->and_where($id_field, '!=', $id) ; $result = $query->execute(); if($result->count() == 0) { return true; } else { // Get the Validation instance for the current Fieldset and set error message $val = Fieldset::instance($form_name)->validation(); $val->set_message('unique', 'That :label is already used. Please pick a different :label.'); return false; } } }
Jelmer Schreuder wrote on Sunday 22nd of May 2011:There's an instance of the Validation class in the callables array, as I explained before. Also if you're on RC2, there was a bug in the factory method that made it always return the core class - this has been fixed in the develop branch already.
$this->set_message()? EDIT 2:On a side note, when trying to have a generic validation callback such as _validation_unique() and trying to add an error message, the validation instance is needed. In order to get the validation instance when using a Fieldset, the name assigned to the Fieldset is needed. I ended up passing the Fieldset name (which is not a public variable) to _validation_unique(). Is there an easier or preferred way to to do this?
Ok, now we're getting somewhere. I'm not sure why you're surprised that the 2nd callable is an instance of the Validation class, as that's exactly what I said it would & should be (otherwise you couldn't use $this in the validation methods). Now the question is why it's an instance of Fuel\Core\Validation when you say you're sure that your extension is used. Could you do the following:Mitchell Steinberg wrote on Sunday 22nd of May 2011:I'm sorry, but I'm not sure I followed your answer. However, I downloaded the latest core from the develop branch, and the callback functions still generate an error notice that they're unknown even though the extended validation class appears to be recognized. I don't know if this is any help, but when I trace through the fieldset/field.php add_rule() function, $callables[0] = 'Model_Admin' which is a string representing the model where the fieldset fields are added. But $callables[1] is not a string with the name of the class. Instead it's an object Fuel\Core\Validation. So the function method_exists is false for both elements of the $callables array.
Could you do the following:
- check which type of class the validation instance you're using is, using get_class($validation)
- show me how you're creating the instance
public static function set_form_fields(Fieldset $form, $user_id = null) { $val = $form->validation(); $c = get_class($val); ..... $form->add('email', 'Email', array( 'type' => 'text', 'value' => ! empty($admin['email']) ? $admin['email'] : '', 'id' => 'email', 'size' => 50, ), array( array('required'), array('trim'), array('max_length', 255), array('valid_email'), array('unique', "auth_users|email|user_id|$user_id|$form_name"), ) ); ..... }The class $c is 'Fuel\Core\Validation'. If there's anything else useful to examine, just let me know.
public static function set_form_fields(Fieldset $form, $user_id = null) { ....... $form->add('email', 'Email', array( 'type' => 'text', 'value' => ! empty($admin['email']) ? $admin['email'] : '', 'id' => 'email', 'size' => 50, ), array( array('required'), array('trim'), array('max_length', 255), array('valid_email'), array('unique', "auth_users|email|user_id|$user_id|$form_name"), ) ); ....... }
It looks like you're new here. If you want to get involved, click one of these buttons!