Вопрос

I would like to know how i can set a checkbox $_POSt at 0 when it's not checked (without adding a hidden input)

my model extends ORM :

public function rules()
{
    return array(
        'title' => array(
            array('not_empty')
        )
    );
}

My html form :

<form>
<input type="text" name="title">
<input type="checkbox" name="is_send" value="1">
</form>

My controller

try
    {
        $mymodel = ORM::factory('mymodel')->values($_POST, array("title", "is_send"));
            $mymodel->save();
    }
    catch (ORM_Validation_Exception $e)
    {
       $errors = $e->errors();
    }

The problem is that when i don't check the box "is_send" i can't put is_send to 0, because it's not isset, any idea ?

thx

Это было полезно?

Решение

You want something like this:

$post = $this->request->post();
$post['is_send'] = Arr::get($post, 'is_send', 0);

try
{
    $mymodel = ORM::factory('mymodel')->values($post);
    $mymodel->save();
}
catch (ORM_Validation_Exception $e)
{
    $errors = $e->errors();
 }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top